文章目录
前言
刚开始的时候除了Controller层接受的对象我是直接通过一些 Spring 提供好的注解来实现校验比如@Valid、@NotNull 等等,在一些需要对参数做校验的其他地方我都是通过手动编程if
else判断的方式来实现。后面重构代码发现有更好的方式来满足我的需求,然后花了半天时间对这部分内容做了一个简单的总结,希望可以对不了解这部分知识的朋友有帮助。
基础知识和依赖
相关依赖
如果开发普通 Java 程序的的话,你需要可能需要像下面这样依赖:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
使用 Spring Boot 程序的话只需要spring-boot-starter-web 就够了,它的子依赖包含了我们所需要的东西。除了这个依赖,下面的演示还用到了 lombok ,所以不要忘记添加上相关依赖。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
实体类
下面这个是示例用到的实体类。
@Data
@AllArgsConstructor
@NoArgsConstructor
//hibernate对象懒加载,json序列化失败,因为懒加载这个对象属性只是一个代理对象,如果json直接当作一个存在的属性去序列化就会出现错误需要在实体类上加上,在controller 直接返回对象会出现这个问题
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
public class Person {
@NotNull(message = "classId 不能为空")
private String classId;
@Size(max = 33)
@NotNull(message = "name 不能为空")
private String name;
@Pattern(regexp = "((^Man$|^Woman$|^UGM$))", message = "sex 值不在可选范围")
@NotNull(message = "sex 不能为空")
private String sex;
@Pattern(regexp = "^1(3|4|5|7|8)\\d{9}$",message = "手机号码格式错误")
@NotBlank(message = "手机号码不能为空")
private String phone;
@Email(message = "email 格式不正确")
@NotNull(message = "email 不能为空")
private String email;
}
JSR提供的校验注解
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
Hibernate Validator提供的校验注解
@NotBlank(message =) 验证字符串非null,且长度必须大于0
@Email 被注释的元素必须是电子邮箱地址
@Length(min=,max=) 被注释的字符串的大小必须在指定的范围内
@NotEmpty 被注释的字符串的必须非空
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
验证请求体(RequestBody)
Controller:
我们在需要验证的参数上加上了@Valid注解,如果验证失败,它将抛出MethodArgumentNotValidException。默认情况下,Spring会将此异常转换为HTTP Status 400(错误请求)
@RestController
public class PersonController {
@Autowired
private PersonRepository repository;
@PostMapping("save")
public void savePerson(@RequestBody @Valid Person person) {
repository.save(person);
}
}
此时进行测试访问的结果
{
"id":1,
"name":"william",
"sex":"1", //sex没有填写Man
"phone":"17521080146",
"email":"643384967@qq.com"
}
输出结果
{
"timestamp": "2019-09-18T03:12:03.254+0000",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"Pattern.person.sex",
"Pattern.sex",
"Pattern.java.lang.String",
"Pattern"
],
"arguments": [
{
"codes": [
"person.sex",
"sex"
],
"arguments": null,
"defaultMessage": "sex",
"code": "sex"
},
[],
{
"arguments": null,
"defaultMessage": "((^Man$|^Woman$|^UGM$))",
"codes": [
"((^Man$|^Woman$|^UGM$))"
]
}
],
"defaultMessage": "sex 值不在可选范围",
"objectName": "person",
"field": "sex",
"rejectedValue": "1",
"bindingFailure": false,
"code": "Pattern"
}
],
"message": "Validation failed for object='person'. Error count: 1",
"path": "/facial-recognition-view-assembler-service/save"
}
后台出错信息
2019-09-18 11:15:43.187 WARN 9841 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for
argument [0] in public void
是不是感觉输出的信息很乱,这时候我们需要一个全局捕获异常
ExceptionHandler:
自定义异常处理器可以帮助我们捕获异常,并进行一些简单的处理。如果对于下面的处理异常的代码不太理解的话
@ControllerAdvice(assignableTypes = {PersonController.class})
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationExceptions(
MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
}
}
加完后的输出结果
{
"phone": "手机号码格式错误"
}
通过测试验证:
下面我通过 MockMvc 模拟请求 Controller 的方式来验证是否生效,当然你也可以通过 Postman 这种工具来验证。
我们试一下所有参数输入正确的情况。
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class PersonControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
public void should_get_person_correctly() throws Exception {
Person person = new Person();
person.setName("SnailClimb");
person.setSex("Man");
person.setClassId("82938390");
person.setEmail("Snailclimb@qq.com");
mockMvc.perform(post("/api/person")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(person)))
.andExpect(MockMvcResultMatchers.jsonPath("name").value("SnailClimb"))
.andExpect(MockMvcResultMatchers.jsonPath("classId").value("82938390"))
.andExpect(MockMvcResultMatchers.jsonPath("sex").value("Man"))
.andExpect(MockMvcResultMatchers.jsonPath("email").value("Snailclimb@qq.com"));
}
}
验证出现参数不合法的情况抛出异常并且可以正确被捕获。
@Test
public void should_check_person_value() throws Exception {
Person person = new Person();
person.setSex("Man22");
person.setClassId("82938390");
person.setEmail("SnailClimb");
mockMvc.perform(post("/api/person")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(person)))
.andExpect(MockMvcResultMatchers.jsonPath("sex").value("sex 值不在可选范围"))
.andExpect(MockMvcResultMatchers.jsonPath("name").value("name 不能为空"))
.andExpect(MockMvcResultMatchers.jsonPath("email").value("email 格式不正确"));
}
使用 Postman 验证结果如下
验证请求参数(Path Variables 和 Request Parameters)
Controller:类似Path Variables和Request Parameters一定一定不要忘记在类上加上 Validated 注解了,这个参数可以告诉 Spring 去校验方法参数。
@RestController
@Validated
public class PersonController {
@GetMapping("find/{id}")
public Person savePerson(@Valid @PathVariable("id") @Max(value = 2, message = "超过范围啦") Long id) {
Person one = repository.getOne(id);
return one;
}
@PutMapping("/person")
public String getPersonByName(@Valid @RequestParam("name") @Size(max = 6, message = "超过 name 的范围了") String name) {
return null;
}
报错的输出信息
{
"isSuccess": false,
"error": {
"code": null,
"message": "savePerson.id: 超过范围啦"
}
}
ExceptionHandler:
@ExceptionHandler(ConstraintViolationException.class)
ResponseEntity<String> handleConstraintViolationException(ConstraintViolationException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
输出结果
savePerson.id: 超过范围啦
通过测试验证:
@Test
public void should_check_param_value() throws Exception {
mockMvc.perform(get("/api/person/6")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isBadRequest())
.andExpect(content().string("getPersonByID.id: 超过 id 的范围了"));
}
@Test
public void should_check_param_value2() throws Exception {
mockMvc.perform(put("/api/person")
.param("name","snailclimbsnailclimb")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isBadRequest())
.andExpect(content().string("getPersonByName.name: 超过 name 的范围了"));
}
验证 Service 中的方法
我们还可以验证任何Spring组件的输入,而不是验证控制器级别的输入,我们可以使用@Validated和@Valid注释的组合来实现这一需求。
一定一定不要忘记在类上加上 Validated 注解了,这个参数可以告诉 Spring 去校验方法参数。
@Service
@Validated
public class PersonService {
public void validatePerson(@Valid Person person){
// do something
}
}
通过测试验证
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class PersonServiceTest {
@Autowired
private PersonService service;
@Test(expected = ConstraintViolationException.class)
public void should_throw_exception_when_person_is_not_valid() {
Person person = new Person();
person.setSex("Man22");
person.setClassId("82938390");
person.setEmail("SnailClimb");
service.validatePerson(person);
}
}
Validator 编程方式手动进行参数验证
某些场景下可能会需要我们手动校验并获得校验结果。
@Test
public void check_person_manually() {
StringBuffer stringBuffer = new StringBuffer();
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Person person = new Person();
person.setSex("Man22");
person.setClassId("82938390");
person.setEmail("SnailClimb");
//返回值是不符合注解规范的信息
//比如:ConstraintViolationImpl{interpolatedMessage='不能为空', propertyPath=visualId, rootBeanClass=class com.wbie.wdpro.frvas.model.checkQuest, messageTemplate='{javax.validation.constraints.NotEmpty.message}'}
Set<ConstraintViolation<Person>> violations = validator.validate(person);
constraintViolations.forEach(item -> {
//这行代码可以参考下面的图片
String annotationType = item.getConstraintDescriptor().getAnnotation().annotationType().getName();
if (stringBuffer.length() > 0) {
stringBuffer.append(", ");
}
if (annotationType.equals(ANNOTATION_NOTNULL)) {
stringBuffer.append(item.getPropertyPath().toString() + ": should be not empty");
}
});
return stringBuffer.toString();
}
上面我们是通过 Validator 工厂类获得的 Validator 示例,当然你也可以通过 @Autowired 直接注入的方式。但是在非 Spring Component 类中使用这种方式的话,只能通过工厂类来获得 Validator。
@Autowired
Validator validate
自定义 Validator
如果自带的校验注解无法满足你的需求的话,你还可以自定义实现注解。比如我们的Person类多了一个 region 字段,region 字段只能是China、China-Taiwan、China-HongKong这三个中的一个。
第一步你需要创建一个注解:
@Target({FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = RegionValidator.class)
@Documented
public @interface Region {
String message() default "Region 值不在可选范围内";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
第二步你需要实现 ConstraintValidator接口,并重写isValid 方法
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
public class RegionValidator implements ConstraintValidator<Region, String> {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
HashSet<Object> regions = new HashSet<>();
regions.add("China");
regions.add("China-Taiwan");
regions.add("China-HongKong");
return regions.contains(value);
}
}
现在你就可以使用这个注解:
@Region
private String region;
使用验证组
很多时候我们需要使用到验证组,这样说可能不太清楚,说简单点就是对对象操作的不同方法有不同的验证规则,示例如下。
先创建两个接口:
public interface AddPersonGroup {
}
public interface DeletePersonGroup {
}
我们可以这样去使用验证组
@NotNull(groups = DeletePersonGroup.class)
@Null(groups = AddPersonGroup.class)
private String group;
@Service
@Validated
public class PersonService {
public void validatePerson(@Valid Person person) {
// do something
}
@Validated(AddPersonGroup.class)
public void validatePersonGroupForAdd(@Valid Person person) {
// do something
}
@Validated(DeletePersonGroup.class)
public void validatePersonGroupForDelete(@Valid Person person) {
// do something
}
}
通过测试验证:
@Test(expected = ConstraintViolationException.class)
public void should_check_person_with_groups() {
Person person = new Person();
person.setSex("Man22");
person.setClassId("82938390");
person.setEmail("SnailClimb");
person.setGroup("group1");
service.validatePersonGroupForAdd(person);
}
@Test(expected = ConstraintViolationException.class)
public void should_check_person_with_groups2() {
Person person = new Person();
person.setSex("Man22");
person.setClassId("82938390");
person.setEmail("SnailClimb");
service.validatePersonGroupForDelete(person);
}
使用验证组这种方式的时候一定要小心,这是一种反模式,还会造成代码逻辑性变差。