springboot参数校验 spring-boot-starter-validation
spring-boot-starter-validation 校验参数
一、前言
本章介绍使用spring-boot-starter-validation 校验 SpringMVC 的入参。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
二、常用注解
三、定义分组
用于分组校验。
使用场景,对同一个对象例如User(username , id) 在不同的接口时 需要的校验规则不同。
例如,访问一个接口需要 username 不为null且长度大于0 ,id>=0 ; 访问另一个接口 需要 username 参数的长度 在 [1,3]之间。
public class ValidateGroup {
public interface FirstGroup {
}
public interface SecondeGroup {
}
public interface ThirdGroup {
}
}
四、定义需要校验的对象
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import lombok.Data;
@Data
public class User {
@NotEmpty(message = "用户名不能为空")
@Size(message = "用户名长度 [1-3] ", min = 1, max = 3,groups = ValidateGroup.FirstGroup.class)
private String username;
@Min(message = "id不得小于0", value = 0)
private Integer id;
}
五、在handler 即 Controller中 校验
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nbpicc.controller.ValidateGroup.FirstGroup;
@RestController
@RequestMapping("/")
public class TestController {
@PostMapping("test3")
public User test3(@RequestBody @Validated({ FirstGroup.class }) User u) {
System.out.println(u);
return u;
}
@PostMapping("test4")
public User test4(@Validated User u) {
System.out.println(u);
return u;
}
}
校验失败,会直接抛出异常。这样不太友好,可以使用@ControllerAdvice处理全局异常。
六、定义全局异常处理类
import java.util.List;
import org.springframework.validation.BindException;
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 lombok.extern.slf4j.Slf4j;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = BindException.class)
public JsonResult exceptionHandle(BindException exception) {
BindingResult result = exception.getBindingResult();
StringBuilder errorMsg = new StringBuilder();
List<FieldError> fieldErrors = result.getFieldErrors();
fieldErrors.forEach(error -> {
log.error("field: " + error.getField() + ", msg:" + error.getDefaultMessage());
errorMsg.append(error.getDefaultMessage()).append("!");
});
return JsonResult.fail(errorMsg.toString());
}
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public JsonResult MyExceptionHandle(MethodArgumentNotValidException exception) {
BindingResult result = exception.getBindingResult();
StringBuilder errorMsg = new StringBuilder();
List<FieldError> fieldErrors = result.getFieldErrors();
fieldErrors.forEach(error -> {
log.error("field: " + error.getField() + ", msg:" + error.getDefaultMessage());
errorMsg.append(error.getDefaultMessage()).append("!");
});
return JsonResult.fail(errorMsg.toString());
}
// 处理运行时异常
@ExceptionHandler(RuntimeException.class)
public JsonResult doHandleRuntimeException(RuntimeException e) {
log.error(e.getMessage(), e);
e.printStackTrace();
return JsonResult.fail(e.getMessage());
}
}
另外JsonResult.java用于接口返回统一个json格式
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
*@author wang
*@Date 2020-9-14
*
**/
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JsonResult<T> {
/** 成功 */
public static final int SUCCESS=200;
/**内部服务器错误**/
public static final int FAIL=500;
/** 没有登录 */
public static final int NOT_LOGIN = 400;
/** 发生异常 */
public static final int EXCEPTION = 401;
/** 系统错误 */
public static final int SYS_ERROR = 402;
/** 参数错误 */
public static final int PARAMS_ERROR = 403;
/** 不支持或已经废弃 */
public static final int NOT_SUPPORTED = 410;
/** AuthCode错误 */
public static final int INVALID_AUTHCODE = 444;
/** 太频繁的调用 */
public static final int TOO_FREQUENT = 445;
/** 未知的错误 */
public static final int UNKNOWN_ERROR = 499;
private Integer code;
private String msg;
private T data;
public static JsonResult fail() {
return new JsonResult(FAIL, "请求处理失败",null);
}
public static JsonResult fail(String msg) {
return new JsonResult(FAIL, msg,null);
}
public static JsonResult fail(Integer code,String msg) {
return new JsonResult(code, msg,null);
}
public static JsonResult success() {
return new JsonResult(SUCCESS,"请求处理成功",null);
}
public static JsonResult success(String msg) {
return new JsonResult(SUCCESS,msg,null);
}
public static <T> JsonResult success(T data) {
return new JsonResult<T> (SUCCESS,"请求处理成功",data);
}
public static <T> JsonResult success(String msg,T data) {
return new JsonResult<T>(SUCCESS, msg,data);
}
public static JsonResult err() {
return build(EXCEPTION);
}
public static JsonResult err(String msg) {
return build(EXCEPTION, msg);
}
public JsonResult<T> code(int code) {
this.code = code;
return this;
}
public JsonResult<T> msg(String msg) {
this.msg = msg;
return this;
}
public JsonResult<T> data(T data) {
this.data = data;
return this;
}
public static JsonResult build() {
return new JsonResult();
}
public static JsonResult build(int code) {
return new JsonResult().code(code);
}
public static JsonResult build(int code, String msg) {
return new JsonResult<String>().code(code).msg(msg);
}
public static <T> JsonResult<T> build(int code, T data) {
return new JsonResult<T>().code(code).data(data);
}
public static <T> JsonResult<T> build(int code, String msg, T data) {
return new JsonResult<T>().code(code).msg(msg).data(data);
}
}
当然还有其他异常处理方式可以参考:
https://www.cnblogs.com/hujunzheng/p/9952563.html#autoid-0-0-0
七、测试效果
--------------------------------------
--------------------------------------
八、嵌套对象的校验
import lombok.Data;
import javax.validation.Valid;
import javax.validation.constraints.*;
@Data
public class User {
@NotEmpty(message = "用户名不能为空")
@Size(message = "长度 [1-3] ", min = 1, max = 3, groups = ValidateGroup.FirstGroup.class)
private String username;
@Min(message = "id不得小于0", value = 0)
private Integer id;
@NotBlank(message = "地址不能为空", groups = {ValidateGroup.ThirdGroup.class, ValidateGroup.SecondeGroup.class})
private String address;
//在内部属性是自定义对象的时候添加 @Valid 注解 ,即可开启对In对象的校验。
//记得添加@NotNull注解,否则该对象可以为null,并且此时In对象的校验规则也不会抛出异常 。
@Valid
@NotNull(message = "In 对象不能为null ", groups = {ValidateGroup.ThirdGroup.class, ValidateGroup.SecondeGroup.class})
private In in;
}
/**
*自定义的对象,User对象中的一个属性。
*/
@Data
public class In {
@NotBlank(message = "str不能为空", groups = {ValidateGroup.ThirdGroup.class, ValidateGroup.SecondeGroup.class})
private String str;
}
九、自定义注解(自定义校验规则)
9.1 实现ConstraintValidator接口
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class WordConstraintValidator implements ConstraintValidator<CustomValidaor, Object> {
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
// 具体的校验规则
return value.toString().length() == 10;
}
}
9.2 自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = WordConstraintValidator.class)
public @interface CustomValidaor {
String message();
// groups 和 payload 这两个parameter 必须包含,不然会报错
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
9.3 使用
@Data
public class Inner {
@NotBlank(message = "str不能为空", groups = { ValidateGroup.ThirdGroup.class, ValidateGroup.SecondeGroup.class })
@CustomValidaor(message = "长度必须为10", groups = { ValidateGroup.ThirdGroup.class, ValidateGroup.SecondeGroup.class })
private String str;
}
9.3测试
访问接口
@PostMapping("test6")
public User test6(@Validated({ValidateGroup.SecondeGroup.class}) @RequestBody User u) {
System.out.println(u);
return u;
}