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

这篇文章主要介绍了springboot~@Valid注解对嵌套类型的校验,主要介绍 @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 employees = new ArrayList<>(); }

Restful接口:

 @PostMapping() public Response initialAccount( @ApiParam("客户编号") @PathVariable String code, @ApiParam("账期") @PathVariable YearMonth accountPeriod, @ApiParam("请求体") @Valid @RequestBody Request request) { ClientAccount result = clientAccountService.initialAccount( code, accountPeriod, request.getOperator(), request.getBody());{}

上面代码中,我们为请求体Request添加了校验,在测试时,如果你的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 employees = new ArrayList<>(); }

下面看一下验证结果,我们的400错误就可以在单元测试下面正常输出了!

 @Test public void initialAccount_employee_name_empty() { List employees = new ArrayList<>(); employees.add(Employee.builder() .name("") .email("zzl@sina.com") .idNumber("110111198203182012") .build()); List departments = new ArrayList<>(); departments.add(DepartmentDto.builder() .name("部门") .description("技术部") .salaryType(SalaryType.ResearchAndDevelopmentCosts) .employees(employees) .build()); ClientAccountDto clientAccountDto = ClientAccountDto.builder() .name("客户") .departments(departments) .build(); Request 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注解对嵌套类型的校验功能的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java