springboot自动判定空值

/ 默认分类 / 没有评论 / 2952浏览

Spring Boot 参数校验

前言

搭建springboot项目,我们都是采用的Restful接口,那么问题来了,当前端调用接口或者是其他项目调用时,我们不能单一靠调用方来控制参数的准确性,自己也要对一些非空的 值进行判定。

方案

按照我们以往的做法,都是对request中的参数一个一个进行非空判定。

Model:

publicclassOrder{
 private Long userID;
 private Long addressID;
 private String comment;
}

Controller:

@PostMapping("/createOrders")
publicStringcreateOrders(@RequestBodyOrderdto){
		if(dto.getUserID==null){
      	return "userID不能为空";
    }
		if(dto.getAddressID==null){
        return "addressID不能为空";
    }
		if(dto.getComment==null){
        return "comment不能为空";
    }
		return "sucess";

}

这种做法首先是可取的,能达到我们的要求,但是这样如果model字段过多,判定的就很 多,相对维护起来就不是那么方便,其次增加controller层的负担,既然我们来到spring4 的时代,就应该适应使用注解的趋势,下面是使用注解后的比变化。

Model:

publicclassOrder{

	@NotNull(message = "用户ID不能为空")
	private Long userID;

	@NotNull(message = "收货人地址id不能为空")
	private Long addressID;

	@NotBlank(message = "备注不为空")
	private String comment;
}

Controller:

@PostMapping("/createOrders")
publicStringcreateOrders(@RequestBody @Valid Order dto,BindingResult results) {

		if (results.hasErrors()){
			return results.getFieldError().getDefaultMessage();      
    }
		return "success";

}

这样我们就只需要在model字段上加上非空验证和相应提示语就好了。 备注:@Valid 和@Validated效果一样,可以加在controller中,也可以加载dto上

常用的校验注解

1. javax.validation.constraints.NotNull 
2. @Null 被注释的元素必须为null
3. @NotNull 被注释的元素不能为null 
4. @AssertTrue 被注释的元素必须为true
5. @AssertFalse 被注释的元素必须为false 
6. @Min(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最小值 
7. @Max(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最大值
8. @DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的 最小值
9. @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的 最大值
10. @Size(max,min) 被注释的元素的大小必须在指定的范围内。
11. @Digits(integer,fraction) 被注释的元素必须是一个数字,其值必须在可接 受的范围内
12. @Past 被注释的元素必须是一个过去的日期
13. @Future 被注释的元素必须是一个将来的日期
14. @Pattern(regexp) 被注释的元素必须符合指定的正则表达式。
15. @Email 被注释的元素必须是电子邮件地址
16. @Length 被注释的字符串的大小必须在指定的范围内
17. @NotEmpty 被注释的字符串必须非空
18. @Range 被注释的元素必须在合适的范围内

其他

@Valid 注解类型的使用:
@Null
限制只能为null
@NotNull
限制必须不为null
@AssertFalse
限制必须为false,
@AssertTrue
限制必须为true,
@DecimalMax(value)
限制必须为一个不大于指定值的数字
@DecimalMin(value)
限制必须为一个不小于指定值的数字
@Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过 fraction
@Future
限制必须是一个将来的日期
@Max(value)
限制必须为一个不大于指定值的数字
@Min(value)
限制必须为一个不小于指定值的数字
@Past
限制必须是一个过去的日期
@Pattern(value)
限制必须符合指定的正则表达式
@Size(max,min)
限制字符长度必须在min到max之间
@Past
验证注解的元素值(日期类型)比当前时间早
@NotEmpty 
验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) 
@NotBlank 
验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty, @NotBlank只应用于字符串且在比较时会去除字符串的空格
@Email
验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式

问题

@NotBlank无效 可能你为了使用@NotBlank引入了包

<dependency>
	<groupId>jakarta.validation</groupId>
	<artifactId>jakarta.validation‐api</arti
	<version>2.0.2</version>
</dependency>

之所以需要引入这个包,是因为你的spring boot 版本是2.3.1或者更高,此时的spring boot 已经不在内置验证。 此时需要引入包 哪怕与@Valid搭配也是没有效果,大概率是因为我们少导入了一个包hibernate- validator,我们需要同时导入以下两个包


 <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring‐boot‐starter‐validation</artifactId> 
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
		<groupId>org.hibernate.validator</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>6.0.2.Final</version>
</dependency>

或者是不导入以上两个包,直接将spring boot修改为2.1.1均可以解决此问题

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring‐boot‐starter‐web</artifactId>
		<version>2.1.1.RELEASE</version>
</dependency>

扩展

1.@NotNull:不能为null,但可以为empty

("",""," ")


2.@NotEmpty:不能为null,而且长度必须大于0

(" "," ")

3.@NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0("test") 即:必须有实际字符


@NotNull:The CharSequence,Collection,Map or Array object is not null,but can be empty.

@NotEmpty:The CharSequence,Collection,Map or Array object is not null and size > 0 .

@NotBlank:The string is not null and the trimmed length is greater than zero.

Stringname=null;
@NotNull:false
@NotEmpty:false
@NotBlank:false

Stringname="";
@NotNull:true
@NotEmpty:false
@NotBlank:false

Stringname=" ";
@NotNull:true
@NotEmpty:true
@NotBlank:false

Stringname="Greatanswer!";
@NotNull:true
@NotEmpty:true
@NotBlank:true