spring-boot-starter-validation 校验参数的实现

一、前言

本章介绍使用spring-boot-starter-validation 校验 SpringMVC 的入参。

	
		org.springframework.boot
		spring-boot-starter-parent
		2.5.2
		 
	
	
		
			org.springframework.boot
			spring-boot-starter-validation
		

二、常用注解

spring-boot-starter-validation 校验参数的实现_第1张图片

三、定义分组

用于分组校验。
使用场景,对同一个对象例如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 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 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  {
		/** 成功 */	
		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  JsonResult success(T data) {
            return new JsonResult (SUCCESS,"请求处理成功",data);
        }
        public static   JsonResult success(String msg,T data) {
            return new JsonResult(SUCCESS, msg,data);
        }
        

        
        public static JsonResult err() {
            return build(EXCEPTION);
        }
        public static JsonResult err(String msg) {
            return build(EXCEPTION, msg);
        }
        
	
        public JsonResult code(int code) {
            this.code = code;
            return this;
        }
        public JsonResult msg(String msg) {
            this.msg = msg;
            return this;
        }
        public JsonResult 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().code(code).msg(msg);
        }
        public static  JsonResult build(int code, T data) {
            return new JsonResult().code(code).data(data);
        }
        public static  JsonResult build(int code, String msg, T data) {
            return new JsonResult().code(code).msg(msg).data(data);
        }
        
        
}

当然还有其他异常处理方式可以参考:https://www.jb51.net/article/244379.htm

七、测试效果

spring-boot-starter-validation 校验参数的实现_第2张图片

spring-boot-starter-validation 校验参数的实现_第3张图片

八、嵌套对象的校验

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 {
	@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[] 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.4测试

访问接口

@PostMapping("test6")
public User test6(@Validated({ValidateGroup.SecondeGroup.class}) @RequestBody User u) {
System.out.println(u);
return u;
}

spring-boot-starter-validation 校验参数的实现_第4张图片

到此这篇关于spring-boot-starter-validation 校验参数的实现的文章就介绍到这了,更多相关spring-boot-starter-validation 校验参数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(spring-boot-starter-validation 校验参数的实现)