1、@Validated和@Valid区别
- @Validated:可以用在类、方法和方法参数上。但是不能用在成员属性(字段)上
- @Valid:可以用在方法、构造函数、方法参数和成员属性(字段)上
2、引入依赖
Spring Boot 2.3 1 之前,只需要引用spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot 2.3 1 之后,spring-boot-starter-validation 已经不包括在了 spring-boot-starter-web 中,需要我们手动加上。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
3、示例代码
测试实体类
package com.test.entity;
import javax.validation.constraints.NotBlank;
import lombok.Data;
/**
* 测试实体类
*/
@Data
public class Test {
/**
* 账号
*/
@NotBlank(message = "登陆账号不能为空")
private String username;
/**
* 密码
*/
@NotBlank(message = "密码不能为空")
private String password;
}
Controller
package com.test.controller;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.test.entity.Test;
/**
* 测试参数校验
*/
@RestController
@RequestMapping("/test")
@Validated
public class TestController {
@RequestMapping(value = "getId", method = RequestMethod.GET)
public String getId(@NotNull Integer id) {
return "id: "+id;
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@Valid Test test) {
System.out.println(test.getUsername());
return "success";
}
}
4、验证结果
getId方法
save方法
大家会看到现在的错误提示非常不友好, 我们可以自定义异常返回错误消息
参考文章:SpringBoot注解校验validation自定义异常返回错误消息给前端_自定义校验注解前台如何弹窗提示-CSDN博客
@NotBlank注解String字段会报错_@notblank报错-CSDN博客
4、@Validated和@Valid使用情况测试
1.@Validated和@Valid同时使用--结果:验证成功
/**
* 1.@Validated和@Valid同时使用--结果:验证成功
*/
@RestController
@RequestMapping("/test")
@Validated
public class TestController {
@RequestMapping(value = "getId", method = RequestMethod.GET)
public String getId(@NotNull Integer id) {
return "id: "+id;
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@Valid Test test) {
System.out.println(test.getUsername());
return "success";
}
}
2.@Validated单独使用--结果:getId方法的参数id校验成功,save方法实体校验失效
/**
* 2.@Validated单独使用--结果:getId方法的参数id校验成功,save方法实体校验失效
*/
@RestController
@RequestMapping("/test")
@Validated
public class TestController {
@RequestMapping(value = "getId", method = RequestMethod.GET)
public String getId(@NotNull Integer id) {
return "id: "+id;
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(Test test) {
System.out.println(test.getUsername());
return "success";
}
}
3.@Valid单独使用--结果:getId方法的参数id校验失效,save方法实体校验成功
/**
* 3.@Valid单独使用--结果:getId方法的参数id校验失效,save方法实体校验成功
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "getId", method = RequestMethod.GET)
public String getId(@NotNull Integer id) {
return "id: "+id;
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@Valid Test test) {
System.out.println(test.getUsername());
return "success";
}
}
4.参数id也加上@Valid--结果:getId方法的参数id校验失效,save方法实体校验成功
/**
* 4.参数id也加上@Valid--结果:getId方法的参数id校验失效,save方法实体校验成功
*/
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "getId", method = RequestMethod.GET)
@Validated
public String getId(@Valid @NotNull Integer id) {
return "id: "+id;
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@Valid Test test) {
System.out.println(test.getUsername());
return "success";
}
}
5、总结
- 实体类参数:参数上必须加上@Valid,类上@Validated可加可不加;
- 非实体的参数:类上必须加上@Validated;
推荐都加上,如果类上 @Validated不加,万一后期改造添加新参数,可能会忘记注解@Validated