{"id":10006989,"date":"2022-12-19T14:45:32","date_gmt":"2022-12-19T06:45:32","guid":{"rendered":"https:\/\/ityet.com\/?p=10006989"},"modified":"2022-12-19T14:45:32","modified_gmt":"2022-12-19T06:45:32","slug":"controller-%e5%b1%82%e4%bb%a3%e7%a0%81%e5%b0%b1%e8%af%a5%e8%bf%99%e4%b9%88%e5%86%99%ef%bc%8c%e7%ae%80%e6%b4%81%e5%8f%88%e4%bc%98%e9%9b%85%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006989","title":{"rendered":"Controller \u5c42\u4ee3\u7801\u5c31\u8be5\u8fd9\u4e48\u5199\uff0c\u7b80\u6d01\u53c8\u4f18\u96c5\uff01"},"content":{"rendered":"<p>\u6765\u6e90\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=j9enagmOKGmKB8m1q%2Babiw%3D%3D.f9zA3dC8lfLMVc3WU%2F54%2Fx4EVz3reuMB1v3kTgy3Zz%2Fulki0LvXKR0pEKwBHelZT\" rel=\"nofollow\">https:\/\/juejin.cn\/post\/712309&#8230;<\/a><\/p>\n<h2><strong>\u4e00\u4e2a\u4f18\u79c0\u7684 Controller \u5c42\u903b\u8f91<\/strong><\/h2>\n<p>\u8bf4\u5230 Controller\uff0c\u76f8\u4fe1\u5927\u5bb6\u90fd\u4e0d\u964c\u751f\uff0c\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u5916\u63d0\u4f9b\u6570\u636e\u63a5\u53e3\u3002\u5b83\u7684\u5b9a\u4f4d\uff0c\u6211\u8ba4\u4e3a\u662f\u300c\u4e0d\u53ef\u6216\u7f3a\u7684\u914d\u89d2\u300d\u3002<\/p>\n<p>\u8bf4\u5b83\u4e0d\u53ef\u6216\u7f3a\u662f\u56e0\u4e3a\u65e0\u8bba\u662f\u4f20\u7edf\u7684\u4e09\u5c42\u67b6\u6784\u8fd8\u662f\u73b0\u5728\u7684 COLA \u67b6\u6784\uff0cController \u5c42\u4f9d\u65e7\u6709\u4e00\u5e2d\u4e4b\u5730\uff0c\u8bf4\u660e\u4ed6\u7684\u5fc5\u8981\u6027\u3002<\/p>\n<p>\u8bf4\u5b83\u662f\u914d\u89d2\u662f\u56e0\u4e3a Controller \u5c42\u7684\u4ee3\u7801\u4e00\u822c\u662f\u4e0d\u8d1f\u8d23\u5177\u4f53\u7684\u903b\u8f91\u4e1a\u52a1\u903b\u8f91\u5b9e\u73b0\uff0c\u4f46\u662f\u5b83\u8d1f\u8d23\u63a5\u6536\u548c\u54cd\u5e94\u8bf7\u6c42\u3002<\/p>\n<h2><strong>\u4ece\u73b0\u72b6\u770b\u95ee\u9898<\/strong><\/h2>\n<p>Controller \u4e3b\u8981\u7684\u5de5\u4f5c\u6709\u4ee5\u4e0b\u51e0\u9879\uff1a<\/p>\n<ul>\n<li>\u63a5\u6536\u8bf7\u6c42\u5e76\u89e3\u6790\u53c2\u6570<\/li>\n<li>\u8c03\u7528 Service \u6267\u884c\u5177\u4f53\u7684\u4e1a\u52a1\u4ee3\u7801\uff08\u53ef\u80fd\u5305\u542b\u53c2\u6570\u6821\u9a8c\uff09<\/li>\n<li>\u6355\u83b7\u4e1a\u52a1\u903b\u8f91\u5f02\u5e38\u505a\u51fa\u53cd\u9988<\/li>\n<li>\u4e1a\u52a1\u903b\u8f91\u6267\u884c\u6210\u529f\u505a\u51fa\u54cd\u5e94<\/li>\n<\/ul>\n<pre><code>\/\/DTO\n@Data\npublic class TestDTO {\n    private Integer num;\n    private String type;\n}\n\n\n\/\/Service\n@Service\npublic class TestService {\n\n    public Double service(TestDTO testDTO) throws Exception {\n        if (testDTO.getNum() &lt;= 0) {\n            throw new Exception(&quot;\u8f93\u5165\u7684\u6570\u5b57\u9700\u8981\u5927\u4e8e0&quot;);\n        }\n        if (testDTO.getType().equals(&quot;square&quot;)) {\n            return Math.pow(testDTO.getNum(), 2);\n        }\n        if (testDTO.getType().equals(&quot;factorial&quot;)) {\n            double result = 1;\n            int num = testDTO.getNum();\n            while (num &gt; 1) {\n                result = result * num;\n                num -= 1;\n            }\n            return result;\n        }\n        throw new Exception(&quot;\u672a\u8bc6\u522b\u7684\u7b97\u6cd5&quot;);\n    }\n}\n\n\n\/\/Controller\n@RestController\npublic class TestController {\n\n    private TestService testService;\n\n    @PostMapping(&quot;\/test&quot;)\n    public Double test(@RequestBody TestDTO testDTO) {\n        try {\n            Double result = this.testService.service(testDTO);\n            return result;\n        } catch (Exception e) {\n            throw new RuntimeException(e);\n        }\n    }\n\n    @Autowired\n    public DTOid setTestService(TestService testService) {\n        this.testService = testService;\n    }\n}<\/code><\/pre>\n<p>\u5982\u679c\u771f\u7684\u6309\u7167\u4e0a\u9762\u6240\u5217\u7684\u5de5\u4f5c\u9879\u6765\u5f00\u53d1 Controller \u4ee3\u7801\u4f1a\u6709\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u53c2\u6570\u6821\u9a8c\u8fc7\u591a\u5730\u8026\u5408\u4e86\u4e1a\u52a1\u4ee3\u7801\uff0c\u8fdd\u80cc\u5355\u4e00\u804c\u8d23\u539f\u5219<\/li>\n<li>\u53ef\u80fd\u5728\u591a\u4e2a\u4e1a\u52a1\u4e2d\u90fd\u629b\u51fa\u540c\u4e00\u4e2a\u5f02\u5e38\uff0c\u5bfc\u81f4\u4ee3\u7801\u91cd\u590d<\/li>\n<li>\u5404\u79cd\u5f02\u5e38\u53cd\u9988\u548c\u6210\u529f\u54cd\u5e94\u683c\u5f0f\u4e0d\u7edf\u4e00\uff0c\u63a5\u53e3\u5bf9\u63a5\u4e0d\u53cb\u597d<\/li>\n<\/ul>\n<h2><strong>\u6539\u9020 Controller \u5c42\u903b\u8f91<\/strong><\/h2>\n<h3>\u7edf\u4e00\u8fd4\u56de\u7ed3\u6784<\/h3>\n<p>\u7edf\u4e00\u8fd4\u56de\u503c\u7c7b\u578b\u65e0\u8bba\u9879\u76ee\u524d\u540e\u7aef\u662f\u5426\u5206\u79bb\u90fd\u662f\u975e\u5e38\u5fc5\u8981\u7684\uff0c\u65b9\u4fbf\u5bf9\u63a5\u63a5\u53e3\u7684\u5f00\u53d1\u4eba\u5458\u66f4\u52a0\u6e05\u6670\u5730\u77e5\u9053\u8fd9\u4e2a\u63a5\u53e3\u7684\u8c03\u7528\u662f\u5426\u6210\u529f\uff08\u4e0d\u80fd\u4ec5\u4ec5\u7b80\u5355\u5730\u770b\u8fd4\u56de\u503c\u662f\u5426\u4e3a null \u5c31\u5224\u65ad\u6210\u529f\u4e0e\u5426\uff0c\u56e0\u4e3a\u6709\u4e9b\u63a5\u53e3\u7684\u8bbe\u8ba1\u5c31\u662f\u5982\u6b64\uff09\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u6700\u5168\u6559\u7a0b\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=heGqWrX7hAeXbcjO8uD4gg%3D%3D.01RCwgtR2hmTF8ZbP0Mu6MJdxjA77MCbRmfenHun463s2i6ykoQ0r3N5TML0Tbl2gOhdSV5%2FkM6z6rKO8XzYiQ%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<p>\u4f7f\u7528\u4e00\u4e2a\u72b6\u6001\u7801\u3001\u72b6\u6001\u4fe1\u606f\u5c31\u80fd\u6e05\u695a\u5730\u4e86\u89e3\u63a5\u53e3\u8c03\u7528\u60c5\u51b5\uff1a<\/p>\n<pre><code>\/\/\u5b9a\u4e49\u8fd4\u56de\u6570\u636e\u7ed3\u6784\npublic interface IResult {\n    Integer getCode();\n    String getMessage();\n}\n\n\/\/\u5e38\u7528\u7ed3\u679c\u7684\u679a\u4e3e\npublic enum ResultEnum implements IResult {\n    SUCCESS(2001, &quot;\u63a5\u53e3\u8c03\u7528\u6210\u529f&quot;),\n    VALIDATE_FAILED(2002, &quot;\u53c2\u6570\u6821\u9a8c\u5931\u8d25&quot;),\n    COMMON_FAILED(2003, &quot;\u63a5\u53e3\u8c03\u7528\u5931\u8d25&quot;),\n    FORBIDDEN(2004, &quot;\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u8d44\u6e90&quot;);\n\n    private Integer code;\n    private String message;\n\n    \/\/\u7701\u7565get\u3001set\u65b9\u6cd5\u548c\u6784\u9020\u65b9\u6cd5\n}\n\n\/\/\u7edf\u4e00\u8fd4\u56de\u6570\u636e\u7ed3\u6784\n@Data\n@NoArgsConstructor\n@AllArgsConstructor\npublic class Result&lt;T&gt; {\n    private Integer code;\n    private String message;\n    private T data;\n\n    public static &lt;T&gt; Result&lt;T&gt; success(T data) {\n        return new Result&lt;&gt;(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);\n    }\n\n    public static &lt;T&gt; Result&lt;T&gt; success(String message, T data) {\n        return new Result&lt;&gt;(ResultEnum.SUCCESS.getCode(), message, data);\n    }\n\n    public static Result&lt;?&gt; failed() {\n        return new Result&lt;&gt;(ResultEnum.COMMON_FAILED.getCode(), ResultEnum.COMMON_FAILED.getMessage(), null);\n    }\n\n    public static Result&lt;?&gt; failed(String message) {\n        return new Result&lt;&gt;(ResultEnum.COMMON_FAILED.getCode(), message, null);\n    }\n\n    public static Result&lt;?&gt; failed(IResult errorResult) {\n        return new Result&lt;&gt;(errorResult.getCode(), errorResult.getMessage(), null);\n    }\n\n    public static &lt;T&gt; Result&lt;T&gt; instance(Integer code, String message, T data) {\n        Result&lt;T&gt; result = new Result&lt;&gt;();\n        result.setCode(code);\n        result.setMessage(message);\n        result.setData(data);\n        return result;\n    }\n}<\/code><\/pre>\n<p>\u7edf\u4e00\u8fd4\u56de\u7ed3\u6784\u540e\uff0c\u5728 Controller \u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\uff0c\u4f46\u662f\u6bcf\u4e00\u4e2a Controller \u90fd\u5199\u8fd9\u4e48\u4e00\u6bb5\u6700\u7ec8\u5c01\u88c5\u7684\u903b\u8f91\uff0c\u8fd9\u4e9b\u90fd\u662f\u5f88\u91cd\u590d\u7684\u5de5\u4f5c\uff0c\u6240\u4ee5\u8fd8\u8981\u7ee7\u7eed\u60f3\u529e\u6cd5\u8fdb\u4e00\u6b65\u5904\u7406\u7edf\u4e00\u8fd4\u56de\u7ed3\u6784\u3002<\/p>\n<h3>\u7edf\u4e00\u5305\u88c5\u5904\u7406<\/h3>\n<p>Spring \u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7c7b ResponseBodyAdvice \uff0c\u80fd\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u4e0a\u8ff0\u9700\u6c42\uff1a<\/p>\n<pre><code>public interface ResponseBodyAdvice&lt;T&gt; {\n    boolean supports(MethodParameter returnType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; converterType);\n\n    @Nullable\n    T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; selectedConverterType, ServerHttpRequest request, ServerHttpResponse response);\n}<\/code><\/pre>\n<p>ResponseBodyAdvice \u662f\u5bf9 Controller \u8fd4\u56de\u7684\u5185\u5bb9\u5728 HttpMessageConverter \u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u4e4b\u524d\u62e6\u622a\uff0c\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u64cd\u4f5c\u540e\uff0c\u518d\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u90a3\u8fd9\u6837\u5c31\u53ef\u4ee5\u628a\u7edf\u4e00\u5305\u88c5\u7684\u5de5\u4f5c\u653e\u5230\u8fd9\u4e2a\u7c7b\u91cc\u9762\uff1a<\/p>\n<ul>\n<li><strong>supports\uff1a<\/strong> \u5224\u65ad\u662f\u5426\u8981\u4ea4\u7ed9 beforeBodyWrite \u65b9\u6cd5\u6267\u884c\uff0cture\uff1a\u9700\u8981\uff1bfalse\uff1a\u4e0d\u9700\u8981<\/li>\n<li><strong>beforeBodyWrite\uff1a<\/strong> \u5bf9 response \u8fdb\u884c\u5177\u4f53\u7684\u5904\u7406<\/li>\n<\/ul>\n<pre><code>\/\/ \u5982\u679c\u5f15\u5165\u4e86swagger\u6216knife4j\u7684\u6587\u6863\u751f\u6210\u7ec4\u4ef6\uff0c\u8fd9\u91cc\u9700\u8981\u4ec5\u626b\u63cf\u81ea\u5df1\u9879\u76ee\u7684\u5305\uff0c\u5426\u5219\u6587\u6863\u65e0\u6cd5\u6b63\u5e38\u751f\u6210\n@RestControllerAdvice(basePackages = &quot;com.example.demo&quot;)\npublic class ResponseAdvice implements ResponseBodyAdvice&lt;Object&gt; {\n    @Override\n    public boolean supports(MethodParameter returnType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; converterType) {\n        \/\/ \u5982\u679c\u4e0d\u9700\u8981\u8fdb\u884c\u5c01\u88c5\u7684\uff0c\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u6821\u9a8c\u624b\u6bb5\uff0c\u6bd4\u5982\u6dfb\u52a0\u6807\u8bb0\u6392\u9664\u7684\u6ce8\u89e3\n        return true;\n    }\n\n\n    @Override\n    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class&lt;? extends HttpMessageConverter&lt;?&gt;&gt; selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {\n        \/\/ \u63d0\u4f9b\u4e00\u5b9a\u7684\u7075\u6d3b\u5ea6\uff0c\u5982\u679cbody\u5df2\u7ecf\u88ab\u5305\u88c5\u4e86\uff0c\u5c31\u4e0d\u8fdb\u884c\u5305\u88c5\n        if (body instanceof Result) {\n            return body;\n        }\n        return Result.success(body);\n    }\n}<\/code><\/pre>\n<p>\u7ecf\u8fc7\u8fd9\u6837\u6539\u9020\uff0c\u65e2\u80fd\u5b9e\u73b0\u5bf9 Controller \u8fd4\u56de\u7684\u6570\u636e\u8fdb\u884c\u7edf\u4e00\u5305\u88c5\uff0c\u53c8\u4e0d\u9700\u8981\u5bf9\u539f\u6709\u4ee3\u7801\u8fdb\u884c\u5927\u91cf\u7684\u6539\u52a8\u3002<\/p>\n<h3>\u53c2\u6570\u6821\u9a8c<\/h3>\n<p>Java API \u7684\u89c4\u8303 JSR303 \u5b9a\u4e49\u4e86\u6821\u9a8c\u7684\u6807\u51c6 validation-api \uff0c\u5176\u4e2d\u4e00\u4e2a\u6bd4\u8f83\u51fa\u540d\u7684\u5b9e\u73b0\u662f hibernate validation\u3002<\/p>\n<p>spring validation \u662f\u5bf9\u5176\u7684\u4e8c\u6b21\u5c01\u88c5\uff0c\u5e38\u7528\u4e8e SpringMVC \u7684\u53c2\u6570\u81ea\u52a8\u6821\u9a8c\uff0c\u53c2\u6570\u6821\u9a8c\u7684\u4ee3\u7801\u5c31\u4e0d\u9700\u8981\u518d\u4e0e\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u8fdb\u884c\u8026\u5408\u4e86\u3002<\/p>\n<h5>\u2460@PathVariable \u548c @RequestParam \u53c2\u6570\u6821\u9a8c<\/h5>\n<p>Get \u8bf7\u6c42\u7684\u53c2\u6570\u63a5\u6536\u4e00\u822c\u4f9d\u8d56\u8fd9\u4e24\u4e2a\u6ce8\u89e3\uff0c\u4f46\u662f\u5904\u4e8e url \u6709\u957f\u5ea6\u9650\u5236\u548c\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\uff0c\u8d85\u8fc7 5 \u4e2a\u53c2\u6570\u5c3d\u91cf\u7528\u5b9e\u4f53\u6765\u4f20\u53c2\u3002<\/p>\n<p>\u5bf9 @PathVariable \u548c @RequestParam \u53c2\u6570\u8fdb\u884c\u6821\u9a8c\u9700\u8981\u5728\u5165\u53c2\u58f0\u660e\u7ea6\u675f\u7684\u6ce8\u89e3\u3002<\/p>\n<p>\u5982\u679c\u6821\u9a8c\u5931\u8d25\uff0c\u4f1a\u629b\u51fa MethodArgumentNotValidException \u5f02\u5e38\u3002<\/p>\n<pre><code>@RestController(value = &quot;prettyTestController&quot;)\n@RequestMapping(&quot;\/pretty&quot;)\npublic class TestController {\n\n    private TestService testService;\n\n    @GetMapping(&quot;\/{num}&quot;)\n    public Integer detail(@PathVariable(&quot;num&quot;) @Min(1) @Max(20) Integer num) {\n        return num * num;\n    }\n\n    @GetMapping(&quot;\/getByEmail&quot;)\n    public TestDTO getByAccount(@RequestParam @NotBlank @Email String email) {\n        TestDTO testDTO = new TestDTO();\n        testDTO.setEmail(email);\n        return testDTO;\n    }\n\n    @Autowired\n    public void setTestService(TestService prettyTestService) {\n        this.testService = prettyTestService;\n    }\n}<\/code><\/pre>\n<h5>\u6821\u9a8c\u539f\u7406<\/h5>\n<p>\u5728 SpringMVC \u4e2d\uff0c\u6709\u4e00\u4e2a\u7c7b\u662f RequestResponseBodyMethodProcessor\uff0c\u8fd9\u4e2a\u7c7b\u6709\u4e24\u4e2a\u4f5c\u7528\uff08\u5b9e\u9645\u4e0a\u53ef\u4ee5\u4ece\u540d\u5b57\u4e0a\u5f97\u5230\u4e00\u70b9\u542f\u53d1\uff09<\/p>\n<ul>\n<li>\u7528\u4e8e\u89e3\u6790 @RequestBody \u6807\u6ce8\u7684\u53c2\u6570<\/li>\n<li>\u5904\u7406 @ResponseBody \u6807\u6ce8\u65b9\u6cd5\u7684\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<p>\u89e3\u6790 @RequestBoyd \u6807\u6ce8\u53c2\u6570\u7684\u65b9\u6cd5\u662f resolveArgument\u3002<\/p>\n<pre><code>public class RequestResponseBodyMethodProcessor extends AbstractMessageConverterMethodProcessor {\n      \/**\n     * Throws MethodArgumentNotValidException if validation fails.\n     * @throws HttpMessageNotReadableException if {@link RequestBody#required()}\n     * is {@code true} and there is no body content or if there is no suitable\n     * converter to read the content with.\n     *\/\n    @Override\n    public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,\n        NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {\n\n      parameter = parameter.nestedIfOptional();\n      \/\/\u628a\u8bf7\u6c42\u6570\u636e\u5c01\u88c5\u6210\u6807\u6ce8\u7684DTO\u5bf9\u8c61\n      Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());\n      String name = Conventions.getVariableNameForParameter(parameter);\n\n      if (binderFactory != null) {\n        WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);\n        if (arg != null) {\n          \/\/\u6267\u884c\u6570\u636e\u6821\u9a8c\n          validateIfApplicable(binder, parameter);\n          \/\/\u5982\u679c\u6821\u9a8c\u4e0d\u901a\u8fc7\uff0c\u5c31\u629b\u51faMethodArgumentNotValidException\u5f02\u5e38\n          \/\/\u5982\u679c\u6211\u4eec\u4e0d\u81ea\u5df1\u6355\u83b7\uff0c\u90a3\u4e48\u6700\u7ec8\u4f1a\u7531DefaultHandlerExceptionResolver\u6355\u83b7\u5904\u7406\n          if (binder.getBindingResult().hasErrors() &amp;&amp; isBindExceptionRequired(binder, parameter)) {\n            throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());\n          }\n        }\n        if (mavContainer != null) {\n          mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());\n        }\n      }\n\n      return adaptArgumentIfNecessary(arg, parameter);\n    }\n}\n\npublic abstract class AbstractMessageConverterMethodArgumentResolver implements HandlerMethodArgumentResolver {\n  \/**\n    * Validate the binding target if applicable.\n    * &lt;p&gt;The default implementation checks for {@code @javax.validation.Valid},\n    * Spring's {@link org.springframework.validation.annotation.Validated},\n    * and custom annotations whose name starts with &quot;Valid&quot;.\n    * @param binder the DataBinder to be used\n    * @param parameter the method parameter descriptor\n    * @since 4.1.5\n    * @see #isBindExceptionRequired\n    *\/\n   protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {\n    \/\/\u83b7\u53d6\u53c2\u6570\u4e0a\u7684\u6240\u6709\u6ce8\u89e3\n      Annotation[] annotations = parameter.getParameterAnnotations();\n      for (Annotation ann : annotations) {\n      \/\/\u5982\u679c\u6ce8\u89e3\u4e2d\u5305\u542b\u4e86@Valid\u3001@Validated\u6216\u8005\u662f\u540d\u5b57\u4ee5Valid\u5f00\u5934\u7684\u6ce8\u89e3\u5c31\u8fdb\u884c\u53c2\u6570\u6821\u9a8c\n         Object[] validationHints = ValidationAnnotationUtils.determineValidationHints(ann);\n         if (validationHints != null) {\n        \/\/\u5b9e\u9645\u6821\u9a8c\u903b\u8f91\uff0c\u6700\u7ec8\u4f1a\u8c03\u7528Hibernate Validator\u6267\u884c\u771f\u6b63\u7684\u6821\u9a8c\n        \/\/\u6240\u4ee5Spring Validation\u662f\u5bf9Hibernate Validation\u7684\u4e8c\u6b21\u5c01\u88c5\n            binder.validate(validationHints);\n            break;\n         }\n      }\n   }\n}<\/code><\/pre>\n<h5>\u2461@RequestBody \u53c2\u6570\u6821\u9a8c<\/h5>\n<p>Post\u3001Put \u8bf7\u6c42\u7684\u53c2\u6570\u63a8\u8350\u4f7f\u7528 @RequestBody \u8bf7\u6c42\u4f53\u53c2\u6570\u3002<\/p>\n<p>\u5bf9 @RequestBody \u53c2\u6570\u8fdb\u884c\u6821\u9a8c\u9700\u8981\u5728 DTO \u5bf9\u8c61\u4e2d\u52a0\u5165\u6821\u9a8c\u6761\u4ef6\u540e\uff0c\u518d\u642d\u914d @Validated \u5373\u53ef\u5b8c\u6210\u81ea\u52a8\u6821\u9a8c\u3002<\/p>\n<p>\u5982\u679c\u6821\u9a8c\u5931\u8d25\uff0c\u4f1a\u629b\u51fa ConstraintViolationException \u5f02\u5e38\u3002<\/p>\n<pre><code>\/\/DTO\n@Data\npublic class TestDTO {\n    @NotBlank\n    private String userName;\n\n    @NotBlank\n    @Length(min = 6, max = 20)\n    private String password;\n\n    @NotNull\n    @Email\n    private String email;\n}\n\n\/\/Controller\n@RestController(value = &quot;prettyTestController&quot;)\n@RequestMapping(&quot;\/pretty&quot;)\npublic class TestController {\n\n    private TestService testService;\n\n    @PostMapping(&quot;\/test-validation&quot;)\n    public void testValidation(@RequestBody @Validated TestDTO testDTO) {\n        this.testService.save(testDTO);\n    }\n\n    @Autowired\n    public void setTestService(TestService testService) {\n        this.testService = testService;\n    }\n}<\/code><\/pre>\n<h5>\u6821\u9a8c\u539f\u7406<\/h5>\n<p>\u58f0\u660e\u7ea6\u675f\u7684\u65b9\u5f0f\uff0c\u6ce8\u89e3\u52a0\u5230\u4e86\u53c2\u6570\u4e0a\u9762\uff0c\u53ef\u4ee5\u6bd4\u8f83\u5bb9\u6613\u731c\u6d4b\u5230\u662f\u4f7f\u7528\u4e86 AOP \u5bf9\u65b9\u6cd5\u8fdb\u884c\u589e\u5f3a\u3002<\/p>\n<p>\u800c\u5b9e\u9645\u4e0a Spring \u4e5f\u662f\u901a\u8fc7 MethodValidationPostProcessor \u52a8\u6001\u6ce8\u518c AOP \u5207\u9762\uff0c\u7136\u540e\u4f7f\u7528 MethodValidationInterceptor \u5bf9\u5207\u70b9\u65b9\u6cd5\u8fdb\u884c\u7ec7\u5165\u589e\u5f3a\u3002<\/p>\n<pre><code>public class MethodValidationPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor implements InitializingBean {\n\n    \/\/\u6307\u5b9a\u4e86\u521b\u5efa\u5207\u9762\u7684Bean\u7684\u6ce8\u89e3\n   private Class&lt;? extends Annotation&gt; validatedAnnotationType = Validated.class;\n\n    @Override\n    public void afterPropertiesSet() {\n        \/\/\u4e3a\u6240\u6709@Validated\u6807\u6ce8\u7684Bean\u521b\u5efa\u5207\u9762\n        Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true);\n        \/\/\u521b\u5efaAdvisor\u8fdb\u884c\u589e\u5f3a\n        this.advisor = new DefaultPointcutAdvisor(pointcut, createMethodValidationAdvice(this.validator));\n    }\n\n    \/\/\u521b\u5efaAdvice\uff0c\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u65b9\u6cd5\u62e6\u622a\u5668\n    protected Advice createMethodValidationAdvice(@Nullable Validator validator) {\n        return (validator != null ? new MethodValidationInterceptor(validator) : new MethodValidationInterceptor());\n    }\n}\n\npublic class MethodValidationInterceptor implements MethodInterceptor {\n    @Override\n    public Object invoke(MethodInvocation invocation) throws Throwable {\n        \/\/\u65e0\u9700\u589e\u5f3a\u7684\u65b9\u6cd5\uff0c\u76f4\u63a5\u8df3\u8fc7\n        if (isFactoryBeanMetadataMethod(invocation.getMethod())) {\n            return invocation.proceed();\n        }\n\n        Class&lt;?&gt;[] groups = determineValidationGroups(invocation);\n        ExecutableValidator execVal = this.validator.forExecutables();\n        Method methodToValidate = invocation.getMethod();\n        Set&lt;ConstraintViolation&lt;Object&gt;&gt; result;\n        try {\n            \/\/\u65b9\u6cd5\u5165\u53c2\u6821\u9a8c\uff0c\u6700\u7ec8\u8fd8\u662f\u59d4\u6258\u7ed9Hibernate Validator\u6765\u6821\u9a8c\n             \/\/\u6240\u4ee5Spring Validation\u662f\u5bf9Hibernate Validation\u7684\u4e8c\u6b21\u5c01\u88c5\n            result = execVal.validateParameters(\n                invocation.getThis(), methodToValidate, invocation.getArguments(), groups);\n        }\n        catch (IllegalArgumentException ex) {\n            ...\n        }\n        \/\/\u6821\u9a8c\u4e0d\u901a\u8fc7\u629b\u51faConstraintViolationException\u5f02\u5e38\n        if (!result.isEmpty()) {\n            throw new ConstraintViolationException(result);\n        }\n        \/\/Controller\u65b9\u6cd5\u8c03\u7528\n        Object returnValue = invocation.proceed();\n        \/\/\u4e0b\u9762\u662f\u5bf9\u8fd4\u56de\u503c\u505a\u6821\u9a8c\uff0c\u6d41\u7a0b\u548c\u4e0a\u9762\u5927\u6982\u4e00\u6837\n        result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);\n        if (!result.isEmpty()) {\n            throw new ConstraintViolationException(result);\n        }\n        return returnValue;\n    }\n}<\/code><\/pre>\n<h5>\u2462\u81ea\u5b9a\u4e49\u6821\u9a8c\u89c4\u5219<\/h5>\n<p>\u6709\u4e9b\u65f6\u5019 JSR303 \u6807\u51c6\u4e2d\u63d0\u4f9b\u7684\u6821\u9a8c\u89c4\u5219\u4e0d\u6ee1\u8db3\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6821\u9a8c\u89c4\u5219\u3002<\/p>\n<p>\u81ea\u5b9a\u4e49\u6821\u9a8c\u89c4\u5219\u9700\u8981\u505a\u4e24\u4ef6\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li>\u81ea\u5b9a\u4e49\u6ce8\u89e3\u7c7b\uff0c\u5b9a\u4e49\u9519\u8bef\u4fe1\u606f\u548c\u4e00\u4e9b\u5176\u4ed6\u9700\u8981\u7684\u5185\u5bb9<\/li>\n<li>\u6ce8\u89e3\u6821\u9a8c\u5668\uff0c\u5b9a\u4e49\u5224\u5b9a\u89c4\u5219<\/li>\n<\/ul>\n<pre><code>\/\/\u81ea\u5b9a\u4e49\u6ce8\u89e3\u7c7b\n@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Constraint(validatedBy = MobileValidator.class)\npublic @interface Mobile {\n    \/**\n     * \u662f\u5426\u5141\u8bb8\u4e3a\u7a7a\n     *\/\n    boolean required() default true;\n\n    \/**\n     * \u6821\u9a8c\u4e0d\u901a\u8fc7\u8fd4\u56de\u7684\u63d0\u793a\u4fe1\u606f\n     *\/\n    String message() default &quot;\u4e0d\u662f\u4e00\u4e2a\u624b\u673a\u53f7\u7801\u683c\u5f0f&quot;;\n\n    \/**\n     * Constraint\u8981\u6c42\u7684\u5c5e\u6027\uff0c\u7528\u4e8e\u5206\u7ec4\u6821\u9a8c\u548c\u6269\u5c55\uff0c\u7559\u7a7a\u5c31\u597d\n     *\/\n    Class&lt;?&gt;[] groups() default {};\n    Class&lt;? extends Payload&gt;[] payload() default {};\n}\n\n\/\/\u6ce8\u89e3\u6821\u9a8c\u5668\npublic class MobileValidator implements ConstraintValidator&lt;Mobile, CharSequence&gt; {\n\n    private boolean required = false;\n\n    private final Pattern pattern = Pattern.compile(&quot;^1[34578][0-9]{9}$&quot;); \/\/ \u9a8c\u8bc1\u624b\u673a\u53f7\n\n    \/**\n     * \u5728\u9a8c\u8bc1\u5f00\u59cb\u524d\u8c03\u7528\u6ce8\u89e3\u91cc\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u83b7\u53d6\u5230\u4e00\u4e9b\u6ce8\u89e3\u91cc\u7684\u53c2\u6570\n     *\n     * @param constraintAnnotation annotation instance for a given constraint declaration\n     *\/\n    @Override\n    public void initialize(Mobile constraintAnnotation) {\n        this.required = constraintAnnotation.required();\n    }\n\n    \/**\n     * \u5224\u65ad\u53c2\u6570\u662f\u5426\u5408\u6cd5\n     *\n     * @param value   object to validate\n     * @param context context in which the constraint is evaluated\n     *\/\n    @Override\n    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {\n        if (this.required) {\n            \/\/ \u9a8c\u8bc1\n            return isMobile(value);\n        }\n        if (StringUtils.hasText(value)) {\n            \/\/ \u9a8c\u8bc1\n            return isMobile(value);\n        }\n        return true;\n    }\n\n    private boolean isMobile(final CharSequence str) {\n        Matcher m = pattern.matcher(str);\n        return m.matches();\n    }\n}<\/code><\/pre>\n<p>\u81ea\u52a8\u6821\u9a8c\u53c2\u6570\u771f\u7684\u662f\u4e00\u9879\u975e\u5e38\u5fc5\u8981\u3001\u975e\u5e38\u6709\u610f\u4e49\u7684\u5de5\u4f5c\u3002JSR303 \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u53c2\u6570\u6821\u9a8c\u89c4\u5219\uff0c\u518d\u52a0\u4e0a\u590d\u6742\u4e1a\u52a1\u7684\u81ea\u5b9a\u4e49\u6821\u9a8c\u89c4\u5219\uff0c\u5b8c\u5168\u628a\u53c2\u6570\u6821\u9a8c\u548c\u4e1a\u52a1\u903b\u8f91\u89e3\u8026\u5f00\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u7b26\u5408\u5355\u4e00\u804c\u8d23\u539f\u5219\u3002<\/p>\n<h3>\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0e\u7edf\u4e00\u62e6\u622a\u5f02\u5e38<\/h3>\n<p>\u539f\u6765\u7684\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\u6709\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u629b\u51fa\u7684\u5f02\u5e38\u4e0d\u591f\u5177\u4f53\uff0c\u53ea\u662f\u7b80\u5355\u5730\u628a\u9519\u8bef\u4fe1\u606f\u653e\u5230\u4e86 Exception \u4e2d<\/li>\n<li>\u629b\u51fa\u5f02\u5e38\u540e\uff0cController \u4e0d\u80fd\u5177\u4f53\u5730\u6839\u636e\u5f02\u5e38\u505a\u51fa\u53cd\u9988<\/li>\n<li>\u867d\u7136\u505a\u4e86\u53c2\u6570\u81ea\u52a8\u6821\u9a8c\uff0c\u4f46\u662f\u5f02\u5e38\u8fd4\u56de\u7ed3\u6784\u548c\u6b63\u5e38\u8fd4\u56de\u7ed3\u6784\u4e0d\u4e00\u81f4<\/li>\n<\/ul>\n<p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u662f\u4e3a\u4e86\u540e\u9762\u7edf\u4e00\u62e6\u622a\u5f02\u5e38\u65f6\uff0c\u5bf9\u4e1a\u52a1\u4e2d\u7684\u5f02\u5e38\u6709\u66f4\u52a0\u7ec6\u9897\u7c92\u5ea6\u7684\u533a\u5206\uff0c\u62e6\u622a\u65f6\u9488\u5bf9\u4e0d\u540c\u7684\u5f02\u5e38\u4f5c\u51fa\u4e0d\u540c\u7684\u54cd\u5e94\u3002<\/p>\n<p>\u800c\u7edf\u4e00\u62e6\u622a\u5f02\u5e38\u7684\u76ee\u7684\u4e00\u4e2a\u662f\u4e3a\u4e86\u53ef\u4ee5\u4e0e\u524d\u9762\u5b9a\u4e49\u4e0b\u6765\u7684\u7edf\u4e00\u5305\u88c5\u8fd4\u56de\u7ed3\u6784\u80fd\u5bf9\u5e94\u4e0a\uff0c\u53e6\u4e00\u4e2a\u662f\u6211\u4eec\u5e0c\u671b\u65e0\u8bba\u7cfb\u7edf\u53d1\u751f\u4ec0\u4e48\u5f02\u5e38\uff0cHttp \u7684\u72b6\u6001\u7801\u90fd\u8981\u662f 200 \uff0c\u5c3d\u53ef\u80fd\u7531\u4e1a\u52a1\u6765\u533a\u5206\u7cfb\u7edf\u7684\u5f02\u5e38\u3002<\/p>\n<pre><code>\/\/\u81ea\u5b9a\u4e49\u5f02\u5e38\npublic class ForbiddenException extends RuntimeException {\n    public ForbiddenException(String message) {\n        super(message);\n    }\n}\n\n\/\/\u81ea\u5b9a\u4e49\u5f02\u5e38\npublic class BusinessException extends RuntimeException {\n    public BusinessException(String message) {\n        super(message);\n    }\n}\n\n\/\/\u7edf\u4e00\u62e6\u622a\u5f02\u5e38\n@RestControllerAdvice(basePackages = &quot;com.example.demo&quot;)\npublic class ExceptionAdvice {\n\n    \/**\n     * \u6355\u83b7 {@code BusinessException} \u5f02\u5e38\n     *\/\n    @ExceptionHandler({BusinessException.class})\n    public Result&lt;?&gt; handleBusinessException(BusinessException ex) {\n        return Result.failed(ex.getMessage());\n    }\n\n    \/**\n     * \u6355\u83b7 {@code ForbiddenException} \u5f02\u5e38\n     *\/\n    @ExceptionHandler({ForbiddenException.class})\n    public Result&lt;?&gt; handleForbiddenException(ForbiddenException ex) {\n        return Result.failed(ResultEnum.FORBIDDEN);\n    }\n\n    \/**\n     * {@code @RequestBody} \u53c2\u6570\u6821\u9a8c\u4e0d\u901a\u8fc7\u65f6\u629b\u51fa\u7684\u5f02\u5e38\u5904\u7406\n     *\/\n    @ExceptionHandler({MethodArgumentNotValidException.class})\n    public Result&lt;?&gt; handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {\n        BindingResult bindingResult = ex.getBindingResult();\n        StringBuilder sb = new StringBuilder(&quot;\u6821\u9a8c\u5931\u8d25:&quot;);\n        for (FieldError fieldError : bindingResult.getFieldErrors()) {\n            sb.append(fieldError.getField()).append(&quot;\uff1a&quot;).append(fieldError.getDefaultMessage()).append(&quot;, &quot;);\n        }\n        String msg = sb.toString();\n        if (StringUtils.hasText(msg)) {\n            return Result.failed(ResultEnum.VALIDATE_FAILED.getCode(), msg);\n        }\n        return Result.failed(ResultEnum.VALIDATE_FAILED);\n    }\n\n    \/**\n     * {@code @PathVariable} \u548c {@code @RequestParam} \u53c2\u6570\u6821\u9a8c\u4e0d\u901a\u8fc7\u65f6\u629b\u51fa\u7684\u5f02\u5e38\u5904\u7406\n     *\/\n    @ExceptionHandler({ConstraintViolationException.class})\n    public Result&lt;?&gt; handleConstraintViolationException(ConstraintViolationException ex) {\n        if (StringUtils.hasText(ex.getMessage())) {\n            return Result.failed(ResultEnum.VALIDATE_FAILED.getCode(), ex.getMessage());\n        }\n        return Result.failed(ResultEnum.VALIDATE_FAILED);\n    }\n\n    \/**\n     * \u9876\u7ea7\u5f02\u5e38\u6355\u83b7\u5e76\u7edf\u4e00\u5904\u7406\uff0c\u5f53\u5176\u4ed6\u5f02\u5e38\u65e0\u6cd5\u5904\u7406\u65f6\u5019\u9009\u62e9\u4f7f\u7528\n     *\/\n    @ExceptionHandler({Exception.class})\n    public Result&lt;?&gt; handle(Exception ex) {\n        return Result.failed(ex.getMessage());\n    }\n\n}<\/code><\/pre>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p>\u505a\u597d\u4e86\u8fd9\u4e00\u5207\u6539\u52a8\u540e\uff0c\u53ef\u4ee5\u53d1\u73b0 Controller \u7684\u4ee3\u7801\u53d8\u5f97\u975e\u5e38\u7b80\u6d01\uff0c\u53ef\u4ee5\u5f88\u6e05\u695a\u5730\u77e5\u9053\u6bcf\u4e00\u4e2a\u53c2\u6570\u3001\u6bcf\u4e00\u4e2a DTO \u7684\u6821\u9a8c\u89c4\u5219\uff0c\u53ef\u4ee5\u5f88\u660e\u786e\u5730\u770b\u5230\u6bcf\u4e00\u4e2a Controller \u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4ec0\u4e48\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u6bcf\u4e00\u4e2a\u5f02\u5e38\u5e94\u8be5\u5982\u4f55\u8fdb\u884c\u53cd\u9988\u3002<\/p>\n<p>\u8fd9\u4e00\u5957\u64cd\u4f5c\u4e0b\u6765\u540e\uff0c\u6211\u4eec\u80fd\u66f4\u52a0\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91\u7684\u5f00\u53d1\uff0c\u4ee3\u7801\u7b80\u4ecb\u3001\u529f\u80fd\u5b8c\u5584\uff0c\u4f55\u4e50\u800c\u4e0d\u4e3a\u5462\uff1f<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=URY0FYDsgIDgUjGMtWa%2F%2Bg%3D%3D.IxXpslqQClWtHLcXsDYAC0ITd2VtzKt7N3UFYINTOvs%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=DHGsV3kAuV6jMs%2BwU6NEtg%3D%3D.YSeJoxmyLK4OdLsySJZeC8htuj%2BJ5%2ForAMp4JyDS%2FjIgEWhxiPV%2FtTq2%2FU5P4gRIVpzXktImE0FGCLiWrTMGsw%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=eKLU3vYBBVx00SnnSqsxWQ%3D%3D.W2i7mDg%2FbFCJGeAdaG%2Fe1mOul71UVFdadkBfooaOj5tFawV66%2B4Z7c6J9EY46Ns9\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=xK0o3ElxsLFCAOKEfY7kQw%3D%3D.TKYHtIOR6W5aJJL9ogFAqNBssBecS7zdmjH6KX%2BLIee5RqR8ysW%2BIFiZv0l%2Fk6eMhlOLNezeR8iBMHBmCiVe8Q%3D%3D\" rel=\"nofollow\">\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 href=\"https:\/\/link.segmentfault.com\/?enc=gQqPYuPJa7ucgCXVbLZJEg%3D%3D.ACwuc1SkyC9rca57loG5ZcehGtMAnue3WyZC0EcaKNbZpgact03s4gNY9SiVHO9uwEAR%2FJfURR92%2F%2FbxVrO0UrQDjMXh8K%2Bp4PV9xTZioq8%3D\" rel=\"nofollow\">\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\/1190000043104742\">https:\/\/segmentfault.com\/a\/1190000043104742<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1ahttps:\/\/juejin.cn\/post\/712309&#8230; \u4e00\u4e2a\u4f18\u79c0\u7684 Controlle [&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-10006989","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\/10006989","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=10006989"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006989\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}