当前位置:网站首页>@valid,@Validated 的学习笔记
@valid,@Validated 的学习笔记
2022-04-23 10:53:00 【Mu_Mu是一只小白】
1.简介
- @Validated:可以用在类型、方法和方法参数上。但是不能用在成员属性(字段)上,不支持嵌套检测
- @Valid:可以用在方法、构造函数、方法参数和成员属性(字段)上,支持嵌套检测
2.引入maven
springboot 2.3.0 以后不会自动引入jar包,所以要添加以下maven,2.3以前则不需要引入maven包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
3.配合使用注解
@Null 限制只能为null
@NotNull 限制必须不为null
@AssertFalse 限制必须为false
@AssertTrue 限制必须为true
@DecimalMax(value) 限制必须为一个不大于指定值的数字
@DecimalMin(value) 限制必须为一个不小于指定值的数字
@Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction
@Future 限制必须是一个将来的日期
@Max(value) 限制必须为一个不大于指定值的数字
@Min(value) 限制必须为一个不小于指定值的数字
@Past 限制必须是一个过去的日期
@Pattern(value) 限制必须符合指定的正则表达式
@Size(max,min) 限制字符长度必须在min到max之间
@Past 验证注解的元素值(日期类型)比当前时间早
@NotEmpty 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)
@NotBlank 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格
@Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
4.例子
4.1 model
@Valid 定义schoole属性是为了实现嵌套验证,没有这个注解无法校验school类内部需要校验的属性。
public class UserVO {
@NotNull(message = "id不能为空。" ,groups = {Insert.class})
private Integer id;
@NotBlank(message = "name不能为空。",groups = {Update.class})
private String name;
@NotNull(message = "schoole不能为空。")
@Valid
private Schoole schoole;
@Min(value = 1,message = "age不能小于1")
@Max(value = 130,message = "age不能大于130")
private int age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Schoole getSchoole() {
return schoole;
}
public void setSchoole(Schoole schoole) {
this.schoole = schoole;
}
public class Schoole {
@NotBlank(message = "name不能为空。",groups = {Update.class})
private String name;
@NotNull(message = "id不能为空")
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
4.3分组接口
实现Default 接口不然@Validated({Update.class}) 使用Update分组时,未定义分组的默认校验属性也不会生效(比如校验schoole的 @NotNull(message = “schoole不能为空。”))
import javax.validation.groups.Default;
public interface Insert extends Default {
}
public interface Update extends Default {
}
4.4controller
@Validated 标明group 时(Update.class)只有需要校验属性上校验注解含有该接口(Update.class)才生效,如果group (Update.class)实现了Default接口那么需要校验属性上的校验注解未定义任何group 时也会生效。
@RestController
@RequestMapping("/valid")
public class TestValidController {
private static final Logger LOG = LoggerFactory.getLogger(TestValidController.class);
@RequestMapping("/test")
public void testValid(@Validated({Update.class}) UserVO userVO){
LOG.info("userVo:"+userVO);
}
@PostMapping("/test2")
public void testValid2(@Validated() @RequestBody UserVO userVO){
LOG.info("userVo:"+userVO);
}
}
4.5使用全局异常拦截器拦截参数校验异常
MethodArgumentNotValidException异常由@RequestBody 修饰的参数未校验过抛出,其他未校验通过抛出异常BindException。
@RestControllerAdvice
public class GlobalExceptionHandler {
private Logger LOG= LoggerFactory.getLogger(GlobalExceptionHandler.class);
//BindException 校验参数不满足条件抛出
@ExceptionHandler(BindException.class)
public Object handleValidException(BindException e) {
return ResponseUtil.fail(400,e.getBindingResult().getFieldError().getDefaultMessage());
}
//MethodArgumentNotValidException @RequestBody 修饰的参数未校验过抛出
@ExceptionHandler(MethodArgumentNotValidException.class)
public Object handleValidException2(MethodArgumentNotValidException e) {
return ResponseUtil.fail(400,e.getBindingResult().getFieldError().getDefaultMessage());
}
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseUtil handleRuntimeException(RuntimeException ex) {
int code = 500;
String message = ex.getMessage();
ex.printStackTrace();
LOG.error(ex.getMessage());
return ResponseUtil.fail(code,message);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseUtil handleException(Exception ex) {
int code = 500;
String message = ex.getMessage();
ex.printStackTrace();
LOG.error(ex.getMessage());
return ResponseUtil.fail(code,message);
}
}
版权声明
本文为[Mu_Mu是一只小白]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lin1214000999/article/details/124282745
边栏推荐
- Visual common drawing (IV) histogram
- 242. Valid Letter ectopic words (hash table)
- Construction and traversal of binary tree
- 期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
- Linked list intersection (linked list)
- What about Jerry's stack overflow? [chapter]
- MySql常用语句
- Yarn resource scheduler
- The difference between restful and soap
- Visual solutions to common problems (VIII) mathematical formulas
猜你喜欢
ID number verification system based on visual structure - Raspberry implementation
Windows installs redis and sets the redis service to start automatically
Reading integrity monitoring techniques for vision navigation systems - 3 background
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
【leetcode】102. Sequence traversal of binary tree
得到知识服务app原型设计比较与实践
Intuitive understanding entropy
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
Let the LAN group use the remote device
VIM + ctags + cscope development environment construction guide
随机推荐
SSH利用私钥无密钥连接服务器踩坑实录
SQL Server 递归查询上下级
Resolution and size of mainstream mobile phones
Deploy jar package
Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
Esp32 learning - use and configuration of GPIO
App. In wechat applet JS files, components, APIs
图像处理——噪声小记
第六站神京门户-------手机号码的转换
How does the swagger2 interface import postman
【leetcode】102. Sequence traversal of binary tree
全栈交叉编译X86完成过程经验分享
How to bind a process to a specified CPU
我的创作纪念日
Chapter 120 SQL function round
JVM - common parameters
VIM + ctags + cscope development environment construction guide
Simple thoughts on the design of a microblog database
Idea - indexing or scanning files to index every time you start
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis