Spring注解式参数校验

178 阅读1分钟
@Data
@ToString
public class xxxDto {

  @NotNull(message = "不能为null")
  private InvoiceType type;

  @Size(min = 2, max = 100, message = "至少包含2个字符,最多包含100个字符")
  private String ent_name;

  @Pattern(regexp = "^([0-9ABCDEFGHJKLMNPQRTUWXY]{2})(\\d{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-9ABCDEFGHJKLMNPQRTUWXY])$", message = "格式不正确")
  private String number;
}

@NotNull、@NotBlank、@NotEmpty的区别:
1.String name = null;

@NotNull: false
@NotEmpty:false 
@NotBlank:false 

2.String name = "";

@NotNull:true
@NotEmpty: false
@NotBlank: false

3.String name = " ";

@NotNull: true
@NotEmpty: true
@NotBlank: false

4.String name = "Great answer!";

@NotNull: true
@NotEmpty:true
@NotBlank:true