这篇文章主要介绍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注解对嵌套类型的校验功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。