千家信息网

@Valid注解怎么规范使用

发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,这篇文章主要介绍"@Valid注解怎么规范使用"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"@Valid注解怎么规范使用"文章能帮助大家解决问题。@Valid
千家信息网最后更新 2024年10月19日@Valid注解怎么规范使用

这篇文章主要介绍"@Valid注解怎么规范使用"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"@Valid注解怎么规范使用"文章能帮助大家解决问题。

@Valid注解大全及用法规范

注解描述
@AssertFalse带注解的元素必须为false,支持boolean/Boolean
@AssertTrue带注解的元素必须为true,支持boolean/Boolean
@DecimalMax带注解的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin带注解的元素必须是一个数字,其值必须大于等于指定的最小值
@Digits带注解的元素必须是一个可接受范围内的数字
@Future带注解的元素必须是将来的某个时刻、日期或时间
@Max带注解的元素必须是一个数字,其值必须小于等于指定的最大值
@Min带注解的元素必须是一个数字,其值必须大于等于指定的最小值
@NotNull带注解的元素不能是Null
@Null带注解的元素必须是Null
@Past带注解的元素必须是过去的某个时刻、日期或时间
@Pattern带注解的元素必须符合指定的正则表达式
@Size带注解的元素必须大于等于指定的最小值,小于等于指定的最大值
@Email带注解的元素必须是格式良好的电子邮箱地址
@NotEmpty带注解的元素不能是空,String类型不能为null,Array、Map不能为空,切size/length大于0
@NotBlank字符串不能为空、空字符串、全空格
@URL字符串必须是一个URL

@Valid注解规范用户请求的参数

业务场景

对于一个用户的注册操作(Post请求),往往涉及到账号(username)、密码(password)的Post提交:

//用户发送POST请求创建新的用户@PostMappingpublic User create(@RequestBody User user){    /**        一些数据持久化操作,如:写入数据库    **/    //打印用户提交的信息    System.out.println(user.getId());    System.out.println(user.getUsername());    System.out.println(user.getPassword());    System.out.println(user.getBirthday());    return user;}

业务出现的问题

但用户往往会不小心提交了空的密码来注册,这是不允许的,因此我们往往需要对用户提交的密码信息进行空判断,常见的方法是直接进行if语句的空判断:

//用户发送POST请求创建新的用户@PostMappingpublic User create(@RequestBody User user){    if( StringUtils.isBlank(user.getPassword())){        //用户输入密码为空,进行异常处理    }    /**        一些数据持久化操作,如:写入数据库    **/    //打印用户提交的信息    System.out.println(user.getId());    System.out.println(user.getUsername());    System.out.println(user.getPassword());    System.out.println(user.getBirthday());    return user;}

以上方法看似行得通,但一旦Post的方法变多,则需要对每个Post请求都进行一次if判断是否为空,代码变得冗余,而且一旦修改一个地方,所有if语句都需要修改,可维护性就变得很差。

优化的解决方案

那么,有没有一种方法可以一劳永逸、既没有大量代码冗余,可维护性又好呢?这时 javax.validation包下的@Valid注解就派上用场了。

1.首先,我们在实体类User.java中的密码(password)属性加上@NotBlank注解(hibernate.validator.constraints包)

import org.hibernate.validator.constraints.NotBlank;public class User {    public interface UserSimpleView{}    public interface UserDetailView extends UserSimpleView{}    private String username;    //给该属性加入NotBlank非空的约束    @NotBlank    private String password;    private String id;    private Date birthday;    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    @JsonView(UserSimpleView.class)    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    @JsonView(UserSimpleView.class)    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    @JsonView(UserDetailView.class)    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

2.我们在Controller类的Post方法的参数中加入@Valid注解,并使用BindingResult将错误信息作为日志打印到后台

@PostMappingpublic User create(@Valid @RequestBody User user,                       BindingResult errors){    if (errors.hasErrors()){        //异常处理        errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));    }    user.setId("1");    System.out.println(user.getId());    System.out.println(user.getUsername());    System.out.println(user.getPassword());    System.out.println(user.getBirthday());    return user;}

3.这时,当我们向服务器Post提交空的密码信息时,后台会打印出错误信息:

may not be empty

关于"@Valid注解怎么规范使用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

0