在 javax.validation.constraints包中定义了非常多的校验注解,引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在实体类上添加对应注解即可,
实体类:
package com.zhmsky.mallproduct.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
@TableName("pms_brand")
public class BrandEntity implements Serializable {
private static final long serialVersionUID = 1L;
@NotBlank(message = "品牌名不能为空")
private String name;
}
一、在controller接口处理校验异常
message可指定校验说明, 接下来在对应的controller接口方法中使用@Valid 注解来指定接口参数校验,后面紧跟BindingResult来接收校验结果,当发生校验失败则封装并返回校验失败信息,如果通过校验则执行相关逻辑
@RequestMapping(value = "/save",method = RequestMethod.POST)
public R save(@Valid @RequestBody BrandEntity brand, BindingResult result){
if(result.hasErrors()){
Map<String, String> map = new HashMap<>();
for (FieldError fieldError : result.getFieldErrors()) {
String message = fieldError.getDefaultMessage();
String field = fieldError.getField();
map.put(field,message);
}
return R.error(20001,"参数校验失败").put("errorMap",map);
}
brandService.save(brand);
return R.ok();
}
二、统一异常处理
如果在每一个接口都进行参数校验异常处理,工作量比较大,并且代码比较繁琐,于是引入全局异常处理, 也就是@RestControllerAdvice+@ExceptionHandler注解的使用。
package com.zhmsky.mallproduct.exception;
import com.zhmsky.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestControllerAdvice(basePackages = {"com.zhmsky.mallproduct.controller"})
public class MallProductControllerAdvice {
@ExceptionHandler(MethodArgumentNotValidException.class)
public R handleValidException(MethodArgumentNotValidException e){
log.error("数据异常:{},异常类型:{}",e.getMessage(),e.getClass());
BindingResult result = e.getBindingResult();
Map<String, String> map = new HashMap<>();
for (FieldError fieldError : result.getFieldErrors()) {
map.put(fieldError.getDefaultMessage(),fieldError.getField());
}
return R.error().put("errorMap",map);
}
@ExceptionHandler(Throwable.class)
public R handleException(Throwable e){
return R.error();
}
}
三、错误码枚举类
package com.zhmsky.common.exception;
public enum ErrorCodeEnum {
UNKNOWN_EXCEPTION(10000, "系统未知错误"),
VALID_EXCEPTION(10001, "参数格式校验失败");
private Integer code;
private String msg;
ErrorCodeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode(){
return this.code;
}
public String getMsg(){
return this.msg;
}
}
四、自定义参数校验注解
1、编写自定义校验注解
参照javax.validation.constraints的@NotBlank注解,
@Documented
@Constraint(validatedBy = { })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface NotBlank {
String message() default "{javax.validation.constraints.NotBlank.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
public @interface List {
NotBlank[] value();
}
}
模仿上面可快速编写一个自定义注解:
@Documented
@Constraint(validatedBy = {StatusValueConstraintValidator.class})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
public @interface statusValue {
String message() default "{com.zhmsky.common.validator.statusValue.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
int[] values() default {};
}
创建 ValidationMessages.properties 文件,在配置文件中配置自定义的校验规则提示信息:
com.zhmsky.common.validator.statusValue.message=error.......xxxx
2、编写自定义校验器
public class StatusValueConstraintValidator implements ConstraintValidator<statusValue, Integer> {
private Set<Integer> set = new HashSet<>();
@Override
public void initialize(statusValue constraintAnnotation) {
int[] values = constraintAnnotation.values();
for (int value : values) {
set.add(value);
}
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return set.contains(value);
}
}
3、关联自定义注解和自定义校验器
也就是在自定义注解的@Constraint注解中指明自定义的校验器,
4、测试自定义注解@statusValue
@Data
@TableName("pms_brand")
public class BrandEntity implements Serializable {
private static final long serialVersionUID = 1L;
@NotBlank(message = "品牌名不能为空")
private String name;
@statusValue(values={0,1})
private Integer showStatus;
}
在对应的controller接口中使用@Validated注解来进行校验,showStatus字段传入值3, 测试结果如图:
|