springboot @Valid注解对嵌套类型的校验功能

时间:2019-04-15
本文章向大家介绍springboot @Valid注解对嵌套类型的校验功能,主要包括springboot @Valid注解对嵌套类型的校验功能使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

@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注解对嵌套类型的校验,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!