十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹springboot中如何實現(xiàn)@Valid注解對嵌套類型的校驗功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
10年積累的網(wǎng)站設(shè)計、網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有安義免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
@Valid注解可以實現(xiàn)數(shù)據(jù)的驗證,你可以定義實體,在實體的屬性上添加校驗規(guī)則,而在API接收數(shù)據(jù)時添加@valid關(guān)鍵字,這時你的實體將會開啟一個校驗的功能,具體的代碼如下,是最基本的應(yīng)用:
實體:
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 Listemployees = new ArrayList<>(); }
Restful接口:
@PostMapping() public ResponseinitialAccount( @ApiParam("客戶編號") @PathVariable String code, @ApiParam("賬期") @PathVariable YearMonth accountPeriod, @ApiParam("請求體") @Valid @RequestBody Request request) { ClientAccount result = clientAccountService.initialAccount( code, accountPeriod, request.getOperator(), request.getBody());{}
上面代碼中,我們?yōu)檎埱篌wRequest
添加了校驗,在測試時,如果你的DepartmnetDto.name為空字符時,當(dāng)出現(xiàn)400的異常,麗時異常消息是『部門名稱不能為空』,這對于我們來說是沒有問題的,也是符合我們要求的,下面看另一個場景。
需要驗證的實體是另一個實休的屬性
這種方式我們也需要會看到,一個大對象,如被封裝的其它小對象組成,比如部門下面有員工,這時如果需要驗證員工的有效性,需要如何實現(xiàn)呢?如果我們不修改源代碼,執(zhí)行結(jié)果是否定的, 它并不會校驗員工這個對象,而只針對第一層對象的屬性 。
我們將實體的員工屬性添加上@Valid即可實現(xiàn)對這個屬性的校驗
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 Listemployees = new ArrayList<>(); }
下面看一下驗證結(jié)果,我們的400錯誤就可以在單元測試下面正常輸出了!
@Test public void initialAccount_employee_name_empty() { Listemployees = new ArrayList<>(); employees.add(Employee.builder() .name("") .email("zzl@sina.com") .idNumber("110111198203182012") .build()); List departments = new ArrayList<>(); departments.add(DepartmentDto.builder() .name("部門") .description("技術(shù)部") .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("姓名不能為空"); }
結(jié)果如下,測試通過
如果是測試它是IsOk的話,由于用戶名為空,所以會出現(xiàn)錯誤提示
api.post() .uri("/v1/12345/2018-03") .body(BodyInserters.fromObject(request)) .exchange() .expectStatus().isOk();
可以看一下結(jié)果的提示信息
以上是“springboot中如何實現(xiàn)@Valid注解對嵌套類型的校驗功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!