[SpringBoot] Common annotations, @NotNull, @NotBlank, @Valid annotations

foreword

When the front end passes parameters to the back end, the back end should make some judgments, such as non-empty. And SpringBoot provides related explanations.

Common Notes

annotation illustrate
@Null Can only be null
@NotNull must not be null
@Min(value) Must be a number not less than the specified value
@Max(value) Must be a number not greater than the specified value
@NotBlank Verify that the element value of the annotation is not empty (not null, the length is 0 after removing the first space), unlike @NotEmpty, @NotBlank is only applied to strings and the spaces of strings will be removed when comparing
@NotEmpty Verify that the element value of the annotation is not null and not empty (the length of the string is not 0, the size of the collection is not 0)
@AssertFalse must be false
@AssertTrue must be true
@DecimalMax(value) Must be a number not greater than the specified value
@DecimalMin(value) Must be a number not less than the specified value
@Digits(integer,fraction) Must be a decimal number, and the number of digits in the integer part cannot exceed integer, and the number of digits in the fractional part cannot exceed fraction
@Future Must be a future date
@Past Must be a date in the past
@Pattern(value) must match the specified regular expression
@Size(max,min) The character length must be between min and max
@Pattern(value) must match the specified regular expression
@Email Verify that the element value of the annotation is Email, and you can also specify a custom email format through regular expressions and flags

the case

Entity class:


public class OrderSync {
    
    
	private static final long serialVersionUID = 1L;
	/**
	 * 用户Id
	 */
	@ApiModelProperty(value = "用户id")
	@NotEmpty(message = "用户id不能为空")
	private String userId;
	/**
	 * 购买数量
	 */
	@NotNull(message = "购买数量不能为空")
	private Integer quantity;
	/**
	 * 支付时间
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@NotEmpty(message = "支付时间不能为空")
	private LocalDateTime paymentTime;

	/**
	 * 订单状态1、待发货 2、待收货 3、确认收货/已完成 5、已关闭
	 */
	@Excel(name = "订单状态", readConverterExp = "1=待发货,2=待收货,3=确认收货/已完成 ,5=已关闭")
	@NotEmpty(message = "订单状态不能为空")
	private String orderStatus;

}

@valid annotation

The @valid annotation is mainly used for data validation and can be defined in two places.
1. Define the attributes in the entity class and add different annotations to complete different verification rules.
2. Defined in the interface class, add the @Valid annotation before receiving the parameters to enable the validation function of the entity class.

the case

1. Add @Valid related annotations to the entity class

For a single entity class, you only need to add corresponding annotations:

public class User{
    
    
    @NotBlank(message = "姓名不为空")
    private String username;
    @NotBlank(message = "密码不为空")
    private String password;
}

But if it is a nested entity object, you need to add @Valid annotation on the outermost property

public class User{
    
    
    @NotBlank(message = "姓名不为空")
    private String username;
    @NotBlank(message = "密码不为空")
    private String password;
    //嵌套必须加@Valid,否则嵌套中的验证不生效
    @Valid
    @NotNull(message = "用户信息不能为空")
    private UserInfo userInfo;
}

2. Define and add @Valid related annotations in the interface class

	@PostMapping(value = "/app/OrderSync")
	public OrderSyncResp orderSync(@Valid @RequestBody OrderSyncReq req) {
    
    
		log.info("OrderSync param:[{}]", JSON.toJSONString(req));
		MiGuOrderSyncResp resp = miGuOrderSyncService.orderSync(req);
		log.info("OrderSync resp:[{}]", JSON.toJSONString(resp));
		return resp;
	}

Guess you like

Origin blog.csdn.net/someday____/article/details/128395263