validation 验证参数
一、引入POM依赖
添加spring-boot-starter-validation
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
或添加hibernate-validator
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
或添加spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-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=):小于等于value,inclusive=true是小于等于。
- @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;
@Past
@Past(message = "首次发表日期不正确")
@ApiModelProperty("首次发表日期")
@NotBlank(message = "首次发表日期不能为空")
private String publishDate;
@Size @Empty
@Empty(message = "字体文件数量不能为空")
@Size(max = 50, message = "字体文件数量过多")
@ApiModelProperty("字体文件")
private List<Long> fontFile;
@Length
@Length(max = 20, message = "企业法人名称过长")
@ApiModelProperty("企业法人名称")
private String legalName;
@Range
@Range(min=0,max=2,message="非法性别")
private String sex;
@Email(message="非法邮件地址")
private String email;
@Min @Max
@Min(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 ResultVo<String> 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();
Set<ConstraintViolation<T>> 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);
}
Set<ConstraintViolation<T>> 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 @Valid
@Validated和@Valid都是Java中用于数据校验的注解,它们通常与Java Bean Validation(JSR 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
异常。
六、全局异常处理ConstraintViolationException
import 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="";
Set<ConstraintViolation<?>> 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 ="";
List<ObjectError> errors = ((BindException) e).getBindingResult().getAllErrors();
msg = getValidExceptionMsg(errors);
return ResultVo.error(ErrorCode.ILLEGAL_PARAMETER,msg);
}
//MethodArgumentNotValidException
if (e instanceof MethodArgumentNotValidException){
// post请求的对象参数校验异常
String msg="";
List<ObjectError> 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(List<ObjectError> 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;
}
}