{"id":6307,"date":"2022-07-07T13:16:18","date_gmt":"2022-07-07T05:16:18","guid":{"rendered":"http:\/\/123.57.164.21\/?p=6307"},"modified":"2022-07-07T13:16:18","modified_gmt":"2022-07-07T05:16:18","slug":"springboot%e4%b8%advalidated%e6%b3%a8%e8%a7%a3%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=6307","title":{"rendered":"Springboot\u4e2d@Validated\u6ce8\u89e3\u4f7f\u7528"},"content":{"rendered":"\n<p>Spring-boot\u4e2d\u53ef\u4ee5\u7528@validated\u6765\u6821\u9a8c\u6570\u636e\uff0c\u5982\u679c\u6570\u636e\u5f02\u5e38\u5219\u4f1a\u7edf\u4e00\u629b\u51fa\u5f02\u5e38\uff0c\u65b9\u4fbf\u5f02\u5e38\u4e2d\u5fc3\u7edf\u4e00\u5904\u7406\u3002<\/p>\n\n\n\n<p><strong>\u6dfb\u52a0\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u7528Spring-boot-starter-web \u5c31\u4e0d\u9700\u8981\u91cd\u590d\u6dfb\u52a0 spring-boot-starter-validation \u4f9d\u8d56\u4e86\uff0c\u4f46\u5982\u679c\u6ca1\u7528 web \u4f9d\u8d56\u65f6\u5019\u60f3\u8981\u5b9e\u73b0 Bean \u9a8c\u8bc1\uff0c\u5219\u53ea\u8981\u5355\u52a0\u5165 spring-boot-starter-validation \u4f9d\u8d56\u5373\u53ef\u3002<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;dependency>\n    &lt;groupId>org.springframework.boot&lt;\/groupId>\n    &lt;artifactId>spring-boot-starter-validation&lt;\/artifactId>\n&lt;\/dependency><\/pre>\n\n\n\n<p><strong>\u5728bean\u4e0a\u58f0\u660e\u9700\u8981\u88ab\u6821\u9a8c\u7684\u5b57\u6bb5<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * @author Boss\n *\/\n@ApiModel(\"\u6ce8\u518cDTO\")\n@Data\npublic class RegisterDTO {\n    @ApiModelProperty(value = \"\u8d26\u6237(\u5c0f\u4e8e\u7b49\u4e8e12\u4f4d,\u5927\u4e8e\u7b49\u4e8e8\u4f4d\u5b57\u7b26)\", required = true)\n    @Size(max = 12,min = 8)\n    @NotBlank\n    private String account;\n    @ApiModelProperty(value = \"\u5bc6\u7801(\u5c0f\u4e8e\u7b49\u4e8e12\u4f4d,\u5927\u4e8e\u7b49\u4e8e8\u4f4d\u5b57\u7b26)\", required = true)\n    @Size(max = 12,min = 8)\n    @NotBlank\n    private String password;\n    @ApiModelProperty(value = \"\u6635\u79f0(\u5c0f\u4e8e\u7b49\u4e8e12\u4f4d,\u5927\u4e8e\u7b49\u4e8e8\u4f4d\u5b57\u7b26)\", required = true)\n    @Size(max = 12,min = 8)\n    @NotBlank\n    private String name;\n\n    @Override\n    public String toString() {\n        return \"RegisterDTO{\" +\n                \"account='\" + account + '\\'' +\n                \", password='\" + password + '\\'' +\n                \", name='\" + name + '\\'' +\n                '}';\n    }\n}<\/pre>\n\n\n\n<p><strong>\u53ef\u4ee5\u5728 Controller\u7684\u65b9\u6cd5\u4e0a\u4f7f\u7528@Validated \uff0c\u8fd9\u6837\u53ea\u9a8c\u8bc1\u8be5\u65b9\u6cd5\u7684RequestBody\u53c2\u6570<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/**\n     * \u6ce8\u518c\u63a5\u53e3\n     *\n     * @param registerDTO\n     * @return\n     *\/\n    @ApiOperation(\"\u6ce8\u518c\u63a5\u53e3(\u8d26\u6237\u3001\u6635\u79f0\u4e0d\u80fd\u91cd\u590d)[\u4e0d\u9700\u8981token]\")\n    @PostMapping(\"register\")\n    public ResponseVO&lt;Object> register(@RequestBody @Validated RegisterDTO registerDTO) throws InterruptedException {\n        log.info(\"\u8bf7\u6c42\u63a5\u53e3 \/netsadcloud\/user\/register \u53c2\u6570\uff1a{}\", JSON.toJSONString(registerDTO));\n        return userService.register(registerDTO);\n    }<\/pre>\n\n\n\n<p><strong>\u4e5f\u53ef\u4ee5\u5728Controller\u7c7b\u6dfb\u52a0\u6ce8\u89e3@Validated \uff08\u8fd9\u6837\u6574\u4e2a\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u90fd\u4f1a\u9a8c\u8bc1\u53c2\u6570\uff09<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Validated\n@RestController\n@RequestMapping(\"\/api\")\npublic class PersonController {\n\n    @GetMapping(\"\/person\/{id}\")\n    public ResponseEntity&lt;Integer> getPersonByID(@Validated @PathVariable(\"id\") @Max(value = 5,message = \"\u8d85\u8fc7 id \u7684\u8303\u56f4\u4e86\") Integer id) {\n        return ResponseEntity.ok().body(id);\n    }\n\n    @PutMapping(\"\/person\")\n    public ResponseEntity&lt;String> getPersonByName(@Validated @RequestParam(\"name\") @Size(max = 6,message = \"\u8d85\u8fc7 name \u7684\u8303\u56f4\u4e86\") String name) {\n        return ResponseEntity.ok().body(name);\n    }\n}<\/pre>\n\n\n\n<p><strong>@validated\u6ce8\u89e3\u8bf4\u660e<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"733\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-1024x733.png\" alt=\"\" class=\"wp-image-6311\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-1024x733.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-300x215.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-768x550.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-830x594.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-230x165.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-350x250.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119-480x343.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/07\/\u56fe\u7247-119.png 1294w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Spring-boot\u4e2d\u53ef\u4ee5\u7528@validated\u6765\u6821\u9a8c\u6570\u636e\uff0c\u5982\u679c\u6570\u636e\u5f02\u5e38\u5219\u4f1a\u7edf\u4e00\u629b\u51fa\u5f02\u5e38\uff0c\u65b9\u4fbf\u5f02\u5e38\u4e2d\u5fc3\u7edf\u4e00\u5904 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6307"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6307"}],"version-history":[{"count":4,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6307\/revisions"}],"predecessor-version":[{"id":6312,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/6307\/revisions\/6312"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}