The @Valid annotation can realize data verification. You can define entities and add verification rules to the entity's properties. When the API receives data, your entity will enable a verification function. The specific code is as follows, which is the most basic application:
entity:
public class DepartmentDto { @ApiModelProperty("id") private String id; @ApiModelProperty("Superior Id") private String parentId; @ApiModelProperty("Number") @NotBlank(message = "Department number cannot be empty.") private String code; @ApiModelProperty("Number") @NotBlank(message = "Department name cannot be empty.") private String name; @ApiModelProperty("Employee> employees = new ArrayList<>();}Restful interface:
@PostMapping() public Response<ClientAccount> initialAccount( @ApiParam("Customer Number") @PathVariable String code, @ApiParam("Registration Period") @PathVariable YearMonth accountPeriod, @ApiParam("Request Body") @Valid @RequestBody Request<DepartmentDto> request) { ClientAccount result = clientAccountService.initialAccount(code, accountPeriod, request.getOperator(), request.getBody());{} In the above code, we added a check for the request body Request<DepartmentDto> . During the test, if your DepartmentnetDto.name is empty, when an exception of 400 appears, the Lishi exception message is "the department name cannot be empty", which is no problem for us and it also meets our requirements. Let's see another scenario below.
The entity that needs to be verified is another real property
We also need to see that a large object, such as other small objects encapsulated, is composed of, for example, employees under the department. If you need to verify the effectiveness of the employees, how should you achieve it? If we do not modify the source code and the execution result is negative, it will not verify the employee object, but will only target the properties of the first layer object.
We can verify this attribute by adding @Valid to @Valid
public class DepartmentDto { @ApiModelProperty("id") private String id; @ApiModelProperty("Superior Id") private String parentId; @ApiModelProperty("Number") @NotBlank(message = "Department number cannot be empty.") private String code; @ApiModelProperty("Number") @NotBlank(message = "Department name cannot be empty.") private String name; @Valid @ApiModelProperty("Employee> employees = new ArrayList<>();}Let’s take a look at the verification results below. Our 400 errors can be output normally under the unit test!
@Test public void initialAccount_employee_name_empty() { List<Employee> employees = new ArrayList<>(); employees.add(Employee.builder() .name("") .email("[email protected]") .idNumber("110111198203182012") .build()); List<DepartmentDto> departments = new ArrayList<>(); departments.add(DepartmentDto.builder() .name("Department") .description("Technical Department") .salaryType(SalaryType.ResearchAndDevelopmentCosts) .employees(employees) .build()); ClientAccountDto clientAccountDto = ClientAccountDto.builder() .name("Customer") .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("Name cannot be empty"); }The results are as follows, the test passes
If it is tested, it is IsOk, since the username is empty, an error message will appear
api.post() .uri("/v1/12345/2018-03") .body(BodyInserters.fromObject(request)) .exchange() .expectStatus().isOk();You can check the results prompt information
Summarize
The above is the verification of the nested type by the springboot @Valid annotation introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!