Spring注解之@validated使用詳解
Spring注解之@validated使用
概念
spring-boot中可以用@validated來(lái)校驗(yàn)數(shù)據(jù),如果數(shù)據(jù)異常則會(huì)統(tǒng)一拋出異常,方便異常中心統(tǒng)一處理。
注解源碼:
@Validated 作用在類(lèi)、方法和參數(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ǔ)包,所以直接使用就可以了,沒(méi)必要單獨(dú)引入此依賴
使用(在controller中使用)
方法接收參數(shù)的實(shí)體類(lèi)上使用
/**
* 注冊(cè)接口
*
* @param registerDTO
* @return
*/
@ApiOperation("注冊(cè)接口(賬戶、昵稱(chēng)不能重復(fù))[不需要token]")
@PostMapping("register")
public ResponseVO<Object> register(@RequestBody @Validated RegisterDTO registerDTO) throws InterruptedException {
log.info("請(qǐng)求接口 /netsadcloud/user/register 參數(shù):{}", JSON.toJSONString(registerDTO));
return userService.register(registerDTO);
}實(shí)體類(lèi)
/**
* @author Boss
*/
@ApiModel("注冊(cè)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 = "昵稱(chēng)(小于等于12位,大于等于8位字符)", required = true)
@Size(max = 12,min = 8)
@NotBlank
private String name;
}常用注解類(lèi)型
注意,不要錯(cuò)用了異常類(lèi)型,比如在int上不可用@size,常用注解如下:

PS:spring@Validated校驗(yàn)用法
1、controller添加注解
public BaseResponse addOrUpdateUnit(@RequestBody @Validated RiskUnitDto riskUnitDto) {
doublePreventDataService.addOrUpdateUnit(riskUnitDto);
return BaseResponse.success(null);
}2、參數(shù)對(duì)象添加注解
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對(duì)象", description = "風(fēng)險(xiǎn)分析單元新增/編輯請(qǐng)求類(lèi)")
public class RiskUnitDto {
@Size(max = 32,message = "sign不能超過(guò)32個(gè)字符")
@ApiModelProperty(value = "標(biāo)識(shí)",required = true)
@NotBlank(message = "sign不能為空")
private String sign;
@Valid
@ApiModelProperty(value = "風(fēng)險(xiǎn)分析單元對(duì)象數(shù)據(jù)",required = true)
private List<RiskUnitData> data;
@Data
public static class RiskUnitData{
@ApiModelProperty(value = "主鍵id",required = true)
@NotNull(message = "風(fēng)險(xiǎn)分析單元id不能為空")
private Long id;
@ApiModelProperty(value = "風(fēng)險(xiǎn)分析對(duì)象id",required = true)
@NotNull(message = "風(fēng)險(xiǎn)分析對(duì)象id不能為空")
private Long riskObjectId;
@ApiModelProperty(value = "分析單元名稱(chēng)",required = true)
@NotBlank(message = "分析單元名稱(chēng)不能為空")
@Size(max = 50,message = "分析單元名稱(chēng)不能超過(guò)50個(gè)字符")
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個(gè)字符")
private String riskUnitLocation;
// @ApiModelProperty(value = "組織機(jī)構(gòu)編碼")
// private String orgCode;
//
// @ApiModelProperty(value = "風(fēng)險(xiǎn)分析對(duì)象序號(hào)")
// private String serialNum;
@ApiModelProperty(value = "風(fēng)險(xiǎn)等級(jí)")
@Range(max = 3,min = 0,message = "riskLevel:風(fēng)險(xiǎn)等級(jí)傳值不對(duì)")
@NotBlank(message = "riskLevel:風(fēng)險(xiǎn)等級(jí)不能為空")
private String riskLevel;
@ApiModelProperty(value = "聯(lián)系電話")
private String contactNumber;
// @ApiModelProperty(value = "責(zé)任人")
// private String hazardLiablePerson;
@NotBlank(message = "責(zé)任人名稱(chēng)不能為空")
@ApiModelProperty(value = "責(zé)任人名稱(chēng)",required = true)
@Size(max = 50,message = "責(zé)任人名稱(chēng)不能大于50個(gè)字符")
private String hazardLiablePersonName;
@NotBlank(message = "風(fēng)險(xiǎn)分析對(duì)象名稱(chēng)不能為空")
@ApiModelProperty(value = "風(fēng)險(xiǎn)分析對(duì)象名稱(chēng)",required = true)
@Size(max = 50,message = "風(fēng)險(xiǎn)分析對(duì)象名稱(chēng)不能大于50個(gè)字符")
private String riskObjectName;
@NotBlank(message = "分析單元編碼不能為空")
@ApiModelProperty(value = "分析單元編碼",required = true)
@Size(max = 50,message = "分析單元編碼不能大于50個(gè)字符")
private String riskUnitCode;
@NotBlank(message = "是否具有中毒、爆炸、火災(zāi)等危險(xiǎn)的場(chǎng)所 0-否1-是不能為空")
@ApiModelProperty(value = "是否具有中毒、爆炸、火災(zāi)等危險(xiǎn)的場(chǎng)所 0-否1-是")
@Range(max = 1,min = 0,message = "是否具有中毒、爆炸、火災(zāi)等危險(xiǎn)的場(chǎng)所 0-否1")
private String dangerousPlace;
@NotBlank(message = "設(shè)備設(shè)施編號(hào)不能為空")
@ApiModelProperty(value = "設(shè)備設(shè)施編號(hào)",required = true)
@Size(max = 50,message = "equipmentId:設(shè)備設(shè)施編號(hào)不能大于50個(gè)字符")
private String equipmentId;
@NotBlank(message = "作業(yè)活動(dòng)編號(hào)不能為空")
@ApiModelProperty(value = "作業(yè)活動(dòng)編號(hào)",required = true)
@Size(max = 50,message = "activityworkId:作業(yè)活動(dòng)編號(hào)不能大于50個(gè)字符")
private String activityworkId;
@NotBlank(message = "riskpointType:風(fēng)險(xiǎn)點(diǎn)類(lèi)型不能為空")
@ApiModelProperty(value = "風(fēng)險(xiǎn)點(diǎn)類(lèi)型",required = true)
@Range(max = 3,min = 1,message = "riskpointType:風(fēng)險(xiǎn)點(diǎn)類(lèi)型傳值不對(duì)")
private String riskpointType;
}
}說(shuō)明:
@NotBlank 校驗(yàn)字符串,并且校驗(yàn)字符串是否為空""
@NotNull 校驗(yàn)是否為空null,包裝類(lèi)型
@Size字符串長(zhǎng)度校驗(yàn)
@Range數(shù)字范圍校驗(yàn) @Range(max = 3,min = 0)
到此這篇關(guān)于Spring注解之@validated使用的文章就介紹到這了,更多相關(guān)Spring注解@validated使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring注解驅(qū)動(dòng)之ApplicationListener異步處理事件說(shuō)明
這篇文章主要介紹了Spring注解驅(qū)動(dòng)之ApplicationListener異步處理事件說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Java多線程 樂(lè)觀鎖和CAS機(jī)制詳細(xì)
這篇文章主要介紹了Java多線程 樂(lè)觀鎖和CAS機(jī)制,樂(lè)觀鎖是對(duì)于數(shù)據(jù)沖突保持一種樂(lè)觀態(tài)度,操作數(shù)據(jù)時(shí)不會(huì)對(duì)操作的數(shù)據(jù)進(jìn)行加鎖,需要的朋友可以參考下2021-10-10
SpringBoot異常: nested exception is java.lang.NoClassDefFoundE
這篇文章主要介紹了SpringBoot異常: nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext解決方案,說(shuō)明了錯(cuò)誤原因和解決方案,需要的朋友可以參考下2021-06-06
Spring SpringMVC在啟動(dòng)完成后執(zhí)行方法源碼解析
這篇文章主要介紹了SpringMVC在啟動(dòng)完成后執(zhí)行方法源碼解析,還是非常不錯(cuò)的,在這里分享給大家,需要的朋友可以參考下。2017-09-09
springboot 如何通過(guò)SpringTemplateEngine渲染html
通過(guò)Spring的Thymeleaf模板引擎可以實(shí)現(xiàn)將模板渲染為HTML字符串,而不是直接輸出到瀏覽器,這樣可以對(duì)渲染后的字符串進(jìn)行其他操作,如保存到文件或進(jìn)一步處理,感興趣的朋友跟隨小編一起看看吧2024-10-10
詳解JFX11+IDEA跨平臺(tái)打包發(fā)布的完美解決辦法
這篇文章主要介紹了詳解JFX11+IDEA跨平臺(tái)打包發(fā)布的完美解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java?Mybatis?foreach嵌套foreach?List<list<Object>&
在MyBatis的mapper.xml文件中,foreach元素常用于動(dòng)態(tài)生成SQL查詢條件,此元素包括item(必選,元素別名)、index(可選,元素序號(hào)或鍵)、collection(必選,指定迭代對(duì)象)、open、separator、close(均為可選,用于定義SQL結(jié)構(gòu))2024-09-09

