欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring注解之@validated使用詳解

 更新時間:2023年01月09日 15:37:29   作者:愛吃biangbiang面  
spring-boot中可以用@validated來校驗數(shù)據(jù),如果數(shù)據(jù)異常則會統(tǒng)一拋出異常,方便異常中心統(tǒng)一處理,這篇文章主要介紹了Spring注解之@validated使用,需要的朋友可以參考下

Spring注解之@validated使用

概念

spring-boot中可以用@validated來校驗數(shù)據(jù),如果數(shù)據(jù)異常則會統(tǒng)一拋出異常,方便異常中心統(tǒng)一處理。

注解源碼:

@Validated   作用在類、方法和參數(shù)上

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Validated {
    Class<?>[] value() default {};
}

依賴

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

spring-boot-starter-web已經(jīng)引入了基礎(chǔ)包,所以直接使用就可以了,沒必要單獨引入此依賴

使用(在controller中使用)

方法接收參數(shù)的實體類上使用

    /**
     * 注冊接口
     *
     * @param registerDTO
     * @return
     */
    @ApiOperation("注冊接口(賬戶、昵稱不能重復(fù))[不需要token]")
    @PostMapping("register")
    public ResponseVO<Object> register(@RequestBody @Validated RegisterDTO registerDTO) throws InterruptedException {
        log.info("請求接口 /netsadcloud/user/register 參數(shù):{}", JSON.toJSONString(registerDTO));
        return userService.register(registerDTO);
    }

實體類

/**
 * @author Boss
 */
@ApiModel("注冊DTO")
@Data
public class RegisterDTO {
    @ApiModelProperty(value = "賬戶(小于等于12位,大于等于8位字符)", required = true)
     @Size(max = 12,min = 8)
    @NotBlank
    private String account;
    @ApiModelProperty(value = "密碼(小于等于12位,大于等于8位字符)", required = true)
     @Size(max = 12,min = 8)
    @NotBlank
    private String password;
    @ApiModelProperty(value = "昵稱(小于等于12位,大于等于8位字符)", required = true)
     @Size(max = 12,min = 8)
    @NotBlank
    private String name;
}

常用注解類型

注意,不要錯用了異常類型,比如在int上不可用@size,常用注解如下:

PS:spring@Validated校驗用法

1、controller添加注解

public BaseResponse addOrUpdateUnit(@RequestBody @Validated RiskUnitDto riskUnitDto) {
        doublePreventDataService.addOrUpdateUnit(riskUnitDto);
        return BaseResponse.success(null);
    }

2、參數(shù)對象添加注解

package com.cosmo.hg.synctask.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Delegate;
import org.hibernate.validator.constraints.Range;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.List;

@Data
@ApiModel(value = "RiskUnitDto對象", description = "風險分析單元新增/編輯請求類")
public class RiskUnitDto {

    @Size(max = 32,message = "sign不能超過32個字符")
    @ApiModelProperty(value = "標識",required = true)
    @NotBlank(message = "sign不能為空")
    private String sign;

    @Valid
    @ApiModelProperty(value = "風險分析單元對象數(shù)據(jù)",required = true)
    private List<RiskUnitData> data;

    @Data
    public static class RiskUnitData{
        @ApiModelProperty(value = "主鍵id",required = true)
        @NotNull(message = "風險分析單元id不能為空")
        private Long id;

        @ApiModelProperty(value = "風險分析對象id",required = true)
        @NotNull(message = "風險分析對象id不能為空")
        private Long riskObjectId;

        @ApiModelProperty(value = "分析單元名稱",required = true)
        @NotBlank(message = "分析單元名稱不能為空")
        @Size(max = 50,message = "分析單元名稱不能超過50個字符")
        private String riskUnitName;

        @ApiModelProperty(value = "經(jīng)度",required = true)
        @NotNull(message = "經(jīng)度不能為空")
        private BigDecimal longitude;
        @NotNull(message = "緯度不能為空")
        @ApiModelProperty(value = "緯度",required = true)
        private BigDecimal dimension;

        @NotBlank(message = "riskUnitLocation:分析單元位置不能為空")
        @ApiModelProperty(value = "分析單元位置",required = true)
        @Size(max = 100,message = "riskUnitLocation:分析單元位置不能大于100個字符")
        private String riskUnitLocation;

//        @ApiModelProperty(value = "組織機構(gòu)編碼")
//        private String orgCode;
//
//        @ApiModelProperty(value = "風險分析對象序號")
//        private String  serialNum;


        @ApiModelProperty(value = "風險等級")
        @Range(max = 3,min = 0,message = "riskLevel:風險等級傳值不對")
        @NotBlank(message = "riskLevel:風險等級不能為空")
        private String  riskLevel;

        @ApiModelProperty(value = "聯(lián)系電話")
        private String  contactNumber;

//        @ApiModelProperty(value = "責任人")
//        private String  hazardLiablePerson;
        @NotBlank(message = "責任人名稱不能為空")
        @ApiModelProperty(value = "責任人名稱",required = true)
        @Size(max = 50,message = "責任人名稱不能大于50個字符")
        private String  hazardLiablePersonName;

        @NotBlank(message = "風險分析對象名稱不能為空")
        @ApiModelProperty(value = "風險分析對象名稱",required = true)
        @Size(max = 50,message = "風險分析對象名稱不能大于50個字符")
        private String  riskObjectName;
        @NotBlank(message = "分析單元編碼不能為空")
        @ApiModelProperty(value = "分析單元編碼",required = true)
        @Size(max = 50,message = "分析單元編碼不能大于50個字符")
        private String riskUnitCode;


        @NotBlank(message = "是否具有中毒、爆炸、火災(zāi)等危險的場所 0-否1-是不能為空")
        @ApiModelProperty(value = "是否具有中毒、爆炸、火災(zāi)等危險的場所 0-否1-是")
        @Range(max = 1,min = 0,message = "是否具有中毒、爆炸、火災(zāi)等危險的場所 0-否1")
        private String dangerousPlace;

        @NotBlank(message = "設(shè)備設(shè)施編號不能為空")
        @ApiModelProperty(value = "設(shè)備設(shè)施編號",required = true)
        @Size(max = 50,message = "equipmentId:設(shè)備設(shè)施編號不能大于50個字符")
        private String equipmentId;

        @NotBlank(message = "作業(yè)活動編號不能為空")
        @ApiModelProperty(value = "作業(yè)活動編號",required = true)
        @Size(max = 50,message = "activityworkId:作業(yè)活動編號不能大于50個字符")
        private String activityworkId;

        @NotBlank(message = "riskpointType:風險點類型不能為空")
        @ApiModelProperty(value = "風險點類型",required = true)
        @Range(max = 3,min = 1,message = "riskpointType:風險點類型傳值不對")
        private String riskpointType;
    }
}

說明:

@NotBlank 校驗字符串,并且校驗字符串是否為空""
@NotNull 校驗是否為空null,包裝類型
@Size字符串長度校驗
@Range數(shù)字范圍校驗  @Range(max = 3,min = 0)

到此這篇關(guān)于Spring注解之@validated使用的文章就介紹到這了,更多相關(guān)Spring注解@validated使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring注解驅(qū)動之ApplicationListener異步處理事件說明

    Spring注解驅(qū)動之ApplicationListener異步處理事件說明

    這篇文章主要介紹了Spring注解驅(qū)動之ApplicationListener異步處理事件說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • SpringBoot中@Test的介紹和使用案例

    SpringBoot中@Test的介紹和使用案例

    SpringBoot提供了方便的測試功能,可以很容易地進行單元測試和集成測試,這篇文章主要介紹了SpringBoot中@Test的介紹和使用,需要的朋友可以參考下
    2023-08-08
  • Java多線程 樂觀鎖和CAS機制詳細

    Java多線程 樂觀鎖和CAS機制詳細

    這篇文章主要介紹了Java多線程 樂觀鎖和CAS機制,樂觀鎖是對于數(shù)據(jù)沖突保持一種樂觀態(tài)度,操作數(shù)據(jù)時不會對操作的數(shù)據(jù)進行加鎖,需要的朋友可以參考下
    2021-10-10
  • SpringBoot異常: nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext解決方案

    SpringBoot異常: nested exception is java.lang.NoClassDefFoundE

    這篇文章主要介紹了SpringBoot異常: nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext解決方案,說明了錯誤原因和解決方案,需要的朋友可以參考下
    2021-06-06
  • Spring SpringMVC在啟動完成后執(zhí)行方法源碼解析

    Spring SpringMVC在啟動完成后執(zhí)行方法源碼解析

    這篇文章主要介紹了SpringMVC在啟動完成后執(zhí)行方法源碼解析,還是非常不錯的,在這里分享給大家,需要的朋友可以參考下。
    2017-09-09
  • springboot 如何通過SpringTemplateEngine渲染html

    springboot 如何通過SpringTemplateEngine渲染html

    通過Spring的Thymeleaf模板引擎可以實現(xiàn)將模板渲染為HTML字符串,而不是直接輸出到瀏覽器,這樣可以對渲染后的字符串進行其他操作,如保存到文件或進一步處理,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • 簡單介紹Java垃圾回收機制

    簡單介紹Java垃圾回收機制

    這篇文章主要介紹了簡單介紹Java垃圾回收機制,涉及一些相關(guān)的Java術(shù)語,Hotspot虛擬機,jvm體系結(jié)構(gòu)等內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java青蛙跳臺階問題的解決思路與代碼

    Java青蛙跳臺階問題的解決思路與代碼

    這篇文章主要給大家介紹了關(guān)于Java青蛙跳臺階問題的解決思路與代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 詳解JFX11+IDEA跨平臺打包發(fā)布的完美解決辦法

    詳解JFX11+IDEA跨平臺打包發(fā)布的完美解決辦法

    這篇文章主要介紹了詳解JFX11+IDEA跨平臺打包發(fā)布的完美解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java?Mybatis?foreach嵌套foreach?List<list<Object>>問題

    Java?Mybatis?foreach嵌套foreach?List<list<Object>&

    在MyBatis的mapper.xml文件中,foreach元素常用于動態(tài)生成SQL查詢條件,此元素包括item(必選,元素別名)、index(可選,元素序號或鍵)、collection(必選,指定迭代對象)、open、separator、close(均為可選,用于定義SQL結(jié)構(gòu))
    2024-09-09

最新評論