SpringBoot参数校验@Valid
IT利刃出鞘 人气:0简介
说明
本文用示例说明SpringBoot的@Valid的用法。
依赖
<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> </dependency>
代码
Controller
package com.example.demo.simple.controller; import com.example.demo.simple.entity.User; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; import java.util.List; @Api(tags = "不分组") @RestController @RequestMapping("valid") public class ValidController { @ApiOperation("正常用法") @GetMapping("normal") public User normal(@Valid User user) { return user; } @ApiOperation("获得BindingResult") @GetMapping("bindingResult") public User bindingResult(@Valid User user, BindingResult bindingResult) { if (bindingResult.hasErrors()){ List<ObjectError> list = bindingResult.getAllErrors(); for (ObjectError objectError : list) { System.out.println(objectError.getDefaultMessage()); } //System.out.println(bindingResult.getFieldError().getDefaultMessage()); } return user; } }
Entity
User类
package com.example.demo.simple.entity; import lombok.Data; import javax.validation.Valid; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import java.util.List; @Data public class User { @NotBlank(message = "名字不能为空") private String name; private Integer age; @NotBlank(message = "密码不能为空") private String password; @NotEmpty(message = "分数不能为空") private List<Integer> scoreArray; @Valid @NotNull(message = "账户不能为null") private Account account; }
Account类
package com.example.demo.simple.entity; import lombok.Data; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotEmpty; import java.io.Serializable; @Data public class Account { @NotBlank(message = "电话号码不能为空") private String phoneNumber; private String[] emails; }
测试
本文为了展示后端错误,直接用postman来进行请求。
实际上,如果用了knife4j这个接口工具,它能自动识别是否为空的注解,并在前端进行控制。如果不能为空,则前端直接显示为红色的框。
测试1:缺少字段
postman访问:http://localhost:8080/valid/normal
postman访问结果:
后端结果:
2021-12-22 15:32:20.032 WARN 94176 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errors
Field error in object 'user' on field 'account': rejected value [null]; codes [NotNull.user.account,NotNull.account,NotNull.com.example.demo.simple.entity.Account,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.account,account]; arguments []; default message [account]]; default message [账户不能为null]
Field error in object 'user' on field 'password': rejected value [null]; codes [NotBlank.user.password,NotBlank.password,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.password,password]; arguments []; default message [password]]; default message [密码不能为空]
Field error in object 'user' on field 'scoreArray': rejected value [null]; codes [NotEmpty.user.scoreArray,NotEmpty.scoreArray,NotEmpty.java.util.List,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.scoreArray,scoreArray]; arguments []; default message [scoreArray]]; default message [分数不能为空]]
测试2:不缺少字段
postman访问:http://localhost:8080/valid/normal
postman结果:
测试3:缺少字段,后端获取BindResult
postman访问:http://localhost:8080/valid/bindingResult
postman结果:
后端结果
账户不能为null
密码不能为空
分数不能为空
可以看到,没有报错。
加载全部内容