使用javax.validation.constraints包的注解实现校验

一、添加maven依赖包

 <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>1.1.0.Final</version>
    </dependency>

二、在校验字段上添加校验注解

class Profile{
        @NotNull(message = "字段值不能为空")
        private String name;
        @NotNull
        private String sex;
        @Max(value = 20,message = "最大长度为20")
        private String address;
        @NotNull
        @Size(max=10,min=5,message = "字段长度要在5-10之间")
        private String fileName;
        @Pattern(regexp = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$",message = "不满足邮箱正则表达式")
        private String email;
        @AssertTrue(message = "字段为true才能通过")
        private boolean isSave;
        @Future(message = "时间在当前时间之后才可以通过")
        private Date date;

}

三、在Controller层使用@Validated进行验证

@RequestMapping("file/upload")
 public void upload(@RequestPart("files") MultipartFile files, @Validated Profile profile, Errors error) throws IOException {
       if(error.hasErrors()){
           return;
       }
       files.transferTo(new File(files.getOriginalFilename()));保存文件
}

欢迎关注南阁公众号


南阁

javax.validation.constraints.NotEmpty是一个用于验证字段不为空的约束注解。它可以应用于String、Collection、Map和数组等类型的字段上。被@NotEmpty注解修饰的字段不能为null且不能为空。\[3\]在引用\[2\]中的示例中,@NotEmpty(message = "ID不能为空")应用于List<String>类型的userIds字段,表示该字段不能为null且不能为空。因此,在创建的类对象中,如果userIds字段为null或者为空集合,就会出现"执行报错:No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.Long'. Check configuration for 'id'"的错误提示。\[1\] #### 引用[.reference_title] - *1* [No validator could be found for constraint ‘javax.validation.constraints.NotEmpty‘...报错解决](https://blog.csdn.net/qq_40816795/article/details/127109993)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [validation校验常用校验注解@NotEmpty,@NotBlank,@NotNull等注解](https://blog.csdn.net/Ying_ph/article/details/125908693)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [javax.validation.constraints中@NotEmpty,@NotNull,@NotBlank的区别](https://blog.csdn.net/m0_51176516/article/details/117456403)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值