springboot @Valid注解对嵌套类型的校验功能
2018-05-25 14:52
1011 查看
@Valid注解可以实现数据的验证,你可以定义实体,在实体的属性上添加校验规则,而在API接收数据时添加@valid关键字,这时你的实体将会开启一个校验的功能,具体的代码如下,是最基本的应用:
实体:
public class DepartmentDto { @ApiModelProperty("id") private String id; @ApiModelProperty("上级Id") private String parentId; @ApiModelProperty("编号") @NotBlank(message = "部门编号不能为空。") private String code; @ApiModelProperty("名称") @NotBlank(message = "部门名称不能为空。") private String name; @ApiModelProperty("员工集合") @Builder.Default private List<Employee> employees = new ArrayList<>(); }
Restful接口:
@PostMapping() public Response<ClientAccount> initialAccount( @ApiParam("客户编号") @PathVariable String code, @ApiParam("账期") @PathVariable YearMonth accountPeriod, @ApiParam("请求体") @Valid @RequestBody Request<DepartmentDto> request) { ClientAccount result = clientAccountService.initialAccount( code, accountPeriod, request.getOperator(), request.getBody());{}
上面代码中,我们为请求体
Request<DepartmentDto>添加了校验,在测试时,如果你的DepartmnetDto.name为空字符时,当出现400的异常,丽时异常消息是『部门名称不能为空』,这对于我们来说是没有问题的,也是符合我们要求的,下面看另一个场景。
需要验证的实体是另一个实休的属性
这种方式我们也需要会看到,一个大对象,如被封装的其它小对象组成,比如部门下面有员工,这时如果需要验证员工的有效性,需要如何实现呢?如果我们不修改源代码,执行结果是否定的, 它并不会校验员工这个对象,而只针对第一层对象的属性 。
我们将实体的员工属性添加上@Valid即可实现对这个属性的校验
public class DepartmentDto { @ApiModelProperty("id") private String id; @ApiModelProperty("上级Id") private String parentId; @ApiModelProperty("编号") @NotBlank(message = "部门编号不能为空。") private String code; @ApiModelProperty("名称") @NotBlank(message = "部门名称不能为空。") private String name; @Valid @ApiModelProperty("员工集合") @Builder.Default private List<Employee> employees = new ArrayList<>(); }
下面看一下验证结果,我们的400错误就可以在单元测试下面正常输出了!
@Test public void initialAccount_employee_name_empty() { List<Employee> employees = new ArrayList<>(); employees.add(Employee.builder() .name("") .email("zzl@sina.com") .idNumber("110111198203182012") .build()); List<DepartmentDto> departments = new ArrayList<>(); departments.add(DepartmentDto.builder() .name("部门") .description("技术部") .salaryType(SalaryType.ResearchAndDevelopmentCosts) .employees(employees) .build()); ClientAccountDto clientAccountDto = ClientAccountDto.builder() .name("客户") .departments(departments) .build(); Request<ClientAccountDto> request = buildRequest(clientAccountDto); api.post() .uri("/v1/12345/2018-03") .body(BodyInserters.fromObject(request)) .exchange() .expectStatus().isEqualTo(400) .expectBody() .jsonPath("$.errors[0].message").isEqualTo("姓名不能为空"); }
结果如下,测试通过
如果是测试它是IsOk的话,由于用户名为空,所以会出现错误提示
api.post() .uri("/v1/12345/2018-03") .body(BodyInserters.fromObject(request)) .exchange() .expectStatus().isOk();
可以看一下结果的提示信息
总结
以上所述是小编给大家介绍的springboot @Valid注解对嵌套类型的校验,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- spring boot 自定义校验注解
- Spring boot 注解校验
- Spring-boot的@Valid注解
- 使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验
- Spring Boot Validator校验注解
- Kotlin & Spring boot 使用@Valid校验无效解决方法
- SpringBoot 中使用声明式注解简化参数校验
- spring boot注解实现参数校验
- springboot @valid 表单校验
- SpringBoot中@EnableAutoConfiguration注解用法收集
- spring boot @Value注解读取配置
- SpringBoot 注解事务声明式事务的方式
- SpringBoot 整合Mail发送功能问题与解决
- 快速构建Spring boot项目(功能是检测访问页面的设备)
- springboot 常用注解
- Spring Boot集成JPA的Column注解命名字段无效的问题
- Spring Boot项目使用参数校验
- SpringBoot实践之(十五)---自定义注解Annotation的使用
- SpringBoot整合Mybatis完成增删改查功能
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数