这篇文章主要介绍“@Valid注解怎么规范使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“@Valid注解怎么规范使用”文章能帮助大家解决问题。
注解 | 描述 |
@AssertFalse | 带注解的元素必须为false,支持boolean/Boolean |
@AssertTrue | 带注解的元素必须为true,支持boolean/Boolean |
@DecimalMax | 带注解的元素必须是一个数字,其值必须小于等于指定的最大值 |
@DecimalMin | 带注解的元素必须是一个数字,其值必须大于等于指定的最小值 |
@Digits | 带注解的元素必须是一个可接受范围内的数字 |
@Future | 带注解的元素必须是将来的某个时刻、日期或时间 |
@Max | 带注解的元素必须是一个数字,其值必须小于等于指定的最大值 |
@Min | 带注解的元素必须是一个数字,其值必须大于等于指定的最小值 |
@NotNull | 带注解的元素不能是Null |
@Null | 带注解的元素必须是Null |
@Past | 带注解的元素必须是过去的某个时刻、日期或时间 |
@Pattern | 带注解的元素必须符合指定的正则表达式 |
@Size | 带注解的元素必须大于等于指定的最小值,小于等于指定的最大值 |
带注解的元素必须是格式良好的电子邮箱地址 | |
@NotEmpty | 带注解的元素不能是空,String类型不能为null,Array、Map不能为空,切size/length大于0 |
@NotBlank | 字符串不能为空、空字符串、全空格 |
@URL | 字符串必须是一个URL |
对于一个用户的注册操作(Post请求),往往涉及到账号(username)、密码(password)的Post提交:
//用户发送POST请求创建新的用户
@PostMapping
public 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请求创建新的用户
@PostMapping
public 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将错误信息作为日志打印到后台
@PostMapping
public 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注解怎么规范使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。