{"id":10008147,"date":"2023-08-23T13:02:54","date_gmt":"2023-08-23T05:02:54","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008147"},"modified":"2023-08-23T13:02:54","modified_gmt":"2023-08-23T05:02:54","slug":"%e5%91%8a%e5%88%ab%e6%b7%b7%e4%b9%b1%e4%bb%a3%e7%a0%81%ef%bc%9a%e8%bf%99%e4%bb%bd-spring-boot-%e5%90%8e%e7%ab%af%e6%8e%a5%e5%8f%a3%e8%a7%84%e8%8c%83%e6%9d%a5%e5%be%97%e5%a4%aa%e5%8f%8a%e6%97%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008147","title":{"rendered":"\u544a\u522b\u6df7\u4e71\u4ee3\u7801\uff1a\u8fd9\u4efd Spring Boot \u540e\u7aef\u63a5\u53e3\u89c4\u8303\u6765\u5f97\u592a\u53ca\u65f6\u4e86\uff01"},"content":{"rendered":"<h2>\u4e00\u3001\u524d\u8a00<\/h2>\n<p>\u4e00\u4e2a\u540e\u7aef\u63a5\u53e3\u5927\u81f4\u5206\u4e3a\u56db\u4e2a\u90e8\u5206\u7ec4\u6210\uff1a<strong>\u63a5\u53e3\u5730\u5740\uff08url\uff09\u3001\u63a5\u53e3\u8bf7\u6c42\u65b9\u5f0f\uff08get\u3001post\u7b49\uff09\u3001\u8bf7\u6c42\u6570\u636e\uff08request\uff09\u3001\u54cd\u5e94\u6570\u636e\uff08response\uff09<\/strong>\u3002\u867d\u7136\u8bf4\u540e\u7aef\u63a5\u53e3\u7684\u7f16\u5199\u5e76\u6ca1\u6709\u7edf\u4e00\u89c4\u8303\u8981\u6c42\uff0c\u800c\u4e14\u5982\u4f55\u6784\u5efa\u8fd9\u51e0\u4e2a\u90e8\u5206\u6bcf\u4e2a\u516c\u53f8\u8981\u6c42\u90fd\u4e0d\u540c\uff0c\u6ca1\u6709\u4ec0\u4e48\u201c\u4e00\u5b9a\u662f\u6700\u597d\u7684\u201d\u6807\u51c6\uff0c\u4f46\u5176\u4e2d\u6700\u91cd\u8981\u7684\u5173\u952e\u70b9\u5c31\u662f\u770b\u662f\u5426\u89c4\u8303\u3002<\/p>\n<h2>\u4e8c\u3001\u73af\u5883\u8bf4\u660e<\/h2>\n<p>\u56e0\u4e3a\u8bb2\u89e3\u7684\u91cd\u70b9\u662f\u540e\u7aef\u63a5\u53e3\uff0c\u6240\u4ee5\u9700\u8981\u5bfc\u5165\u4e00\u4e2a<code>spring-boot-starter-web<\/code>\u5305\uff0c\u800clombok\u4f5c\u7528\u662f\u7b80\u5316\u7c7b\uff0c\u524d\u7aef\u663e\u793a\u5219\u4f7f\u7528\u4e86knife4j\uff0c\u5177\u4f53\u4f7f\u7528\u5728Spring Boot\u6574\u5408knife4j\u5b9e\u73b0Api\u6587\u6863\u5df2\u5199\u660e\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=Ah8JeyL3atVPRGADq0QXdQ%3D%3D.xZxzRUF9SdY8H2yiVUGPmhvPC8tKj5xB%2FaNVhpF4RcDaf0hdoILbZTscdoYFp2KgwXvuCGL4CqtVchcTkd8t2w%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<p>\u53e6\u5916\u4ecespringboot-2.3\u5f00\u59cb\uff0c\u6821\u9a8c\u5305\u88ab\u72ec\u7acb\u6210\u4e86\u4e00\u4e2astarter\u7ec4\u4ef6\uff0c\u6240\u4ee5\u9700\u8981\u5f15\u5165\u5982\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<pre><code>&lt;dependency&gt;\n&lt;!--\u65b0\u7248\u6846\u67b6\u6ca1\u6709\u81ea\u52a8\u5f15\u5165\u9700\u8981\u624b\u52a8\u5f15\u5165--&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-validation&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;com.github.xiaoymin&lt;\/groupId&gt;\n    &lt;artifactId&gt;knife4j-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;!--\u5728\u5f15\u7528\u65f6\u8bf7\u5728maven\u4e2d\u592e\u4ed3\u5e93\u641c\u7d22\u6700\u65b0\u7248\u672c\u53f7--&gt;\n    &lt;version&gt;2.0.2&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;\n    &lt;artifactId&gt;lombok&lt;\/artifactId&gt;\n    &lt;optional&gt;true&lt;\/optional&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u4e09\u3001\u53c2\u6570\u6821\u9a8c<\/h2>\n<h3>1\u3001\u4ecb\u7ecd<\/h3>\n<p>\u4e00\u4e2a\u63a5\u53e3\u4e00\u822c\u5bf9\u53c2\u6570\uff08\u8bf7\u6c42\u6570\u636e\uff09\u90fd\u4f1a\u8fdb\u884c\u5b89\u5168\u6821\u9a8c\uff0c\u53c2\u6570\u6821\u9a8c\u7684\u91cd\u8981\u6027\u81ea\u7136\u4e0d\u5fc5\u591a\u8bf4\uff0c\u90a3\u4e48\u5982\u4f55\u5bf9\u53c2\u6570\u8fdb\u884c\u6821\u9a8c\u5c31\u6709\u8bb2\u7a76\u4e86\u3002\u4e00\u822c\u6765\u8bf4\u6709\u4e09\u79cd\u5e38\u89c1\u7684\u6821\u9a8c\u65b9\u5f0f\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6700\u7b80\u6d01\u7684\u7b2c\u4e09\u79cd\u65b9\u6cd5<\/p>\n<ul>\n<li>\u4e1a\u52a1\u5c42\u6821\u9a8c<\/li>\n<li>Validator + BindResult\u6821\u9a8c<\/li>\n<li>Validator + \u81ea\u52a8\u629b\u51fa\u5f02\u5e38<\/li>\n<\/ul>\n<p>\u4e1a\u52a1\u5c42\u6821\u9a8c\u65e0\u9700\u591a\u8bf4\uff0c\u5373\u624b\u52a8\u5728java\u7684Service\u5c42\u8fdb\u884c\u6570\u636e\u6821\u9a8c\u5224\u65ad\u3002\u4e0d\u8fc7\u8fd9\u6837\u592a\u7e41\u7410\u4e86\uff0c\u5149\u6821\u9a8c\u4ee3\u7801\u5c31\u4f1a\u6709\u5f88\u591a<\/p>\n<p>\u800c\u4f7f\u7528<code>Validator+ BindingResult<\/code>\u5df2\u7ecf\u662f\u975e\u5e38\u65b9\u4fbf\u5b9e\u7528\u7684\u53c2\u6570\u6821\u9a8c\u65b9\u5f0f\u4e86\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4e5f\u6709\u5f88\u591a\u9879\u76ee\u5c31\u662f\u8fd9\u4e48\u505a\u7684\uff0c\u4e0d\u8fc7\u8fd9\u6837\u8fd8\u662f\u4e0d\u592a\u65b9\u4fbf\uff0c\u56e0\u4e3a\u4f60\u6bcf\u5199\u4e00\u4e2a\u63a5\u53e3\u90fd\u8981\u6dfb\u52a0\u4e00\u4e2aBindingResult\u53c2\u6570\uff0c\u7136\u540e\u518d\u63d0\u53d6\u9519\u8bef\u4fe1\u606f\u8fd4\u56de\u7ed9\u524d\u7aef\uff08\u7b80\u5355\u770b\u4e00\u4e0b\uff09\u3002<\/p>\n<pre><code>@PostMapping(&quot;\/addUser&quot;)\npublic String addUser(@RequestBody @Validated User user, BindingResult bindingResult) {\n    \/\/ \u5982\u679c\u6709\u53c2\u6570\u6821\u9a8c\u5931\u8d25\uff0c\u4f1a\u5c06\u9519\u8bef\u4fe1\u606f\u5c01\u88c5\u6210\u5bf9\u8c61\u7ec4\u88c5\u5728BindingResult\u91cc\n    List&lt;ObjectError&gt; allErrors = bindingResult.getAllErrors();\n    if(!allErrors.isEmpty()){\n        return allErrors.stream()\n            .map(o-&gt;o.getDefaultMessage())\n            .collect(Collectors.toList()).toString();\n    }\n    \/\/ \u8fd4\u56de\u9ed8\u8ba4\u7684\u9519\u8bef\u4fe1\u606f\n    \/\/ return allErrors.get(0).getDefaultMessage();\n    return validationService.addUser(user);\n}<\/code><\/pre>\n<h3>2\u3001Validator + \u81ea\u52a8\u629b\u51fa\u5f02\u5e38\uff08\u4f7f\u7528\uff09<\/h3>\n<p>\u5185\u7f6e\u53c2\u6570\u6821\u9a8c\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6ce8\u89e3<\/th>\n<th>\u6821\u9a8c\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>@AssertFalse<\/td>\n<td>\u5fc5\u987b\u662ffalse<\/td>\n<\/tr>\n<tr>\n<td>@AssertTrue<\/td>\n<td>\u5fc5\u987b\u662ftrue<\/td>\n<\/tr>\n<tr>\n<td>@DecimalMax<\/td>\n<td>\u5c0f\u4e8e\u7b49\u4e8e\u7ed9\u5b9a\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>@DecimalMin<\/td>\n<td>\u5927\u4e8e\u7b49\u4e8e\u7ed9\u5b9a\u7684\u503c<\/td>\n<\/tr>\n<tr>\n<td>@Digits<\/td>\n<td>\u53ef\u8bbe\u5b9a\u6700\u5927\u6574\u6570\u4f4d\u6570\u548c\u6700\u5927\u5c0f\u6570\u4f4d\u6570<\/td>\n<\/tr>\n<tr>\n<td>@Email<\/td>\n<td>\u6821\u9a8c\u662f\u5426\u7b26\u5408Email\u683c\u5f0f<\/td>\n<\/tr>\n<tr>\n<td>@Future<\/td>\n<td>\u5fc5\u987b\u662f\u5c06\u6765\u7684\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>@FutureOrPresent<\/td>\n<td>\u5f53\u524d\u6216\u5c06\u6765\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>@Max<\/td>\n<td>\u6700\u5927\u503c<\/td>\n<\/tr>\n<tr>\n<td>@Min<\/td>\n<td>\u6700\u5c0f\u503c<\/td>\n<\/tr>\n<tr>\n<td>@Negative<\/td>\n<td>\u8d1f\u6570\uff08\u4e0d\u5305\u62ec0\uff09<\/td>\n<\/tr>\n<tr>\n<td>@NegativeOrZero<\/td>\n<td>\u8d1f\u6570\u62160<\/td>\n<\/tr>\n<tr>\n<td>@NotBlank<\/td>\n<td>\u4e0d\u4e3anull\u5e76\u4e14\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u975e\u7a7a\u767d\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>@NotEmpty<\/td>\n<td>\u4e0d\u4e3anull\u5e76\u4e14\u4e0d\u4e3a\u7a7a<\/td>\n<\/tr>\n<tr>\n<td>@NotNull<\/td>\n<td>\u4e0d\u4e3anull<\/td>\n<\/tr>\n<tr>\n<td>@Null<\/td>\n<td>\u4e3anull<\/td>\n<\/tr>\n<tr>\n<td>@Past<\/td>\n<td>\u5fc5\u987b\u662f\u8fc7\u53bb\u7684\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>@PastOrPresent<\/td>\n<td>\u5fc5\u987b\u662f\u8fc7\u53bb\u7684\u65f6\u95f4\uff0c\u5305\u542b\u73b0\u5728<\/td>\n<\/tr>\n<tr>\n<td>@PositiveOrZero<\/td>\n<td>\u6b63\u6570\u62160<\/td>\n<\/tr>\n<tr>\n<td>@Size<\/td>\n<td>\u6821\u9a8c\u5bb9\u5668\u7684\u5143\u7d20\u4e2a\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9996\u5148Validator\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u5236\u5b9a\u6821\u9a8c\u89c4\u5219\uff0c\u5e76\u81ea\u52a8\u5e2e\u4f60\u5b8c\u6210\u6821\u9a8c\u3002\u9996\u5148\u5728\u5165\u53c2\u91cc\u9700\u8981\u6821\u9a8c\u7684\u5b57\u6bb5\u52a0\u4e0a\u6ce8\u89e3,\u6bcf\u4e2a\u6ce8\u89e3\u5bf9\u5e94\u4e0d\u540c\u7684\u6821\u9a8c\u89c4\u5219\uff0c\u5e76\u53ef\u5236\u5b9a\u6821\u9a8c\u5931\u8d25\u540e\u7684\u4fe1\u606f\uff1a<\/p>\n<pre><code>@Data\npublic class User {\n    @NotNull(message = &quot;\u7528\u6237id\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n    private Long id;\n\n    @NotNull(message = &quot;\u7528\u6237\u8d26\u53f7\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n    @Size(min = 6, max = 11, message = &quot;\u8d26\u53f7\u957f\u5ea6\u5fc5\u987b\u662f6-11\u4e2a\u5b57\u7b26&quot;)\n    private String account;\n\n    @NotNull(message = &quot;\u7528\u6237\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n    @Size(min = 6, max = 11, message = &quot;\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u662f6-16\u4e2a\u5b57\u7b26&quot;)\n    private String password;\n\n    @NotNull(message = &quot;\u7528\u6237\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a&quot;)\n    @Email(message = &quot;\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e&quot;)\n    private String email;\n}<\/code><\/pre>\n<p>\u6821\u9a8c\u89c4\u5219\u548c\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u914d\u7f6e\u5b8c\u6bd5\u540e\uff0c\u63a5\u4e0b\u6765\u53ea\u9700\u8981\u5728\u63a5\u53e3\u4ec5\u9700\u8981\u5728\u6821\u9a8c\u7684\u53c2\u6570\u4e0a\u52a0\u4e0a<code>@Valid<\/code>\u6ce8\u89e3\uff08\u53bb\u6389BindingResult\u540e\u4f1a\u81ea\u52a8\u5f15\u53d1\u5f02\u5e38\uff0c\u5f02\u5e38\u53d1\u751f\u4e86\u81ea\u7136\u800c\u7136\u5c31\u4e0d\u4f1a\u6267\u884c\u4e1a\u52a1\u903b\u8f91\uff09\uff1a<\/p>\n<pre><code>@RestController\n@RequestMapping(&quot;user&quot;)\npublic class ValidationController {\n\n    @Autowired\n    private ValidationService validationService;\n\n    @PostMapping(&quot;\/addUser&quot;)\n    public String addUser(@RequestBody @Validated User user) {\n\n        return validationService.addUser(user);\n    }\n}<\/code><\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u8fdb\u884c\u6d4b\u8bd5\uff0c\u6253\u5f00knife4j\u6587\u6863\u5730\u5740\uff0c\u5f53\u8f93\u5165\u7684\u8bf7\u6c42\u6570\u636e\u4e3a\u7a7a\u65f6\uff0cValidator\u4f1a\u5c06\u6240\u6709\u7684\u62a5\u9519\u4fe1\u606f\u5168\u90e8\u8fdb\u884c\u8fd4\u56de\uff0c\u6240\u4ee5\u9700\u8981\u4e0e\u5168\u5c40\u5f02\u5e38\u5904\u7406\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<pre><code>\/\/ \u4f7f\u7528form data\u65b9\u5f0f\u8c03\u7528\u63a5\u53e3\uff0c\u6821\u9a8c\u5f02\u5e38\u629b\u51fa BindException\n\/\/ \u4f7f\u7528 json \u8bf7\u6c42\u4f53\u8c03\u7528\u63a5\u53e3\uff0c\u6821\u9a8c\u5f02\u5e38\u629b\u51fa MethodArgumentNotValidException\n\/\/ \u5355\u4e2a\u53c2\u6570\u6821\u9a8c\u5f02\u5e38\u629b\u51faConstraintViolationException\n\/\/ \u5904\u7406 json \u8bf7\u6c42\u4f53\u8c03\u7528\u63a5\u53e3\u6821\u9a8c\u5931\u8d25\u629b\u51fa\u7684\u5f02\u5e38\n@ExceptionHandler(MethodArgumentNotValidException.class)\npublic ResultVO&lt;String&gt; MethodArgumentNotValidException(MethodArgumentNotValidException e) {\n    List&lt;FieldError&gt; fieldErrors = e.getBindingResult().getFieldErrors();\n    List&lt;String&gt; collect = fieldErrors.stream()\n        .map(DefaultMessageSourceResolvable::getDefaultMessage)\n        .collect(Collectors.toList());\n    return new ResultVO(ResultCode.VALIDATE_FAILED, collect);\n}\n\/\/ \u4f7f\u7528form data\u65b9\u5f0f\u8c03\u7528\u63a5\u53e3\uff0c\u6821\u9a8c\u5f02\u5e38\u629b\u51fa BindException\n@ExceptionHandler(BindException.class)\npublic ResultVO&lt;String&gt; BindException(BindException e) {\n    List&lt;FieldError&gt; fieldErrors = e.getBindingResult().getFieldErrors();\n    List&lt;String&gt; collect = fieldErrors.stream()\n        .map(DefaultMessageSourceResolvable::getDefaultMessage)\n        .collect(Collectors.toList());\n    return new ResultVO(ResultCode.VALIDATE_FAILED, collect);\n}<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"587\" height=\"550\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9m8n\" alt=\"\" title=\"\"><\/p>\n<h3>3\u3001\u5206\u7ec4\u6821\u9a8c\u548c\u9012\u5f52\u6821\u9a8c<\/h3>\n<p>\u5206\u7ec4\u6821\u9a8c\u6709\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u5206\u7ec4\u7c7b\uff08\u6216\u63a5\u53e3\uff09<\/li>\n<li>\u5728\u6821\u9a8c\u6ce8\u89e3\u4e0a\u6dfb\u52a0groups\u5c5e\u6027\u6307\u5b9a\u5206\u7ec4<\/li>\n<li>Controller\u65b9\u6cd5\u7684<code>@Validated<\/code>\u6ce8\u89e3\u6dfb\u52a0\u5206\u7ec4\u7c7b<\/li>\n<\/ul>\n<pre><code>public interface Update extends Default{\n}\n@Data\npublic class User {\n    @NotNull(message = &quot;\u7528\u6237id\u4e0d\u80fd\u4e3a\u7a7a&quot;,groups = Update.class)\n    private Long id;\n  ......\n}\n@PostMapping(&quot;update&quot;)\npublic String update(@Validated({Update.class}) User user) {\n    return &quot;success&quot;;\n}<\/code><\/pre>\n<p>\u5982\u679cUpdate\u4e0d\u7ee7\u627fDefault\uff0c<code>@Validated({Update.class})<\/code>\u5c31\u53ea\u4f1a\u6821\u9a8c\u5c5e\u4e8e<code>Update.class<\/code>\u5206\u7ec4\u7684\u53c2\u6570\u5b57\u6bb5\uff1b\u5982\u679c\u7ee7\u627f\u4e86\uff0c\u4f1a\u6821\u9a8c\u4e86\u5176\u4ed6\u9ed8\u8ba4\u5c5e\u4e8e<code>Default.class<\/code>\u5206\u7ec4\u7684\u5b57\u6bb5\u3002<\/p>\n<p>\u5bf9\u4e8e\u9012\u5f52\u6821\u9a8c\uff08\u6bd4\u5982\u7c7b\u4e2d\u7c7b\uff09\uff0c\u53ea\u8981\u5728\u76f8\u5e94\u5c5e\u6027\u7c7b\u4e0a\u589e\u52a0<code>@Valid<\/code>\u6ce8\u89e3\u5373\u53ef\u5b9e\u73b0\uff08\u5bf9\u4e8e\u96c6\u5408\u540c\u6837\u9002\u7528\uff09<\/p>\n<h3>4\u3001\u81ea\u5b9a\u4e49\u6821\u9a8c<\/h3>\n<p>Spring Validation\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u6821\u9a8c\uff0c\u5b9e\u73b0\u5f88\u7b80\u5355\uff0c\u5206\u4e24\u6b65\uff1a<\/p>\n<ul>\n<li>\u81ea\u5b9a\u4e49\u6821\u9a8c\u6ce8\u89e3<\/li>\n<li>\u7f16\u5199\u6821\u9a8c\u8005\u7c7b<\/li>\n<\/ul>\n<pre><code>@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })\n@Retention(RUNTIME)\n@Documented\n@Constraint(validatedBy = {HaveNoBlankValidator.class})\/\/ \u6807\u660e\u7531\u54ea\u4e2a\u7c7b\u6267\u884c\u6821\u9a8c\u903b\u8f91\npublic @interface HaveNoBlank {\n\n    \/\/ \u6821\u9a8c\u51fa\u9519\u65f6\u9ed8\u8ba4\u8fd4\u56de\u7684\u6d88\u606f\n    String message() default &quot;\u5b57\u7b26\u4e32\u4e2d\u4e0d\u80fd\u542b\u6709\u7a7a\u683c&quot;;\n    Class&lt;?&gt;[] groups() default { };\n    Class&lt;? extends Payload&gt;[] payload() default { };\n    \/**\n     * \u540c\u4e00\u4e2a\u5143\u7d20\u4e0a\u6307\u5b9a\u591a\u4e2a\u8be5\u6ce8\u89e3\u65f6\u4f7f\u7528\n     *\/\n    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })\n    @Retention(RUNTIME)\n    @Documented\n    public @interface List {\n        NotBlank[] value();\n    }\n}\npublic class HaveNoBlankValidator implements ConstraintValidator&lt;HaveNoBlank, String&gt; {\n    @Override\n    public boolean isValid(String value, ConstraintValidatorContext context) {\n        \/\/ null \u4e0d\u505a\u68c0\u9a8c\n        if (value == null) {\n            return true;\n        }\n        \/\/ \u6821\u9a8c\u5931\u8d25\n        return !value.contains(&quot; &quot;);\n        \/\/ \u6821\u9a8c\u6210\u529f\n    }\n}<\/code><\/pre>\n<h2>\u56db\u3001\u5168\u5c40\u5f02\u5e38\u5904\u7406<\/h2>\n<p>\u53c2\u6570\u6821\u9a8c\u5931\u8d25\u4f1a\u81ea\u52a8\u5f15\u53d1\u5f02\u5e38\uff0c\u6211\u4eec\u5f53\u7136\u4e0d\u53ef\u80fd\u518d\u53bb\u624b\u52a8\u6355\u6349\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002\u4f46\u6211\u4eec\u53c8\u4e0d\u60f3\u624b\u52a8\u6355\u6349\u8fd9\u4e2a\u5f02\u5e38\uff0c\u53c8\u8981\u5bf9\u8fd9\u4e2a\u5f02\u5e38\u8fdb\u884c\u5904\u7406\uff0c\u90a3\u6b63\u597d\u4f7f\u7528SpringBoot\u5168\u5c40\u5f02\u5e38\u5904\u7406\u6765\u8fbe\u5230\u4e00\u52b3\u6c38\u9038\u7684\u6548\u679c\uff01<\/p>\n<h3>1\u3001\u57fa\u672c\u4f7f\u7528<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u65b0\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5728\u8fd9\u4e2a\u7c7b\u4e0a\u52a0\u4e0a<code>@ControllerAdvice<\/code>\u6216<code>@RestControllerAdvice<\/code>\u6ce8\u89e3\uff0c\u8fd9\u4e2a\u7c7b\u5c31\u914d\u7f6e\u6210\u5168\u5c40\u5904\u7406\u7c7b\u4e86\u3002<\/p>\n<blockquote><p>\u8fd9\u4e2a\u6839\u636e\u4f60\u7684Controller\u5c42\u7528\u7684\u662f<code>@Controller<\/code>\u8fd8\u662f<code>@RestController<\/code>\u6765\u51b3\u5b9a\u3002<\/p><\/blockquote>\n<p>\u7136\u540e\u5728\u7c7b\u4e2d\u65b0\u5efa\u65b9\u6cd5\uff0c\u5728\u65b9\u6cd5\u4e0a\u52a0\u4e0a<code>@ExceptionHandler<\/code>\u6ce8\u89e3\u5e76\u6307\u5b9a\u4f60\u60f3\u5904\u7406\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u63a5\u7740\u5728\u65b9\u6cd5\u5185\u7f16\u5199\u5bf9\u8be5\u5f02\u5e38\u7684\u64cd\u4f5c\u903b\u8f91\uff0c\u5c31\u5b8c\u6210\u4e86\u5bf9\u8be5\u5f02\u5e38\u7684\u5168\u5c40\u5904\u7406\uff01\u6211\u4eec\u73b0\u5728\u5c31\u6765\u6f14\u793a\u4e00\u4e0b\u5bf9\u53c2\u6570\u6821\u9a8c\u5931\u8d25\u629b\u51fa\u7684<code>MethodArgumentNotValidException<\/code>\u5168\u5c40\u5904\u7406\uff1a<\/p>\n<pre><code>package com.csdn.demo1.global;\n\nimport org.springframework.validation.ObjectError;\nimport org.springframework.web.bind.MethodArgumentNotValidException;\nimport org.springframework.web.bind.annotation.ExceptionHandler;\nimport org.springframework.web.bind.annotation.RestControllerAdvice;\n\n@RestControllerAdvice\n@ResponseBody\npublic class ExceptionControllerAdvice {\n\n    @ExceptionHandler(MethodArgumentNotValidException.class)\n    @ResponseStatus(HttpStatus.BAD_REQUEST)\n    public String MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {\n        \/\/ \u4ece\u5f02\u5e38\u5bf9\u8c61\u4e2d\u62ff\u5230ObjectError\u5bf9\u8c61\n        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);\n        \/\/ \u7136\u540e\u63d0\u53d6\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u8fdb\u884c\u8fd4\u56de\n        return objectError.getDefaultMessage();\n    }\n    \n     \/**\n     * \u7cfb\u7edf\u5f02\u5e38 \u9884\u671f\u4ee5\u5916\u5f02\u5e38\n     *\/\n    @ExceptionHandler(Exception.class)\n    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)\n    public ResultVO&lt;?&gt; handleUnexpectedServer(Exception ex) {\n        log.error(&quot;\u7cfb\u7edf\u5f02\u5e38\uff1a&quot;, ex);\n        \/\/ GlobalMsgEnum.ERROR\u662f\u6211\u81ea\u5df1\u5b9a\u4e49\u7684\u679a\u4e3e\u7c7b\n        return new ResultVO&lt;&gt;(GlobalMsgEnum.ERROR);\n    }\n\n    \/**\n     * \u6240\u4ee5\u5f02\u5e38\u7684\u62e6\u622a\n     *\/\n    @ExceptionHandler(Throwable.class)\n    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)\n    public ResultVO&lt;?&gt; exception(Throwable ex) {\n        log.error(&quot;\u7cfb\u7edf\u5f02\u5e38\uff1a&quot;, ex);\n        return new ResultVO&lt;&gt;(GlobalMsgEnum.ERROR);\n    }\n}<\/code><\/pre>\n<p>\u6211\u4eec\u518d\u6b21\u8fdb\u884c\u6d4b\u8bd5\uff0c\u8fd9\u6b21\u8fd4\u56de\u7684\u5c31\u662f\u6211\u4eec\u5236\u5b9a\u7684\u9519\u8bef\u63d0\u793a\u4fe1\u606f\uff01\u6211\u4eec\u901a\u8fc7\u5168\u5c40\u5f02\u5e38\u5904\u7406\u4f18\u96c5\u7684\u5b9e\u73b0\u4e86\u6211\u4eec\u60f3\u8981\u7684\u529f\u80fd\uff01<\/p>\n<p>\u4ee5\u540e\u6211\u4eec\u518d\u60f3\u5199\u63a5\u53e3\u53c2\u6570\u6821\u9a8c\uff0c\u5c31\u53ea\u9700\u8981\u5728\u5165\u53c2\u7684\u6210\u5458\u53d8\u91cf\u4e0a\u52a0\u4e0aValidator\u6821\u9a8c\u89c4\u5219\u6ce8\u89e3\uff0c\u7136\u540e\u5728\u53c2\u6570\u4e0a\u52a0\u4e0a<code>@Valid<\/code>\u6ce8\u89e3\u5373\u53ef\u5b8c\u6210\u6821\u9a8c\uff0c\u6821\u9a8c\u5931\u8d25\u4f1a\u81ea\u52a8\u8fd4\u56de\u9519\u8bef\u63d0\u793a\u4fe1\u606f\uff0c\u65e0\u9700\u4efb\u4f55\u5176\u4ed6\u4ee3\u7801\uff01<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"448\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9m8o\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>2\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h3>\n<p>\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u629b\u51fa\u5f02\u5e38\uff0c\u6bd4\u5982\u5728\u4e1a\u52a1\u5c42\u5f53\u6709\u4e9b\u6761\u4ef6\u5e76\u4e0d\u7b26\u5408\u4e1a\u52a1\u903b\u8f91\uff0c\u800c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u6709\u8bf8\u591a\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u643a\u5e26\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u4e0d\u50cf\u8fd9\u6837\u53ea\u80fd\u643a\u5e26\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u9879\u76ee\u5f00\u53d1\u4e2d\u7ecf\u5e38\u662f\u5f88\u591a\u4eba\u8d1f\u8d23\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u7edf\u4e00\u4e86\u5bf9\u5916\u5f02\u5e38\u5c55\u793a\u7684\u65b9\u5f0f\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49\u5f02\u5e38\u8bed\u4e49\u66f4\u52a0\u6e05\u6670\u660e\u4e86\uff0c\u4e00\u770b\u5c31\u77e5\u9053\u662f\u9879\u76ee\u4e2d\u624b\u52a8\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u73b0\u5728\u5c31\u6765\u5f00\u59cb\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/p>\n<pre><code>package com.csdn.demo1.global;\n\nimport lombok.Getter;\n\n@Getter \/\/\u53ea\u8981getter\u65b9\u6cd5\uff0c\u65e0\u9700setter\npublic class APIException extends RuntimeException {\n    private int code;\n    private String msg;\n\n    public APIException() {\n        this(1001, &quot;\u63a5\u53e3\u9519\u8bef&quot;);\n    }\n\n    public APIException(String msg) {\n        this(1001, msg);\n    }\n\n    public APIException(int code, String msg) {\n        super(msg);\n        this.code = code;\n        this.msg = msg;\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728\u521a\u624d\u7684\u5168\u5c40\u5f02\u5e38\u7c7b\u4e2d\u52a0\u5165\u5982\u4e0b:<\/p>\n<pre><code>\/\/\u81ea\u5b9a\u4e49\u7684\u5168\u5c40\u5f02\u5e38\n  @ExceptionHandler(APIException.class)\n  public String APIExceptionHandler(APIException e) {\n      return e.getMsg();\n  }<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u5bf9\u5f02\u5e38\u7684\u5904\u7406\u5c31\u6bd4\u8f83\u89c4\u8303\u4e86\uff0c\u5f53\u7136\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u5bf9Exception\u7684\u5904\u7406\uff0c\u8fd9\u6837\u65e0\u8bba\u53d1\u751f\u4ec0\u4e48\u5f02\u5e38\u6211\u4eec\u90fd\u80fd\u5c4f\u853d\u6389\u7136\u540e\u54cd\u5e94\u6570\u636e\u7ed9\u524d\u7aef\uff0c\u4e0d\u8fc7\u5efa\u8bae\u6700\u540e\u9879\u76ee\u4e0a\u7ebf\u65f6\u8fd9\u6837\u505a\uff0c\u80fd\u591f\u5c4f\u853d\u6389\u9519\u8bef\u4fe1\u606f\u66b4\u9732\u7ed9\u524d\u7aef\uff0c\u5728\u5f00\u53d1\u4e2d\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5\u8fd8\u662f\u4e0d\u8981\u8fd9\u6837\u505a\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5f53\u6211\u4eec\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u65f6\u5019\u5168\u5c40\u5f02\u5e38\u5904\u7406\u53ea\u54cd\u5e94\u4e86\u5f02\u5e38\u4e2d\u7684\u9519\u8bef\u4fe1\u606fmsg\u7ed9\u524d\u7aef\uff0c\u5e76\u6ca1\u6709\u5c06\u9519\u8bef\u4ee3\u7801code\u8fd4\u56de\u3002\u8fd9\u8fd8\u9700\u8981\u914d\u5408\u6570\u636e\u7edf\u4e00\u54cd\u5e94\u3002<\/p>\n<p>\u5982\u679c\u5728\u591a\u6a21\u5757\u4f7f\u7528\uff0c\u5168\u5c40\u5f02\u5e38\u7b49\u516c\u5171\u529f\u80fd\u62bd\u8c61\u6210\u5b50\u6a21\u5757\uff0c\u5219\u5728\u9700\u8981\u7684\u5b50\u6a21\u5757\u4e2d\u9700\u8981\u5c06\u8be5\u6a21\u5757\u5305\u626b\u63cf\u52a0\u5165\uff0c<code>@SpringBootApplication(scanBasePackages = {&quot;com.xxx&quot;})<\/code><\/p>\n<h2>\u4e94\u3001\u6570\u636e\u7edf\u4e00\u54cd\u5e94<\/h2>\n<p>\u7edf\u4e00\u6570\u636e\u54cd\u5e94\u662f\u6211\u4eec\u81ea\u5df1\u81ea\u5b9a\u4e49\u4e00\u4e2a\u54cd\u5e94\u4f53\u7c7b\uff0c\u65e0\u8bba\u540e\u53f0\u662f\u8fd0\u884c\u6b63\u5e38\u8fd8\u662f\u53d1\u751f\u5f02\u5e38\uff0c\u54cd\u5e94\u7ed9\u524d\u7aef\u7684\u6570\u636e\u683c\u5f0f\u662f\u4e0d\u53d8\u7684\uff01\u8fd9\u91cc\u6211\u5305\u62ec\u4e86\u54cd\u5e94\u4fe1\u606f\u4ee3\u7801code\u548c\u54cd\u5e94\u4fe1\u606f\u8bf4\u660emsg\uff0c\u9996\u5148\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u679a\u4e3e\u89c4\u8303\u54cd\u5e94\u4f53\u4e2d\u7684\u54cd\u5e94\u7801\u548c\u54cd\u5e94\u4fe1\u606f\u3002<\/p>\n<pre><code>@Getter\npublic enum ResultCode {\n    SUCCESS(1000, &quot;\u64cd\u4f5c\u6210\u529f&quot;),\n    FAILED(1001, &quot;\u54cd\u5e94\u5931\u8d25&quot;),\n    VALIDATE_FAILED(1002, &quot;\u53c2\u6570\u6821\u9a8c\u5931\u8d25&quot;),\n    ERROR(5000, &quot;\u672a\u77e5\u9519\u8bef&quot;);\n    private int code;\n    private String msg;\n    ResultCode(int code, String msg) {\n        this.code = code;\n        this.msg = msg;\n    }\n}<\/code><\/pre>\n<p>\u81ea\u5b9a\u4e49\u54cd\u5e94\u4f53<\/p>\n<pre><code>package com.csdn.demo1.global;\n\nimport lombok.Getter;\n\n@Getter\npublic class ResultVO&lt;T&gt; {\n    \/**\n     * \u72b6\u6001\u7801\uff0c\u6bd4\u59821000\u4ee3\u8868\u54cd\u5e94\u6210\u529f\n     *\/\n    private int code;\n    \/**\n     * \u54cd\u5e94\u4fe1\u606f\uff0c\u7528\u6765\u8bf4\u660e\u54cd\u5e94\u60c5\u51b5\n     *\/\n    private String msg;\n    \/**\n     * \u54cd\u5e94\u7684\u5177\u4f53\u6570\u636e\n     *\/\n    private T data;\n    \n    public ResultVO(T data) {\n        this(ResultCode.SUCCESS, data);\n    }\n\n    public ResultVO(ResultCode resultCode, T data) {\n        this.code = resultCode.getCode();\n        this.msg = resultCode.getMsg();\n        this.data = data;\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\u9700\u8981\u4fee\u6539\u5168\u5c40\u5f02\u5e38\u5904\u7406\u7c7b\u7684\u8fd4\u56de\u7c7b\u578b<\/p>\n<pre><code>@RestControllerAdvice\npublic class ExceptionControllerAdvice {\n\n    @ExceptionHandler(APIException.class)\n    public ResultVO&lt;String&gt; APIExceptionHandler(APIException e) {\n        \/\/ \u6ce8\u610f\u54e6\uff0c\u8fd9\u91cc\u4f20\u9012\u7684\u54cd\u5e94\u7801\u679a\u4e3e\n        return new ResultVO&lt;&gt;(ResultCode.FAILED, e.getMsg());\n    }\n\n    @ExceptionHandler(MethodArgumentNotValidException.class)\n    public ResultVO&lt;String&gt; MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {\n        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);\n        \/\/ \u6ce8\u610f\u54e6\uff0c\u8fd9\u91cc\u4f20\u9012\u7684\u54cd\u5e94\u7801\u679a\u4e3e\n        return new ResultVO&lt;&gt;(ResultCode.VALIDATE_FAILED, objectError.getDefaultMessage());\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\u5728controller\u5c42\u8fdb\u884c\u63a5\u53e3\u4fe1\u606f\u6570\u636e\u7684\u8fd4\u56de<\/p>\n<pre><code>@GetMapping(&quot;\/getUser&quot;)\npublic ResultVO&lt;User&gt; getUser() {\n    User user = new User();\n    user.setId(1L);\n    user.setAccount(&quot;12345678&quot;);\n    user.setPassword(&quot;12345678&quot;);\n    user.setEmail(&quot;123@qq.com&quot;);\n\n    return new ResultVO&lt;&gt;(user);\n}<\/code><\/pre>\n<p>\u7ecf\u8fc7\u6d4b\u8bd5\uff0c\u8fd9\u6837\u54cd\u5e94\u7801\u548c\u54cd\u5e94\u4fe1\u606f\u53ea\u80fd\u662f\u679a\u4e3e\u89c4\u5b9a\u7684\u90a3\u51e0\u4e2a\uff0c\u5c31\u771f\u6b63\u505a\u5230\u4e86\u54cd\u5e94\u6570\u636e\u683c\u5f0f\u3001\u54cd\u5e94\u7801\u548c\u54cd\u5e94\u4fe1\u606f\u89c4\u8303\u5316\u3001\u7edf\u4e00\u5316\uff01<\/p>\n<p><img decoding=\"async\" width=\"517\" height=\"408\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9m8p\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u5168\u5c40\u8fd4\u56de\u7c7b\u5982\u4e0b<\/p>\n<pre><code>@Data\n@AllArgsConstructor\n@NoArgsConstructor\npublic class Msg {\n    \/\/\u72b6\u6001\u7801\n    private int code;\n    \/\/\u63d0\u793a\u4fe1\u606f\n    private String msg;\n    \/\/\u7528\u6237\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u7684\u6570\u636e\n    private Map&lt;String,Object&gt; data = new HashMap&lt;&gt;();\n\n    public static Msg success() {\n        Msg result = new Msg();\n        result.setCode(200);\n        result.setMsg(&quot;\u8bf7\u6c42\u6210\u529f\uff01&quot;);\n        return result;\n    }\n\n    public static Msg fail() {\n        Msg result = new Msg();\n        result.setCode(400);\n        result.setMsg(&quot;\u8bf7\u6c42\u5931\u8d25\uff01&quot;);\n        return result;\n    }\n\n    public static Msg fail(String msg) {\n        Msg result = new Msg();\n        result.setCode(400);\n        result.setMsg(msg);\n        return result;\n    }\n\n    public Msg(ReturnResult returnResult){\n        code = returnResult.getCode();\n        msg = returnResult.getMsg();\n    }\n\n    public Msg add(String key,Object value) {\n        this.getData().put(key, value);\n        return this;\n    }\n}<\/code><\/pre>\n<h2>\u516d\u3001\u5168\u5c40\u5904\u7406\u54cd\u5e94\u6570\u636e(\u53ef\u9009\u62e9)<\/h2>\n<p>\u63a5\u53e3\u8fd4\u56de\u7edf\u4e00\u54cd\u5e94\u4f53 + \u5f02\u5e38\u4e5f\u8fd4\u56de\u7edf\u4e00\u54cd\u5e94\u4f53\uff0c\u5176\u5b9e\u8fd9\u6837\u5df2\u7ecf\u5f88\u597d\u4e86\uff0c\u4f46\u8fd8\u662f\u6709\u53ef\u4ee5\u4f18\u5316\u7684\u5730\u65b9\u3002\u8981\u77e5\u9053\u4e00\u4e2a\u9879\u76ee\u4e0b\u6765\u5b9a\u4e49\u7684\u63a5\u53e3\u641e\u4e2a\u51e0\u767e\u4e2a\u592a\u6b63\u5e38\u4e0d\u8fc7\u4e86\uff0c\u8981\u662f\u6bcf\u4e00\u4e2a\u63a5\u53e3\u8fd4\u56de\u6570\u636e\u65f6\u90fd\u8981\u7528\u54cd\u5e94\u4f53\u6765\u5305\u88c5\u4e00\u4e0b\u597d\u50cf\u6709\u70b9\u9ebb\u70e6\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u7701\u53bb\u8fd9\u4e2a\u5305\u88c5\u8fc7\u7a0b\u5462\uff1f<\/p>\n<p>\u5f53\u7136\u662f\u6709\u7684\uff0c\u8fd8\u662f\u8981\u7528\u5230\u5168\u5c40\u5904\u7406\u3002\u4f46\u662f\u4e3a\u4e86\u6269\u5c55\u6027\uff0c\u5c31\u662f\u5141\u8bb8\u7ed5\u8fc7\u6570\u636e\u7edf\u4e00\u54cd\u5e94\uff08\u8fd9\u6837\u5c31\u53ef\u4ee5\u63d0\u4f9b\u591a\u65b9\u4f7f\u7528\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6ce8\u89e3\uff0c\u5229\u7528\u6ce8\u89e3\u6765\u9009\u62e9\u662f\u5426\u8fdb\u884c\u5168\u5c40\u54cd\u5e94\u5305\u88c5<\/p>\n<p>\u9996\u5148\u521b\u5efa\u81ea\u5b9a\u4e49\u6ce8\u89e3\uff0c\u4f5c\u7528\u76f8\u5f53\u4e8e\u5168\u5c40\u5904\u7406\u7c7b\u5f00\u5173\uff1a<\/p>\n<pre><code>@Retention(RetentionPolicy.RUNTIME)\n@Target({ElementType.METHOD}) \/\/ \u8868\u660e\u8be5\u6ce8\u89e3\u53ea\u80fd\u653e\u5728\u65b9\u6cd5\u4e0a\npublic @interface NotResponseBody {\n}<\/code><\/pre>\n<p>\u5176\u6b21\u521b\u5efa\u4e00\u4e2a\u7c7b\u5e76\u52a0\u4e0a\u6ce8\u89e3\u4f7f\u5176\u6210\u4e3a\u5168\u5c40\u5904\u7406\u7c7b\u3002\u7136\u540e\u7ee7\u627f<code>ResponseBodyAdvice<\/code>\u63a5\u53e3\u91cd\u5199\u5176\u4e2d\u7684\u65b9\u6cd5\uff0c\u5373\u53ef\u5bf9\u6211\u4eec\u7684controller\u8fdb\u884c\u589e\u5f3a\u64cd\u4f5c\uff0c\u5177\u4f53\u770b\u4ee3\u7801\u548c\u6ce8\u91ca\uff1a<\/p>\n<pre><code>package com.csdn.demo1.global;\n\nimport com.fasterxml.jackson.core.JsonProcessingException;\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport org.springframework.core.MethodParameter;\nimport org.springframework.http.MediaType;\nimport org.springframework.http.converter.HttpMessageConverter;\nimport org.springframework.http.server.ServerHttpRequest;\nimport org.springframework.http.server.ServerHttpResponse;\nimport org.springframework.web.bind.annotation.RestControllerAdvice;\nimport org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;\n\n@RestControllerAdvice(basePackages = {&quot;com.scdn.demo1.controller&quot;}) \/\/ \u6ce8\u610f\u54e6\uff0c\u8fd9\u91cc\u8981\u52a0\u4e0a\u9700\u8981\u626b\u63cf\u7684\u5305\npublic class ResponseControllerAdvice implements ResponseBodyAdvice&lt;Object&gt; {\n    @Override\n    public boolean supports(MethodParameter returnType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; aClass) {\n       \/\/ \u5982\u679c\u63a5\u53e3\u8fd4\u56de\u7684\u7c7b\u578b\u672c\u8eab\u5c31\u662fResultVO\u90a3\u5c31\u6ca1\u6709\u5fc5\u8981\u8fdb\u884c\u989d\u5916\u7684\u64cd\u4f5c\uff0c\u8fd4\u56defalse\n        \/\/ \u5982\u679c\u65b9\u6cd5\u4e0a\u52a0\u4e86\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u6ce8\u89e3\u4e5f\u6ca1\u6709\u5fc5\u8981\u8fdb\u884c\u989d\u5916\u7684\u64cd\u4f5c\n        return !(returnType.getParameterType().equals(ResultVO.class) || returnType.hasMethodAnnotation(NotResponseBody.class));\n    }\n\n    @Override\n    public Object beforeBodyWrite(Object data, MethodParameter returnType, MediaType mediaType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; aClass, ServerHttpRequest request, ServerHttpResponse response) {\n        \/\/ String\u7c7b\u578b\u4e0d\u80fd\u76f4\u63a5\u5305\u88c5\uff0c\u6240\u4ee5\u8981\u8fdb\u884c\u4e9b\u7279\u522b\u7684\u5904\u7406\n        if (returnType.getGenericParameterType().equals(String.class)) {\n            ObjectMapper objectMapper = new ObjectMapper();\n            try {\n                \/\/ \u5c06\u6570\u636e\u5305\u88c5\u5728ResultVO\u91cc\u540e\uff0c\u518d\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32\u54cd\u5e94\u7ed9\u524d\u7aef\n                return objectMapper.writeValueAsString(new ResultVO&lt;&gt;(data));\n            } catch (JsonProcessingException e) {\n                throw new APIException(&quot;\u8fd4\u56deString\u7c7b\u578b\u9519\u8bef&quot;);\n            }\n        }\n        \/\/ \u5c06\u539f\u672c\u7684\u6570\u636e\u5305\u88c5\u5728ResultVO\u91cc\n        return new ResultVO&lt;&gt;(data);\n    }\n}<\/code><\/pre>\n<p>\u91cd\u5199\u7684\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u662f\u7528\u6765\u5728controller\u5c06\u6570\u636e\u8fdb\u884c\u8fd4\u56de\u524d\u8fdb\u884c\u589e\u5f3a\u64cd\u4f5c\uff0csupports\u65b9\u6cd5\u8981\u8fd4\u56de\u4e3atrue\u624d\u4f1a\u6267\u884c<code>beforeBodyWrite<\/code>\u65b9\u6cd5\uff0c\u6240\u4ee5\u5982\u679c\u6709\u4e9b\u60c5\u51b5\u4e0d\u9700\u8981\u8fdb\u884c\u589e\u5f3a\u64cd\u4f5c\u53ef\u4ee5\u5728supports\u65b9\u6cd5\u91cc\u8fdb\u884c\u5224\u65ad\u3002<\/p>\n<p>\u5bf9\u8fd4\u56de\u6570\u636e\u8fdb\u884c\u771f\u6b63\u7684\u64cd\u4f5c\u8fd8\u662f\u5728<code>beforeBodyWrite<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u8be5\u65b9\u6cd5\u91cc\u5305\u88c5\u6570\u636e\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u6bcf\u4e2a\u63a5\u53e3\u90fd\u8fdb\u884c\u6570\u636e\u5305\u88c5\u4e86\uff0c\u7701\u53bb\u4e86\u5f88\u591a\u9ebb\u70e6\u3002\u6b64\u65f6controller\u53ea\u9700\u8fd9\u6837\u5199\u5c31\u884c\u4e86\uff1a<\/p>\n<pre><code>@GetMapping(&quot;\/getUser&quot;)\n\/\/@NotResponseBody  \/\/\u662f\u5426\u7ed5\u8fc7\u6570\u636e\u7edf\u4e00\u54cd\u5e94\u5f00\u5173\npublic User getUser() {\n    User user = new User();\n    user.setId(1L);\n    user.setAccount(&quot;12345678&quot;);\n    user.setPassword(&quot;12345678&quot;);\n    user.setEmail(&quot;123@qq.com&quot;);\n    \/\/ \u6ce8\u610f\u54e6\uff0c\u8fd9\u91cc\u662f\u76f4\u63a5\u8fd4\u56de\u7684User\u7c7b\u578b\uff0c\u5e76\u6ca1\u6709\u7528ResultVO\u8fdb\u884c\u5305\u88c5\n    return user;\n}<\/code><\/pre>\n<h2>\u4e03\u3001\u63a5\u53e3\u7248\u672c\u63a7\u5236<\/h2>\n<h3>1\u3001\u7b80\u4ecb<\/h3>\n<p>\u5728spring boot\u9879\u76ee\u4e2d\uff0c\u5982\u679c\u8981\u8fdb\u884crestful\u63a5\u53e3\u7684\u7248\u672c\u63a7\u5236\u4e00\u822c\u6709\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u5411\uff1a<\/p>\n<ul>\n<li>\u57fa\u4e8epath\u7684\u7248\u672c\u63a7\u5236<\/li>\n<li>\u57fa\u4e8eheader\u7684\u7248\u672c\u63a7\u5236<\/li>\n<\/ul>\n<p>\u5728spring MVC\u4e0b\uff0curl\u6620\u5c04\u5230\u54ea\u4e2amethod\u662f\u7531<code>RequestMappingHandlerMapping<\/code>\u6765\u63a7\u5236\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u4e5f\u662f\u901a\u8fc7 <code>RequestMappingHandlerMapping<\/code>\u6765\u505a\u7248\u672c\u63a7\u5236\u7684\u3002<\/p>\n<h3>2\u3001Path\u63a7\u5236\u5b9e\u73b0<\/h3>\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u6ce8\u89e3<\/p>\n<pre><code>@Target({ElementType.METHOD, ElementType.TYPE})\n@Retention(RetentionPolicy.RUNTIME)\npublic @interface ApiVersion {\n    \/\/ \u9ed8\u8ba4\u63a5\u53e3\u7248\u672c\u53f71.0\u5f00\u59cb\uff0c\u8fd9\u91cc\u6211\u53ea\u505a\u4e86\u4e24\u7ea7\uff0c\u591a\u7ea7\u53ef\u5728\u6b63\u5219\u8fdb\u884c\u63a7\u5236\n    String value() default &quot;1.0&quot;;\n}<\/code><\/pre>\n<p><code>ApiVersionCondition<\/code>\u7528\u6765\u63a7\u5236\u5f53\u524drequest \u6307\u5411\u54ea\u4e2amethod<\/p>\n<pre><code>public class ApiVersionCondition implements RequestCondition&lt;ApiVersionCondition&gt; {\n    private static final Pattern VERSION_PREFIX_PATTERN = Pattern.compile(&quot;v(\/\/d+\/\/.\/\/d+)&quot;);\n\n    private final String version;\n\n    public ApiVersionCondition(String version) {\n        this.version = version;\n    }\n\n    @Override\n    public ApiVersionCondition combine(ApiVersionCondition other) {\n        \/\/ \u91c7\u7528\u6700\u540e\u5b9a\u4e49\u4f18\u5148\u539f\u5219\uff0c\u5219\u65b9\u6cd5\u4e0a\u7684\u5b9a\u4e49\u8986\u76d6\u7c7b\u4e0a\u9762\u7684\u5b9a\u4e49\n        return new ApiVersionCondition(other.getApiVersion());\n    }\n\n    @Override\n    public ApiVersionCondition getMatchingCondition(HttpServletRequest httpServletRequest) {\n        Matcher m = VERSION_PREFIX_PATTERN.matcher(httpServletRequest.getRequestURI());\n        if (m.find()) {\n            String pathVersion = m.group(1);\n            \/\/ \u8fd9\u4e2a\u65b9\u6cd5\u662f\u7cbe\u786e\u5339\u914d\n            if (Objects.equals(pathVersion, version)) {\n                return this;\n            }\n            \/\/ \u8be5\u65b9\u6cd5\u662f\u53ea\u8981\u5927\u4e8e\u7b49\u4e8e\u6700\u4f4e\u63a5\u53e3version\u5373\u5339\u914d\u6210\u529f\uff0c\u9700\u8981\u548ccompareTo()\u914d\u5408\n            \/\/ \u4e3e\u4f8b\uff1a\u5b9a\u4e49\u67091.0\/1.1\u63a5\u53e3\uff0c\u8bbf\u95ee1.2\uff0c\u5219\u5b9e\u9645\u8bbf\u95ee\u7684\u662f1.1\uff0c\u5982\u679c\u4ece\u5c0f\u5f00\u59cb\u90a3\u4e48\u6392\u5e8f\u53cd\u8f6c\u5373\u53ef\n\/\/            if(Float.parseFloat(pathVersion)&gt;=Float.parseFloat(version)){\n\/\/                return this;\n\/\/            }\n\n        }\n        return null;\n    }\n\n    @Override\n    public int compareTo(ApiVersionCondition other, HttpServletRequest request) {\n        return 0;\n        \/\/ \u4f18\u5148\u5339\u914d\u6700\u65b0\u7684\u7248\u672c\u53f7\uff0c\u548cgetMatchingCondition\u6ce8\u91ca\u6389\u7684\u4ee3\u7801\u540c\u6b65\u4f7f\u7528\n\/\/        return other.getApiVersion().compareTo(this.version);\n    }\n\n    public String getApiVersion() {\n        return version;\n    }\n\n}<\/code><\/pre>\n<p><code>PathVersionHandlerMapping<\/code>\u7528\u4e8e\u6ce8\u5165spring\u7528\u6765\u7ba1\u7406<\/p>\n<pre><code>public class PathVersionHandlerMapping extends RequestMappingHandlerMapping {\n\n    @Override\n    protected boolean isHandler(Class&lt;?&gt; beanType) {\n        return AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);\n    }\n\n    @Override\n    protected RequestCondition&lt;?&gt; getCustomTypeCondition(Class&lt;?&gt; handlerType) {\n        ApiVersion apiVersion = AnnotationUtils.findAnnotation(handlerType,ApiVersion.class);\n        return createCondition(apiVersion);\n    }\n\n    @Override\n    protected RequestCondition&lt;?&gt; getCustomMethodCondition(Method method) {\n        ApiVersion apiVersion = AnnotationUtils.findAnnotation(method,ApiVersion.class);\n        return createCondition(apiVersion);\n    }\n\n    private RequestCondition&lt;ApiVersionCondition&gt;createCondition(ApiVersion apiVersion) {\n        return apiVersion == null ? null : new ApiVersionCondition(apiVersion.value());\n    }\n}<\/code><\/pre>\n<p><code>WebMvcConfiguration<\/code>\u914d\u7f6e\u7c7b\u8ba9spring\u6765\u63a5\u7ba1<\/p>\n<pre><code>@Configuration\npublic class WebMvcConfiguration implements WebMvcRegistrations {\n\n    @Override\n    public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {\n        return new PathVersionHandlerMapping();\n    }\n}<\/code><\/pre>\n<p>\u6700\u540econtroller\u8fdb\u884c\u6d4b\u8bd5\uff0c\u9ed8\u8ba4\u662fv1.0\uff0c\u5982\u679c\u65b9\u6cd5\u4e0a\u6709\u6ce8\u89e3\uff0c\u4ee5\u65b9\u6cd5\u4e0a\u7684\u4e3a\u51c6\uff08\u8be5\u65b9\u6cd5vx.x\u5728\u8def\u5f84\u4efb\u610f\u4f4d\u7f6e\u51fa\u73b0\u90fd\u53ef\u89e3\u6790\uff09<\/p>\n<pre><code>@RestController\n@ApiVersion\n@RequestMapping(value = &quot;\/{version}\/test&quot;)\npublic class TestController {\n\n    @GetMapping(value = &quot;one&quot;)\n    public String query(){\n        return &quot;test api default&quot;;\n    }\n\n    @GetMapping(value = &quot;one&quot;)\n    @ApiVersion(&quot;1.1&quot;)\n    public String query2(){\n        return &quot;test api v1.1&quot;;\n    }\n\n\n    @GetMapping(value = &quot;one&quot;)\n    @ApiVersion(&quot;3.1&quot;)\n    public String query3(){\n        return &quot;test api v3.1&quot;;\n    }\n}<\/code><\/pre>\n<h3>3\u3001header\u63a7\u5236\u5b9e\u73b0<\/h3>\n<p>\u603b\u4f53\u539f\u7406\u4e0ePath\u7c7b\u4f3c\uff0c\u4fee\u6539<code>ApiVersionCondition<\/code> \u5373\u53ef\uff0c\u4e4b\u540e\u8bbf\u95ee\u65f6\u5728header\u5e26\u4e0a<code>X-VERSION<\/code>\u53c2\u6570\u5373\u53ef<\/p>\n<pre><code>public class ApiVersionCondition implements RequestCondition&lt;ApiVersionCondition&gt; {\n    private static final String X_VERSION = &quot;X-VERSION&quot;;\n    private final String version ;\n    \n    public ApiVersionCondition(String version) {\n        this.version = version;\n    }\n\n    @Override\n    public ApiVersionCondition combine(ApiVersionCondition other) {\n        \/\/ \u91c7\u7528\u6700\u540e\u5b9a\u4e49\u4f18\u5148\u539f\u5219\uff0c\u5219\u65b9\u6cd5\u4e0a\u7684\u5b9a\u4e49\u8986\u76d6\u7c7b\u4e0a\u9762\u7684\u5b9a\u4e49\n        return new ApiVersionCondition(other.getApiVersion());\n    }\n\n    @Override\n    public ApiVersionCondition getMatchingCondition(HttpServletRequest httpServletRequest) {\n        String headerVersion = httpServletRequest.getHeader(X_VERSION);\n        if(Objects.equals(version,headerVersion)){\n            return this;\n        }\n        return null;\n    }\n\n    @Override\n    public int compareTo(ApiVersionCondition apiVersionCondition, HttpServletRequest httpServletRequest) {\n        return 0;\n    }\n    public String getApiVersion() {\n        return version;\n    }\n\n}<\/code><\/pre>\n<h2>\u516b\u3001API\u63a5\u53e3\u5b89\u5168<\/h2>\n<h3>1\u3001\u7b80\u4ecb<\/h3>\n<p>APP\u3001\u524d\u540e\u7aef\u5206\u79bb\u9879\u76ee\u90fd\u91c7\u7528API\u63a5\u53e3\u5f62\u5f0f\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u6570\u636e\u901a\u4fe1\uff0c\u4f20\u8f93\u7684\u6570\u636e\u88ab\u5077\u7aa5\u3001\u88ab\u6293\u5305\u3001\u88ab\u4f2a\u9020\u65f6\u6709\u53d1\u751f\uff0c\u90a3\u4e48\u5982\u4f55\u8bbe\u8ba1\u4e00\u5957\u6bd4\u8f83\u5b89\u5168\u7684API\u63a5\u53e3\u65b9\u6848\u81f3\u5173\u91cd\u8981\uff0c\u4e00\u822c\u7684\u89e3\u51b3\u65b9\u6848\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>Token\u6388\u6743\u8ba4\u8bc1\uff0c\u9632\u6b62\u672a\u6388\u6743\u7528\u6237\u83b7\u53d6\u6570\u636e\uff1b<\/li>\n<li>\u65f6\u95f4\u6233\u8d85\u65f6\u673a\u5236\uff1b<\/li>\n<li>URL\u7b7e\u540d\uff0c\u9632\u6b62\u8bf7\u6c42\u53c2\u6570\u88ab\u7be1\u6539\uff1b<\/li>\n<li>\u9632\u91cd\u653e\uff0c\u9632\u6b62\u63a5\u53e3\u88ab\u7b2c\u4e8c\u6b21\u8bf7\u6c42\uff0c\u9632\u91c7\u96c6\uff1b<\/li>\n<li>\u91c7\u7528HTTPS\u901a\u4fe1\u534f\u8bae\uff0c\u9632\u6b62\u6570\u636e\u660e\u6587\u4f20\u8f93\uff1b<\/li>\n<\/ul>\n<h3>2\u3001Token\u6388\u6743\u8ba4\u8bc1<\/h3>\n<blockquote><p>\u56e0\u4e3aHTTP\u534f\u8bae\u662f\u65e0\u72b6\u6001\u7684\uff0cToken\u7684\u8bbe\u8ba1\u65b9\u6848\u662f\u7528\u6237\u5728\u5ba2\u6237\u7aef\u4f7f\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801\u767b\u5f55\u540e\uff0c\u670d\u52a1\u5668\u4f1a\u7ed9\u5ba2\u6237\u7aef\u8fd4\u56de\u4e00\u4e2aToken\uff0c\u5e76\u5c06Token\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u653e\u5728\u7f13\u5b58\uff08\u4e00\u822c\u662fRedis\uff09\u4e2d\uff0c\u540e\u7eed\u5ba2\u6237\u7aef\u5bf9\u9700\u8981\u6388\u6743\u6a21\u5757\u7684\u6240\u6709\u64cd\u4f5c\u90fd\u8981\u5e26\u4e0a\u8fd9\u4e2aToken\uff0c\u670d\u52a1\u5668\u7aef\u63a5\u6536\u5230\u8bf7\u6c42\u540e\u8fdb\u884cToken\u9a8c\u8bc1\uff0c\u5982\u679cToken\u5b58\u5728\uff0c\u8bf4\u660e\u662f\u6388\u6743\u7684\u8bf7\u6c42\u3002<\/p><\/blockquote>\n<p><strong>Token\u751f\u6210\u7684\u8bbe\u8ba1\u8981\u6c42<\/strong><\/p>\n<ul>\n<li>\u5e94\u7528\u5185\u4e00\u5b9a\u8981\u552f\u4e00\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u6388\u6743\u6df7\u4e71\uff0cA\u7528\u6237\u770b\u5230\u4e86B\u7528\u6237\u7684\u6570\u636e\uff1b<\/li>\n<li>\u6bcf\u6b21\u751f\u6210\u7684Token\u4e00\u5b9a\u8981\u4e0d\u4e00\u6837\uff0c\u9632\u6b62\u88ab\u8bb0\u5f55\uff0c\u6388\u6743\u6c38\u4e45\u6709\u6548\uff1b<\/li>\n<li>\u4e00\u822cToken\u5bf9\u5e94\u7684\u662fRedis\u7684key\uff0cvalue\u5b58\u653e\u7684\u662f\u8fd9\u4e2a\u7528\u6237\u76f8\u5173\u7f13\u5b58\u4fe1\u606f\uff0c\u6bd4\u5982\uff1a\u7528\u6237\u7684id\uff1b<\/li>\n<li>\u8981\u8bbe\u7f6eToken\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u8fc7\u671f\u540e\u9700\u8981\u5ba2\u6237\u7aef\u91cd\u65b0\u767b\u5f55\uff0c\u83b7\u53d6\u65b0\u7684Token\uff0c\u5982\u679cToken\u6709\u6548\u671f\u8bbe\u7f6e\u8f83\u77ed\uff0c\u4f1a\u53cd\u590d\u9700\u8981\u7528\u6237\u767b\u5f55\uff0c\u4f53\u9a8c\u6bd4\u8f83\u5dee\uff0c\u6211\u4eec\u4e00\u822c\u91c7\u7528Token\u8fc7\u671f\u540e\uff0c\u5ba2\u6237\u7aef\u9759\u9ed8\u767b\u5f55\u7684\u65b9\u5f0f\uff0c\u5f53\u5ba2\u6237\u7aef\u6536\u5230Token\u8fc7\u671f\u540e\uff0c\u5ba2\u6237\u7aef\u7528\u672c\u5730\u4fdd\u5b58\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u5728\u540e\u53f0\u9759\u9ed8\u767b\u5f55\u6765\u83b7\u53d6\u65b0\u7684Token\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u5355\u72ec\u51fa\u4e00\u4e2a\u5237\u65b0Token\u7684\u63a5\u53e3\uff0c\u4f46\u662f\u4e00\u5b9a\u8981\u6ce8\u610f\u5237\u65b0\u673a\u5236\u548c\u5b89\u5168\u95ee\u9898\uff1b<\/li>\n<\/ul>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u8bbe\u8ba1\u65b9\u6848\u8981\u6c42\uff0c\u6211\u4eec\u5f88\u5bb9\u6613\u5f97\u5230Token=md5(\u7528\u6237ID+\u767b\u5f55\u7684\u65f6\u95f4\u6233+\u670d\u52a1\u5668\u7aef\u79d8\u94a5)\u8fd9\u79cd\u65b9\u5f0f\u6765\u83b7\u5f97Token\uff0c\u56e0\u4e3a\u7528\u6237ID\u662f\u5e94\u7528\u5185\u552f\u4e00\u7684\uff0c\u767b\u5f55\u7684\u65f6\u95f4\u6233\u4fdd\u8bc1\u6bcf\u6b21\u767b\u5f55\u7684\u65f6\u5019\u90fd\u4e0d\u4e00\u6837\uff0c\u670d\u52a1\u5668\u7aef\u79d8\u94a5\u662f\u914d\u7f6e\u5728\u670d\u52a1\u5668\u7aef\u53c2\u4e0e\u52a0\u5bc6\u7684\u5b57\u7b26\u4e32\uff08\u5373\uff1a\u76d0\uff09\uff0c\u76ee\u7684\u662f\u63d0\u9ad8Token\u52a0\u5bc6\u7684\u7834\u89e3\u96be\u5ea6\uff0c\u6ce8\u610f\u4e00\u5b9a\u4e0d\u8981\u6cc4\u6f0f<\/p>\n<h3>3\u3001\u65f6\u95f4\u6233\u8d85\u65f6\u673a\u5236<\/h3>\n<p>\u5ba2\u6237\u7aef\u6bcf\u6b21\u8bf7\u6c42\u63a5\u53e3\u90fd\u5e26\u4e0a\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233timestamp\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u5230timestamp\u540e\u8ddf\u5f53\u524d\u65f6\u95f4\u8fdb\u884c\u6bd4\u5bf9\uff0c\u5982\u679c\u65f6\u95f4\u5dee\u5927\u4e8e\u4e00\u5b9a\u65f6\u95f4\uff08\u6bd4\u5982\uff1a1\u5206\u949f\uff09\uff0c\u5219\u8ba4\u4e3a\u8be5\u8bf7\u6c42\u5931\u6548\u3002<strong>\u65f6\u95f4\u6233\u8d85\u65f6\u673a\u5236\u662f\u9632\u5fa1DOS\u653b\u51fb\u7684\u6709\u6548\u624b\u6bb5\u3002<\/strong> \u4f8b\u5982<code>http:\/\/url\/getInfo?id=1&amp;timetamp=1661061696<\/code><\/p>\n<h3>4\u3001URL\u7b7e\u540d<\/h3>\n<p>\u5199\u8fc7\u652f\u4ed8\u5b9d\u6216\u5fae\u4fe1\u652f\u4ed8\u5bf9\u63a5\u7684\u540c\u5b66\u80af\u5b9a\u5bf9URL\u7b7e\u540d\u4e0d\u964c\u751f\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5c06\u539f\u672c\u53d1\u9001\u7ed9server\u7aef\u7684\u660e\u6587\u53c2\u6570\u505a\u4e00\u4e0b\u7b7e\u540d\uff0c\u7136\u540e\u5728server\u7aef\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u518d\u505a\u4e00\u6b21\u7b7e\u540d\uff0c\u5bf9\u6bd4\u4e24\u6b21\u7b7e\u540d\u5c31\u53ef\u4ee5\u786e\u4fdd\u5bf9\u5e94\u660e\u6587\u7684\u53c2\u6570\u6709\u6ca1\u6709\u88ab\u4e2d\u95f4\u4eba\u7be1\u6539\u8fc7\u3002\u4f8b\u5982<code>http:\/\/url\/getInfo?id=1&amp;timetamp=1559396263&amp;sign=e10adc3949ba59abbe56e057f20f883e<\/code><\/p>\n<p><strong>\u7b7e\u540d\u7b97\u6cd5\u8fc7\u7a0b<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\u5bf9\u901a\u4fe1\u7684\u53c2\u6570\u6309key\u8fdb\u884c\u5b57\u6bcd\u6392\u5e8f\u653e\u5165\u6570\u7ec4\u4e2d\uff08\u4e00\u822c\u8bf7\u6c42\u7684\u63a5\u53e3\u5730\u5740\u4e5f\u8981\u53c2\u4e0e\u6392\u5e8f\u548c\u7b7e\u540d\uff0c\u90a3\u4e48\u9700\u8981\u989d\u5916\u6dfb\u52a0<code>url=http:\/\/url\/getInfo<\/code>\u8fd9\u4e2a\u53c2\u6570\uff09<\/li>\n<li>\u5bf9\u6392\u5e8f\u5b8c\u7684\u6570\u7ec4\u952e\u503c\u5bf9\u7528&#038;\u8fdb\u884c\u8fde\u63a5\uff0c\u5f62\u6210\u7528\u4e8e\u52a0\u5bc6\u7684\u53c2\u6570\u5b57\u7b26\u4e32<\/li>\n<li>\u5728\u52a0\u5bc6\u7684\u53c2\u6570\u5b57\u7b26\u4e32\u524d\u9762\u6216\u8005\u540e\u9762\u52a0\u4e0a\u79c1\u94a5\uff0c\u7136\u540e\u7528md5\u8fdb\u884c\u52a0\u5bc6\uff0c\u5f97\u5230sign\uff0c\u7136\u540e\u968f\u7740\u8bf7\u6c42\u63a5\u53e3\u4e00\u8d77\u4f20\u7ed9\u670d\u52a1\u5668\u3002\u670d\u52a1\u5668\u7aef\u63a5\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u7528\u540c\u6837\u7684\u7b97\u6cd5\u83b7\u5f97\u670d\u52a1\u5668\u7684sign\uff0c\u5bf9\u6bd4\u5ba2\u6237\u7aef\u7684sign\u662f\u5426\u4e00\u81f4\uff0c\u5982\u679c\u4e00\u81f4\u8bf7\u6c42\u6709\u6548<\/li>\n<\/ul>\n<h3>5\u3001\u9632\u91cd\u653e<\/h3>\n<p>\u5ba2\u6237\u7aef\u7b2c\u4e00\u6b21\u8bbf\u95ee\u65f6\uff0c\u5c06\u7b7e\u540dsign\u5b58\u653e\u5230\u670d\u52a1\u5668\u7684Redis\u4e2d\uff0c\u8d85\u65f6\u65f6\u95f4\u8bbe\u5b9a\u4e3a\u8ddf\u65f6\u95f4\u6233\u7684\u8d85\u65f6\u65f6\u95f4\u4e00\u81f4\uff0c\u4e8c\u8005\u65f6\u95f4\u4e00\u81f4\u53ef\u4ee5\u4fdd\u8bc1\u65e0\u8bba\u5728timestamp\u9650\u5b9a\u65f6\u95f4\u5185\u8fd8\u662f\u5916 URL\u90fd\u53ea\u80fd\u8bbf\u95ee\u4e00\u6b21\uff0c\u5982\u679c\u88ab\u975e\u6cd5\u8005\u622a\u83b7\uff0c\u4f7f\u7528\u540c\u4e00\u4e2aURL\u518d\u6b21\u8bbf\u95ee\uff0c\u5982\u679c\u53d1\u73b0\u7f13\u5b58\u670d\u52a1\u5668\u4e2d\u5df2\u7ecf\u5b58\u5728\u4e86\u672c\u6b21\u7b7e\u540d\uff0c\u5219\u62d2\u7edd\u670d\u52a1\u3002<\/p>\n<p>\u5982\u679c\u5728\u7f13\u5b58\u4e2d\u7684\u7b7e\u540d\u5931\u6548\u7684\u60c5\u51b5\u4e0b\uff0c\u6709\u4eba\u4f7f\u7528\u540c\u4e00\u4e2aURL\u518d\u6b21\u8bbf\u95ee\uff0c\u5219\u4f1a\u88ab\u65f6\u95f4\u6233\u8d85\u65f6\u673a\u5236\u62e6\u622a\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u8981\u6c42sign\u7684\u8d85\u65f6\u65f6\u95f4\u8981\u8bbe\u5b9a\u4e3a\u8ddf\u65f6\u95f4\u6233\u7684\u8d85\u65f6\u65f6\u95f4\u4e00\u81f4\u3002\u62d2\u7edd\u91cd\u590d\u8c03\u7528\u673a\u5236\u786e\u4fddURL\u88ab\u522b\u4eba\u622a\u83b7\u4e86\u4e5f\u65e0\u6cd5\u4f7f\u7528\uff08\u5982\u6293\u53d6\u6570\u636e\uff09<\/p>\n<p><strong>\u65b9\u6848\u6d41\u7a0b<\/strong><\/p>\n<ul>\n<li>\u5ba2\u6237\u7aef\u901a\u8fc7\u7528\u6237\u540d\u5bc6\u7801\u767b\u5f55\u670d\u52a1\u5668\u5e76\u83b7\u53d6Token\uff1b<\/li>\n<li>\u5ba2\u6237\u7aef\u751f\u6210\u65f6\u95f4\u6233timestamp\uff0c\u5e76\u5c06timestamp\u4f5c\u4e3a\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\uff1b<\/li>\n<li>\u5ba2\u6237\u7aef\u5c06\u6240\u6709\u7684\u53c2\u6570\uff0c\u5305\u62ecToken\u548ctimestamp\u6309\u7167\u81ea\u5df1\u7684\u7b7e\u540d\u7b97\u6cd5\u8fdb\u884c\u6392\u5e8f\u52a0\u5bc6\u5f97\u5230\u7b7e\u540dsign<\/li>\n<li>\u5c06token\u3001timestamp\u548csign\u4f5c\u4e3a\u8bf7\u6c42\u65f6\u5fc5\u987b\u643a\u5e26\u7684\u53c2\u6570\u52a0\u5728\u6bcf\u4e2a\u8bf7\u6c42\u7684URL\u540e\u8fb9\uff0c\u4f8b\uff1a<code>http:\/\/url\/request?token=h40adc3949bafjhbbe56e027f20f583a&amp;timetamp=1559396263&amp;sign=e10adc3949ba59abbe56e057f20f883e<\/code><\/li>\n<li>\u670d\u52a1\u7aef\u5bf9token\u3001timestamp\u548csign\u8fdb\u884c\u9a8c\u8bc1\uff0c\u53ea\u6709\u5728token\u6709\u6548\u3001timestamp\u672a\u8d85\u65f6\u3001\u7f13\u5b58\u670d\u52a1\u5668\u4e2d\u4e0d\u5b58\u5728sign\u4e09\u79cd\u60c5\u51b5\u540c\u65f6\u6ee1\u8db3\uff0c\u672c\u6b21\u8bf7\u6c42\u624d\u6709\u6548\uff1b<\/li>\n<\/ul>\n<h3>6\u3001\u91c7\u7528HTTPS\u901a\u4fe1\u534f\u8bae<\/h3>\n<p>\u5b89\u5168\u5957\u63a5\u5b57\u5c42\u8d85\u6587\u672c\u4f20\u8f93\u534f\u8baeHTTPS\uff0c\u4e3a\u4e86\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\uff0cHTTPS\u5728HTTP\u7684\u57fa\u7840\u4e0a\u52a0\u5165\u4e86SSL\u534f\u8bae\uff0cSSL\u4f9d\u9760\u8bc1\u4e66\u6765\u9a8c\u8bc1\u670d\u52a1\u5668\u7684\u8eab\u4efd\uff0c\u5e76\u4e3a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u901a\u4fe1\u52a0\u5bc6\u3002<\/p>\n<p>HTTPS\u4e5f\u4e0d\u662f\u7edd\u5bf9\u5b89\u5168\u7684\uff0c\u6bd4\u5982\u4e2d\u95f4\u4eba\u52ab\u6301\u653b\u51fb\uff0c\u4e2d\u95f4\u4eba\u53ef\u4ee5\u83b7\u53d6\u5230\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u4e4b\u95f4\u6240\u6709\u7684\u901a\u4fe1\u5185\u5bb9<\/p>\n<h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<p>\u81ea\u6b64\u6574\u4e2a\u540e\u7aef\u63a5\u53e3\u57fa\u672c\u4f53\u7cfb\u5c31\u6784\u5efa\u5b8c\u6bd5\u4e86<\/p>\n<ul>\n<li>\u901a\u8fc7Validator + \u81ea\u52a8\u629b\u51fa\u5f02\u5e38\u6765\u5b8c\u6210\u4e86\u65b9\u4fbf\u7684\u53c2\u6570\u6821\u9a8c<\/li>\n<li>\u901a\u8fc7\u5168\u5c40\u5f02\u5e38\u5904\u7406 + \u81ea\u5b9a\u4e49\u5f02\u5e38\u5b8c\u6210\u4e86\u5f02\u5e38\u64cd\u4f5c\u7684\u89c4\u8303<\/li>\n<li>\u901a\u8fc7\u6570\u636e\u7edf\u4e00\u54cd\u5e94\u5b8c\u6210\u4e86\u54cd\u5e94\u6570\u636e\u7684\u89c4\u8303<\/li>\n<li>\u591a\u4e2a\u65b9\u9762\u7ec4\u88c5\u975e\u5e38\u4f18\u96c5\u7684\u5b8c\u6210\u4e86\u540e\u7aef\u63a5\u53e3\u7684\u534f\u8c03\uff0c\u8ba9\u5f00\u53d1\u4eba\u5458\u6709\u66f4\u591a\u7684\u7ecf\u5386\u6ce8\u91cd\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\uff0c\u8f7b\u677e\u6784\u5efa\u540e\u7aef\u63a5\u53e3<\/li>\n<\/ul>\n<p><strong>\u8fd9\u91cc\u518d\u8bf4\u51e0\u70b9<\/strong><\/p>\n<ul>\n<li>controller\u505a\u597dtry-catch\u5de5\u4f5c\uff0c\u53ca\u65f6\u6355\u83b7\u5f02\u5e38\uff0c\u53ef\u4ee5\u518d\u6b21\u629b\u51fa\u5230\u5168\u5c40\uff0c\u7edf\u4e00\u683c\u5f0f\u8fd4\u56de\u524d\u7aef<\/li>\n<li>\u505a\u597d\u65e5\u5fd7\u7cfb\u7edf\uff0c\u5173\u952e\u4f4d\u7f6e\u4e00\u5b9a\u8981\u6709\u65e5\u5fd7<\/li>\n<li>\u505a\u597d\u5168\u5c40\u7edf\u4e00\u8fd4\u56de\u7c7b\uff0c\u6574\u4e2a\u9879\u76ee\u89c4\u8303\u597d\u5b9a\u4e49\u597d<\/li>\n<li>controller\u5165\u53c2\u5b57\u6bb5\u53ef\u4ee5\u62bd\u8c61\u51fa\u4e00\u4e2a\u516c\u5171\u57fa\u7c7b\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u7ee7\u627f\u6269\u5145<\/li>\n<li>controller\u5c42\u505a\u597d\u5165\u53c2\u53c2\u6570\u6821\u9a8c<\/li>\n<li>\u63a5\u53e3\u5b89\u5168\u9a8c\u8bc1<\/li>\n<\/ul>\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<\/p>\n<p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=OzyC6Nxwt%2BTjE3LHpiWzKQ%3D%3D.myA626UY93v2nzavM8Vu0H4TEx9d8WKmPkVLWSdIgUVrzAvImFUB%2B7AlVPYTLA8bWAbb1vdFlfhKJrtEro4iQQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/blog.csdn.net\/xingfuzhijianxia\/article\/details\/87623903<\/a><\/p>\n<p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=H63v9rb1%2F3Q1aplleIEvmw%3D%3D.L3j6PjSicUs6U%2FqM0c3TwpJrnkBmse0IpCovdODpNRCx0CFTN%2FW%2BvL5C33W%2BvRxs\" rel=\"noopener noreferrer\">https:\/\/www.ithere.net\/article\/405<\/a><\/p>\n<p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=xkkQwldP5w38X4PxwlA3UA%3D%3D.No%2FYuCGLw9oMTP2zk6ipBvPpbdoSpkZZoebFH9SX%2BJZY%2Bn5iwlG3QnswOzjie5AO\" rel=\"noopener noreferrer\">https:\/\/juejin.cn\/post\/6887019320666161165<\/a><\/p>\n<p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=5gC0uOi6xv3tued3cwgy3w%3D%3D.2y29vTuzonPxDclvqMgqLqNAJrTxyxMFZmE8OPLOv5w%3D\" rel=\"noopener noreferrer\">http:\/\/learn.lianglianglee.com\/<\/a><\/p>\n<blockquote><p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300c\u9b45Lemon\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\u539f\u6587\u94fe\u63a5\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=ihCXqqFPKyzy1VROlqy2Sw%3D%3D.CTEwqS2VJ%2B4bKBKMpPxWS9XFfodT9I%2FC0OfL%2F2qD1joM14UqeqCzktfb5iwctyr%2BHspKbWW%2BTyGs9Cx%2BBOgmSw%3D%3D\" rel=\"noopener noreferrer\">https:\/\/blog.csdn.net\/lemon_TT\/article\/details\/108309900<\/a><\/p><\/blockquote>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=N2%2F1lAAZH6NsQJmMqvZmkg%3D%3D.tIjyJ%2Bbhp2AXcmeyYthlC6qDcwj5HH62h8WPQyvDJeY%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=ddorc%2BKrLdZHFSTUy%2FEyQA%3D%3D.nkKiR0mM%2FlLJCMY823djPvIaZBhNF%2F%2FVNlV8TBnZvsqJ%2F3tZ2aJUQBUjWvmNDogYk4OgjSlqO18p%2FS8XP%2Bnw%2BQ%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=QgELnA36tdcxwlPxMHt%2Fsw%3D%3D.teYWxMMnOE6EVUdkf%2FcZbbCUT%2FLYFbY2Aia9igjv8339Z9XhXsE19%2FR74ggZ1SRJ\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=qEkuebnb%2BkB6auk2Ag9kfQ%3D%3D.oLKBY4WSdwnBJBS3d0g4YaEv9uNhnriQhQGcr4a6SeOqWnW1fTvBObXRyFUktorEyWYLwarf3N3pT6sQM1%2BjIA%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=6%2B2HIOszEYx0ZAg7tqPhjQ%3D%3D.KUDqdDgWi9dbinfg5VqeMyBFi%2BbMDm8YGeArKzvFShwejm%2FsC5XOhY2qx5fYJqPrwhB%2Bcxrf6FdWuN5%2FKtoP8qoYDcfxq1nm52k9Vy%2B%2Fb84%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044140544\">https:\/\/segmentfault.com\/a\/1190000044140544<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u524d\u8a00 \u4e00\u4e2a\u540e\u7aef\u63a5\u53e3\u5927\u81f4\u5206\u4e3a\u56db\u4e2a\u90e8\u5206\u7ec4\u6210\uff1a\u63a5\u53e3\u5730\u5740\uff08url\uff09\u3001\u63a5\u53e3\u8bf7\u6c42\u65b9\u5f0f\uff08get\u3001post\u7b49\uff09\u3001\u8bf7\u6c42\u6570\u636e\uff08 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008147","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008147"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008147\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}