springboot validation校验参数

使用validation完成前后端参数校验
首先导入依赖:(spring boot2.0以上不需要导入依赖)


 javax.validation
 validation-api
 2.0.1.Final

参数详解:

注解名
适用的类型
含义




@AssertFalse
布尔值,布尔值
用于布尔字段,该字段只能为真


@AssertFalse
布尔值,布尔值
用于布尔字段,该字段只能为假


@DecimalMax(值= x)的
BigDecimal,BigInteger,String,byte,short,int,long
验证注解的元素值小于等于@ DecimalMax指定的值值


@DecimalMin(值= x)的
BigDecimal,BigInteger,String,byte,short,int,long
验证注解的元素值大于等于@ DecimalMin指定的值值


@Digits(整数=整数位数,分数=小数位数)
BigDecimal,BigInteger,String,byte,short,int,long
验证注解的元素值的整数位数和小数位数上限


@电子邮件
为CharSequence
验证注解的元素值是电子邮件,也可以通过正则表达式和标志指定自定义的电子邮件格式


@Future(整数=整数位数,分数=小数位数)
java.util.Date,java.util.Calendar
验证注解的元素值(日期类型)比当前时间晚


@FutureOrPresent(integer =整数位数,fraction =小数位数)
java.util.Date,java.util.Calendar
验证注解的元素值(日期类型)比当前时间晚或者等于当前时间


@过去
java.util.Date,java.util.Calendar
验证注解的元素值(日期类型)比当前时间早


@PastOrPresent
java.util.Date,java.util.Calendar
验证注解的元素值(日期类型)比当前时间早或等于现在


@Max(值= x)的
BigDecimal,BigInteger,byte,short,int,long
验证注解的元素值小于等于@Max指定的值值


@Mix(值= x)的
BigDecimal,BigInteger,byte,short,int,long
验证注解的元素值大于等于@Max指定的值值


@NotBlank
为CharSequence
验证注解的元素值不为空(不为空时,去除首位空格后长度为0),不同于@ NotEmpty,@ NotBlank只应用于字符串且在比较时会去除字符串的空格


@不是空的
为CharSequence
验证注解的元素值不为空且不为空(字符串长度不为0,集合大小不为0)


@NotNull
随便哪种
验证注解的元素值不是空


@空值
随便哪种
验证注解的元素值是空


@Pattern(正则表达式=正则表达式,flag =)
串
验证注解的元素值与指定的正则表达式匹配


@Size(min =最小值,max =最大值)
字符串,集合,映射和数组
验证注解的元素值的在最小和最大(包含)指定区间之内,如字符长度,集合大小

使用:
在实体类加入一下注解即可,看需求而定,此步为了演示

/** 
* @author  作者 : 小布
* @version 创建时间 : 2019年7月8日 下午5:15:26 
* @explain 类说明 : 
*/
public class User {
	
	@NotBlank(message = "userName不能为空")
    @Size(min=2, max=30, message = "userName长度必须在2和30之间")
	private String userName;
	@NotBlank(message = "password不能为空")
    @Size(min=2, max=30, message = "password长度必须在2和30之间")
	private String password;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}

然后在Controller加入@Validated注解校验

@PostMapping("/test")
    @ApiOperation(value="测试内容",notes="测试内容")
	public Map test(@Validated @RequestBody User user){
		Map mp = new ConcurrentHashMap<>(10000);
 		mp.put("user", user);
		return mp;
	}

@Validated 和 @RequestBody 操作时会出现异常然后定义统一异常处理

/** 
* @author  作者 : 小布
* @version 创建时间 : 2019年6月6日 上午11:17:06 
* @explain 类说明 : 异常捕捉
*/
@RestControllerAdvice
public class ExceptionHandling {
	
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Map exception(MethodArgumentNotValidException e) {
    	Map mp = new ConcurrentHashMap<>(10);
        BindingResult bindingResult = e.getBindingResult();
        List allErrors = bindingResult.getAllErrors();
        List errorMsgs = new ArrayList<>();
        allErrors.forEach(objectError -> {
            FieldError fieldError = (FieldError)objectError;
            errorMsgs.add(fieldError.getDefaultMessage());
        });
        mp.put("code", 400);
		mp.put("message", errorMsgs);//返回参数校验
        return mp;
    }
    

你可能感兴趣的:(spring,boot)