validation 验证参数

张开发
2026/5/20 2:00:13 15 分钟阅读
validation 验证参数
validation 验证参数一、引入POM依赖添加spring-boot-starter-validationdependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-validation/artifactId /dependency或添加hibernate-validatordependency groupIdorg.hibernate.validator/groupId artifactIdhibernate-validator/artifactId /dependency或添加spring-boot-starter-webdependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency二、校验注解JSR提供的校验注解Null被注释的元素值必须为null。NotNull被注释的元素值必须不为null。Pattern(regex)被注释的元素字符串必须符合指定的正则表达式。Size(max, min)集合元素数量必须在min和max范围内。AssertTrue被注释的元素必须为true。AssertFalse被注释的元素必须为false。Min(value)被注释的元素必须是一个数字其值必须大于等于指定的最小值。Max(value)被注释的元素必须是一个数字其值必须小于等于指定的最大值。Range(min,max)数字必须在min和max范围内。DecimalMin(value)被注释的元素必须是一个数字其值必须大于等于指定的最小值。DecimalMax(value)被注释的元素必须是一个数字其值必须小于等于指定的最大值。Digits (integer, fraction)被注释的元素必须是一个数字其值必须在可接受的范围内。Past被注释的元素必须是一个过去的日期。Future被注释的元素必须是一个将来的日期。Email字符串必须是Email地址。SafeHtml字符串必须是安全的html。URL字符串必须是合法的URL。CreditCardNumber(ignoreNonDigitCharacters)字符串必须是信用卡号按照美国的标准验证。Size(max,min)限制字符长度必须在min到max之间。Hibernate Validator提供的校验注解NotBlank(message )验证字符串非null且trim后长度必须大于0。Length(min,max)被注释的字符串的大小必须在指定的范围内。NotEmpty被注释的字符串的必须非空。Range(min,max,message)被注释的元素必须在合适的范围内。AssertFalse校验false。AssertTrue校验true。DecimalMax(value,inclusive)小于等于valueinclusivetrue是小于等于。DecimalMin(value,inclusive)与上类似。Max(value)小于等于value。Min(value)大于等于value。NotNull检查Null。Past检查日期。Pattern(regex,flag)正则。Size(min, max)字符串集合map限制大小。Valid对po实体类进行校验。三、常用注解的使用Controller添加Valid或者Validated都可以RestController RequestMapping(/) public class DemoController { RequestMapping(test) public String test(Valid RequestBody request request) { } }Pattern NotBlank//正则 手机号格式是否正确 public static final String REGEX_PHONE (^$)|(^[1][3-9][0-9]{9}$); Pattern(regexp Constants.REGEX_PHONE, message 借款人手机号格式不正确) NotBlank(message 借款人手机号不能为空) ApiModelProperty(借款人手机号) private String borrowerPhone;Pattern(regexp [ABCD], message 权利取得方式不正确) ApiModelProperty(权利取得方式原始A继承B承受C其他D) NotBlank(message 权利取得方式不能为空) private String acqMode;Pattern(regexp agree|disagree, message 分发权利不正确) ApiModelProperty(分发权利agree同意分发disagree不同意分发) private String copyrightDispense;PastPast(message 首次发表日期不正确) ApiModelProperty(首次发表日期) NotBlank(message 首次发表日期不能为空) private String publishDate;Size EmptyEmpty(message 字体文件数量不能为空) Size(max 50, message 字体文件数量过多) ApiModelProperty(字体文件) private ListLong fontFile;LengthLength(max 20, message 企业法人名称过长) ApiModelProperty(企业法人名称) private String legalName;RangeRange(min0,max2,message非法性别) private String sex;EmailEmail(message非法邮件地址) private String email;Min MaxMin(value 1, message 作品是否涉及字体1-4以内整数) Max(value 4, message 作品是否涉及字体1-4以内整数) ApiModelProperty(作品是否涉及字体) private Integer isHasFont;四、分组校验public class ValidationGroups { public interface ValidA {} public interface ValidB {} } ApiModelProperty(身份证背面id) NotNull(message 身份证背面id不能为空, groups {ValidationGroups.ValidA.class,ValidationGroups.ValidB.class}) private Long backAttachId; ApiModelProperty(证件id) NotNull(message 证件id不能为空, groups ValidationGroups.ValidB.class) private Long businessAttachId; //注解传参校验 public ResultVoString addOrUpdateOwnerTemplate( Validated(value {ValidationGroups.ValidA.class,ValidationGroups.ValidB.class}) NotNull OwnerTemplateReq ownerTemplateReq) throws Exception { return ownerTemplateService.addOrUpdateOwnerTemplate(ownerTemplateReq); } //手写代码校验 if (条件) { beanValidate(ownerTemplateReq, ValidationGroups.ValidA.class); } else { beanValidate(ownerTemplateReq, ValidationGroups.ValidB.class); } //校验方法 private static T void beanValidate(T object, Class?... groups) throws ValidationException { Validator validator Validation.buildDefaultValidatorFactory().getValidator(); SetConstraintViolationT validate1 validator.validate(object); if (Objects.nonNull(validate1) validate1.size() 0) { String msg validate1.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(|)); throw new ServiceException(msg); } SetConstraintViolationT validate validator.validate(object, groups); if (Objects.nonNull(validate) validate.size() 0) { String msg validate.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(|)); throw new ServiceException(msg); } }五、Validated ValidValidated和Valid都是Java中用于数据校验的注解它们通常与Java Bean ValidationJSR 303规范一起使用。在Spring框架中可以使用这两个注解对方法参数进行校验。Validated这个注解用于类级别表示该类中的所有方法都会进行数据校验。它主要用于分组校验可以将不同的校验规则应用到不同的组上。Valid这个注解用于方法参数级别表示对该参数进行数据校验。当请求中的参数不满足校验规则时会抛出MethodArgumentNotValidException异常。例如import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; RestController Validated public class UserController { PostMapping(/user) public String createUser(Valid RequestBody User user) { // 保存用户信息 return success; } }在这个例子中createUser方法接收一个User对象作为参数并使用Valid注解对其进行校验。如果请求中的User对象不满足校验规则会抛出MethodArgumentNotValidException异常。六、全局异常处理ConstraintViolationExceptionimport com.fa.notary.common.enums.base.ErrorCode; import com.fa.notary.vo.ResultVo; import io.netty.util.internal.ThrowableUtil; import lombok.extern.log4j.Log4j2; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.dao.DuplicateKeyException; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import java.text.MessageFormat; import java.util.List; import java.util.Set; RestControllerAdvice Log4j2 public class GlobalExceptionHandler { /** * 处理所有不可知的异常 */ ExceptionHandler(Throwable.class) public ResultVo handleException(Throwable e){ //DuplicateKeyException唯一索引重复 if (e instanceof DuplicateKeyException) { return ResultVo.error(ErrorCode.ERROR,请勿重复添加); } //ServiceException if(e instanceof ServiceException){ return ResultVo.error(ErrorCode.ERROR,((ServiceException) e).getMsg()); } //ServiceException if(e instanceof RetryException){ return ResultVo.error(ErrorCode.ERROR,((RetryException) e).getMsg()); } //MissingServletRequestParameterException if(e instanceof MissingServletRequestParameterException){ String msg MessageFormat.format(缺少参数{0}, ((MissingServletRequestParameterException) e).getParameterName()); return ResultVo.error(ErrorCode.ILLEGAL_PARAMETER,msg); } //ConstraintViolationException if(e instanceof ConstraintViolationException){ // 单个参数校验异常 String msg; SetConstraintViolation? sets ((ConstraintViolationException) e).getConstraintViolations(); if(CollectionUtils.isNotEmpty(sets)){ StringBuilder sb new StringBuilder(); sets.forEach(error - { if (error instanceof FieldError) { sb.append(((FieldError)error).getField()).append(:); } sb.append(error.getMessage()).append(;); }); msg sb.toString(); msg StringUtils.substring(msg, 0, msg.length() -1); } return ResultVo.error(ErrorCode.ILLEGAL_PARAMETER,msg); } //BindException if (e instanceof BindException){ // get请求的对象参数校验异常 String msg ; ListObjectError errors ((BindException) e).getBindingResult().getAllErrors(); msg getValidExceptionMsg(errors); return ResultVo.error(ErrorCode.ILLEGAL_PARAMETER,msg); } //MethodArgumentNotValidException if (e instanceof MethodArgumentNotValidException){ // post请求的对象参数校验异常 String msg; ListObjectError errors ((MethodArgumentNotValidException) e).getBindingResult().getAllErrors(); msg getValidExceptionMsg(errors); return ResultVo.error(ErrorCode.ILLEGAL_PARAMETER,msg); } // 打印堆栈信息 log.error(ThrowableUtil.stackTraceToString(e)); return ResultVo.error(ErrorCode.ERROR,网络繁忙请稍后再试); } private String getValidExceptionMsg(ListObjectError errors) { if(CollectionUtils.isNotEmpty(errors)){ StringBuilder sb new StringBuilder(); errors.forEach(error - { if (error instanceof FieldError) { sb.append(((FieldError)error).getField()).append(:); } sb.append(error.getDefaultMessage()).append(;); }); String msg sb.toString(); msg StringUtils.substring(msg, 0, msg.length() -1); return msg; } return null; } }

更多文章