Spring注解之@validated使用详解
作者:爱吃biangbiang面
spring-boot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理,这篇文章主要介绍了Spring注解之@validated使用,需要的朋友可以参考下
Spring注解之@validated使用
概念
spring-boot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理。
注解源码:
@Validated 作用在类、方法和参数上
@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已经引入了基础包,所以直接使用就可以了,没必要单独引入此依赖
使用(在controller中使用)
方法接收参数的实体类上使用
/** * 注册接口 * * @param registerDTO * @return */ @ApiOperation("注册接口(账户、昵称不能重复)[不需要token]") @PostMapping("register") public ResponseVO<Object> register(@RequestBody @Validated RegisterDTO registerDTO) throws InterruptedException { log.info("请求接口 /netsadcloud/user/register 参数:{}", 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、参数对象添加注解
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 = "风险分析单元对象数据",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 = "经度",required = true) @NotNull(message = "经度不能为空") 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 = "组织机构编码") // 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 = "联系电话") 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 = "是否具有中毒、爆炸、火灾等危险的场所 0-否1-是不能为空") @ApiModelProperty(value = "是否具有中毒、爆炸、火灾等危险的场所 0-否1-是") @Range(max = 1,min = 0,message = "是否具有中毒、爆炸、火灾等危险的场所 0-否1") private String dangerousPlace; @NotBlank(message = "设备设施编号不能为空") @ApiModelProperty(value = "设备设施编号",required = true) @Size(max = 50,message = "equipmentId:设备设施编号不能大于50个字符") private String equipmentId; @NotBlank(message = "作业活动编号不能为空") @ApiModelProperty(value = "作业活动编号",required = true) @Size(max = 50,message = "activityworkId:作业活动编号不能大于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数字范围校验 @Range(max = 3,min = 0)
到此这篇关于Spring注解之@validated使用的文章就介绍到这了,更多相关Spring注解@validated使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!