Use of @Valid annotation types commonly used in development

One: Preface

    When building a springboot project, using the Restful interface, when the front-end call interface or other project calls, we cannot rely solely on the caller to control the accuracy of the parameters, and we must judge some non-empty values ​​ourselves.

Two: traditional solutions

    That is, we perform null operations on the parameters one by one in the code.

Model:

/**
 *  @author: liman
 *  @Date: 2020/7/20 16:27
 *  @Description: 
 */
public class User{
	private Long userID;  
	private Long addressID;
	private String userName;
 }

   Controller:


@PostMapping("/createUser")
public String createUser(@RequestBody User dto) {
	if(dto.getUserID==null)
		return "userID不能为空";
	if(dto.getAddressID==null)
		return "addressID不能为空";
	if(dto.getUserName==null)
		return "comment不能为空";
	return "sucess";
}

This approach can meet our requirements, but if there are too many model fields, there will be a lot of judgments, which is not so convenient to maintain. Secondly, it increases the burden of the controller layer. Since we are in the era of spring 4, we should adapt to the use of annotations The following is the ratio change after using the annotation.

Annotated development:

Model:

public class User{
	@NotNull(message = "用户ID不能为空")
	private Long userID;
  
        @Valid  // 重点在这里,一定要加上@Valid,Demo2中的校验才会生效,否则不会生效
	@NotNull(message = "收货人地址id不能为空")
	private Long addressID;
	  
	@NotBlank(message = "备注不为空")
	private String userName;
}

Controller: Add @Valid annotation to the parameters to be verified in the controller layer method

@PostMapping("/createUser")
public String createOrders(@RequestBody @Valid User dto, BindingResult results) { // 重点在这里,需要加上@Valid才会去校验
	if (results.hasErrors()) 
		return results.getFieldError().getDefaultMessage();
	return "success";
}

In this way, we only need to add non-empty verification and corresponding prompts to the model field.

Three: @Valid commonly used annotations are as follows:

@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格式
需要注意每个注解对应的数据类型

For example:

//@NotNull:主要用在基本数据类型上(Int,Integer,Double,Date)

@NotNull(message = “年龄不能为空”)
private Integer age;

//@NotBlank:主要用在String字符串上面(String)

@NotBlank(message = “名字不能为空”)
private String name;

//@NotEmpty; 加了@NotEmpty注解的String类 ,Collection集合,Map ,数组,这些是不能为null或者长度为0的;(String ,Collection,Map的isEmpty()方法)

 

Guess you like

Origin blog.csdn.net/LOVE_Me__/article/details/107475785