{"id":10006518,"date":"2022-09-19T15:13:31","date_gmt":"2022-09-19T07:13:31","guid":{"rendered":"https:\/\/ityet.com\/?p=10006518"},"modified":"2022-09-19T15:13:31","modified_gmt":"2022-09-19T07:13:31","slug":"%e5%be%ae%e6%9c%8d%e5%8a%a1%e5%bc%80%e5%8f%91%e7%b3%bb%e5%88%97%ef%bc%9a%e5%88%a9%e7%94%a8%e5%bc%82%e5%b8%b8%e7%89%b9%e6%80%a7%ef%bc%8c%e6%8a%8a%e5%bc%82%e5%b8%b8%e7%ba%b3%e5%85%a5%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006518","title":{"rendered":"\u5fae\u670d\u52a1\u5f00\u53d1\u7cfb\u5217\uff1a\u5229\u7528\u5f02\u5e38\u7279\u6027\uff0c\u628a\u5f02\u5e38\u7eb3\u5165\u6846\u67b6\u7ba1\u7406\u4e4b\u4e2d"},"content":{"rendered":"<p><a href=\"https:\/\/link.segmentfault.com\/?enc=xKnZmmVnxpEs1WrdTqkSOQ%3D%3D.yxVeOkTjqZ%2Bd73DPA0k2ukyiid9IQc4EWURMKcpkqVa427AC1n9KFOC8yz5RC1DI98PGS1q1ICbWe5jDDp5%2FwA%3D%3D\" rel=\"nofollow\">\u6e90\u7801\u5730\u5740<\/a><\/p>\n<p>\u5728\u7cfb\u7edf\u4e2d\uff0c\u5f02\u5e38\u88ab\u5206\u4e3a\u4e86\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u5df2\u77e5\u5f02\u5e38\uff0c\u4e00\u79cd\u662f\u672a\u77e5\u5f02\u5e38\u3002<\/p>\n<p>\u5df2\u77e5\u5f02\u5e38\u5728\u6846\u67b6\u4e2d\u88ab\u5c01\u88c5\u4e3a <code>cn.vte.framework.common.http.InnerExp<\/code>\uff0c\u4e00\u822c\u4e3a\u4e1a\u52a1\u5f02\u5e38\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u63a5\u53e3\u8bbf\u95ee\u8303\u56f4\u5185\u7684\u4ee3\u7801\u5185\u629b\u51fa\u3002<\/p>\n<h2>InnerExp \u4f5c\u7528<\/h2>\n<p><code>InnerExp<\/code> \u662f\u4e3a\u4e86\u80fd\u591f\u7075\u6d3b\u8fd4\u56de\u4fe1\u606f\u5230\u524d\u7aef\uff0c\u5f53\u65b9\u6cd5\u7684\u8c03\u7528\u8fc7\u591a\u65f6\u6216\u8005\u903b\u8f91\u5d4c\u5957\u8fc7\u6df1\uff0c\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\uff0c\u4e0d\u7528\u4e00\u5c42\u4e00\u5c42\u8fd4\u56de\uff0c\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\u5373\u53ef\u628a\u4fe1\u606f\u8fd4\u56de\u3002<\/p>\n<p>\u914d\u5408 <code>GlobalMvcExceptionHandler<\/code>\u00a0\u4f7f\u7528\uff0c\u8be5\u7c7b\u62e6\u622a\u4e86\u6240\u6709\u7684 mvc http \u63a5\u53e3\u5f02\u5e38\uff0c\u4efb\u4f55\u5f02\u5e38\u90fd\u4f1a\u88ab\u5904\u7406\u6210 RequestResult \u7ed3\u6784\u3002<\/p>\n<p><code>InnerExp<\/code>\u00a0\u7279\u522b\u7684\u662f\uff0c\u5b83\u4f1a\u643a\u5e26\u4e24\u79cd\u4fe1\u606f\uff0c\u4e00\u4e2a\u662f <code>RequestResult<\/code> \u8fd8\u6709\u4e00\u4e2a\u662f <code>Exception<\/code>\u3002\u8fd9\u8ba9 <code>InnerExp<\/code> \u6709\u66f4\u52a0\u7075\u6d3b\u7684\u529f\u80fd\u3002<\/p>\n<p><code>InnerExp<\/code> \u643a\u5e26 <code>RequestResult<\/code> \u80fd\u591f\u8ba9\u8fd4\u56de\u7684\u7ed3\u679c\u66f4\u52a0\u4e30\u5bcc\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u81ea\u5b9a\u4e49 <code>RequestResult<\/code> \u7684\u5185\u5bb9\uff0c\u751a\u81f3\u4e0e\u8fd4\u56de\u6b63\u5e38\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u6bd4\u5982\u5f53\u4f60\u4f7f\u7528\u9012\u5f52\u8c03\u7528\u65f6\uff0c\u6ee1\u8db3\u6761\u4ef6\u6536\u96c6\u5230\u4e86\u8db3\u591f\u7684\u4fe1\u606f\u65f6\uff0c\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u76f4\u63a5\u628a\u7ed3\u679c\u8fd4\u56de\uff0c\u6240\u4ee5\u5f02\u5e38\u7684\u4f7f\u7528\u4e0d\u5b8c\u5168\u662f\u4e3a\u4e86\u901a\u77e5\u7a0b\u5e8f\u5f02\u5e38\u4e86\uff0c\u7075\u6d3b\u4f7f\u7528\u4e5f\u80fd\u591f\u5904\u7406\u6b63\u5e38\u7684\u4e1a\u52a1\u3002<\/p>\n<p><code>InnerExp<\/code> \u643a\u5e26 <code>Exception<\/code> \u65f6\uff0c<code>GlobalMvcExceptionHandler<\/code> \u4f1a\u5224\u65ad cause \u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\u5c31\u76f4\u63a5\u6253\u5370\u5806\u6808\u4fe1\u606f\uff0c\u5e2e\u52a9\u6392\u67e5\u95ee\u9898\uff0c\u5728\u4e1a\u52a1\u4ee3\u7801\u9047\u89c1\u5f02\u5e38\uff0c\u5e76\u4e14\u4e0d\u60f3\u5904\u7406\u65f6\uff0c\u76f4\u63a5\u4ea4\u7ed9\u6700\u4e0a\u5c42\u53bb\u505a\uff0c\u6709\u4e9b\u65f6\u5019\u4f1a\u66f4\u52a0\u65b9\u4fbf\u3002<\/p>\n<p>\u603b\u7ed3\u4e00\u4e0b <code>InnerExp<\/code>\u00a0\u7684\u529f\u80fd\uff1a<\/p>\n<ol>\n<li>\u5feb\u901f\u629b\u51fa\u5f02\u5e38\uff0c\u5c06\u4fe1\u606f\u8fd4\u56de\u7ed9\u524d\u53f0\uff0c\u51fd\u6570\u5d4c\u5957\u7684\u60c5\u51b5\u4e0b\u4e0d\u7528\u5c42\u5c42\u4f20\u9012\u4fe1\u606f\uff0c\u5e76\u4e14\u80fd\u591f\u643a\u5e26\u771f\u5b9e\u5f02\u5e38\uff0c\u9876\u5c42\u6253\u5370\u5806\u6808\u4fe1\u606f\u3002<\/li>\n<li>\u5feb\u901f\u8fd4\u56de\u4e1a\u52a1\u4fe1\u606f\uff0c\u540c\u6837\u4e0d\u7528\u5c42\u5c42\u4f20\u9012\u3002<\/li>\n<li>\u7ee7\u627f <code>Supplier<\/code> \u63a5\u53e3\uff0c\u5728\u6d41\u5f0f\u5904\u7406\u4e2d\u66f4\u52a0\u65b9\u4fbf\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<li>\u5229\u7528 <code>ifThr<\/code> \u4f20\u5165\u6761\u4ef6\u5224\u65ad\u662f\u5426\u5e94\u8be5\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<h2>\u5904\u7406\u5176\u5b83\u5f02\u5e38<\/h2>\n<p>\u5728\u5df2\u77e5\u5f02\u5e38\u4e4b\u5916\uff0c\u5176\u5b83\u5f02\u5e38\u60c5\u51b5\u7684\u5904\u7406\u5c31\u76f8\u5bf9\u68d8\u624b\u4e00\u4e9b\uff0c\u51fa\u73b0\u4ec0\u4e48\u79cd\u7c7b\u7684\u5f02\u5e38\u662f\u5b8c\u5168\u672a\u77e5\u7684\u3002<\/p>\n<p>\u4f60\u53ea\u80fd\u5728\u53d1\u751f\u67d0\u4e9b\u5f02\u5e38\u7684\u65f6\u5019\u5904\u7406\u3002<\/p>\n<p>\u53ef\u80fd\u4f60\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u90fd\u5f53\u6210\u4e00\u79cd\u60c5\u51b5\u5904\u7406\u5462\uff1f\u53ea\u8981\u53d1\u751f\u5f02\u5e38\u4e00\u5f8b\u544a\u77e5\u5185\u90e8\u5f02\u5e38\u3002<\/p>\n<p>\u8fd9\u6837\u5904\u7406\u867d\u7136\u7b80\u5355\uff0c\u4f46\u662f\u5f88\u591a\u5f02\u5e38\u5b9e\u9645\u4e0a\u4e0d\u662f\u540e\u7aef\u903b\u8f91\u9519\u8bef\u5f15\u64ce\u7684\uff0c\u66f4\u591a\u7684\u662f\u6570\u636e\u6821\u9a8c\u65b9\u9762\u7684\u5f15\u8d77\u7684\uff0c\u4e00\u822c\u662f\u7531\u4e8e\u524d\u6bb5\u53d1\u9001\u6570\u636e\u4e0d\u7b26\u5408\u540e\u7aef\u7684\u8981\u6c42\u3002<\/p>\n<p>\u6b64\u65f6\uff0c\u4f60\u5c31\u8981\u5904\u7406\u8fd9\u79cd\u7528\u5f02\u5e38\uff0c\u628a\u4ed6\u4eec\u6574\u7406\u6210\u524d\u7aef\u5f00\u53d1\u4eba\u5458\uff0c\u751a\u81f3\u662f\u7528\u6237\u80fd\u591f\u770b\u61c2\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u6846\u67b6\u4e2d\uff0c\u6240\u9047\u89c1\u8fc7\u7684\u4e00\u4e9b\u5f02\u5e38\u7c7b\u578b\u5904\u7406\u3002<\/p>\n<h3>1 ServiceUnavailable<\/h3>\n<p>\u7531\u4e8e feign \u8c03\u7528\u4e0d\u5230\u7684\u4ea7\u751f\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u6846\u67b6\u4f1a\u8fd4\u56de\u201d<strong>\u540e\u53f0\u670d\u52a1\u672a\u542f\u52a8<\/strong>\u201d\u7684\u63d0\u9192\u3002<\/p>\n<h3>2 HttpMessageNotReadableException<\/h3>\n<p>\u7531\u4e8e\u524d\u7aef\u4f20\u5165\u7684 json \u6570\u636e\uff0c\u65e0\u6cd5\u6b63\u5e38\u5e8f\u5217\u5316\u4e3a java \u5bf9\u8c61\u4ea7\u751f\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u6b64\u5f02\u5e38\u5904\u7406\u7684\u60c5\u51b5\u8f83\u4e3a\u590d\u6742\uff0c\u56e0\u4e3a\u5904\u7406\u5f02\u5e38\u672c\u8eab\u4e0d\u91cd\u8981\uff0c\u66f4\u91cd\u8981\u7684\u662f\u5904\u7406\u5b83\u643a\u5e26\u7684 <code>cause<\/code>\u3002<\/p>\n<p>\u6846\u67b6\u4e2d\u5904\u7406\u4e86\u56db\u79cd <code>cause<\/code> \uff1a<\/p>\n<ol>\n<li><code>InvalidFormatException<\/code><\/li>\n<li><code>MissingKotlinParameterException<\/code><\/li>\n<li><code>JsonMappingException<\/code><\/li>\n<li>\u5176\u5b83\u5f02\u5e38<\/li>\n<\/ol>\n<p>1\u30012\u30013\uff0c\u5b9e\u9645\u4e0a\u90fd\u4e3a jackson \u6240\u629b\u51fa\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u6b64\u5904\uff0c<strong>\u53c8\u662f jackson \u7684\u4e00\u5927\u4eae\u70b9\uff0c\u90a3\u5c31\u662f\u4f60\u80fd\u591f\u901a\u8fc7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u6765\u6355\u6349\u5230 json \u5e8f\u5217\u5316\u7684\u9519\u8bef\u4f4d\u7f6e\u3002<\/strong><\/p>\n<p>\u56e0\u4e3a\u8fd9\u4e9b\u5f02\u5e38\uff0c\u90fd\u643a\u5e26\u6709 <code>getPath()<\/code> \u65b9\u6cd5\uff0c\u80fd\u591f\u627e\u5230\u5d4c\u5957\u7684 json \u9519\u8bef\u8def\u5f84\uff0c\u4e8e\u662f\u6846\u67b6\u4e2d\u5229\u7528\u4e86\u8fd9\u4e00\u70b9\uff0c\u6765\u83b7\u53d6 jackson \u5e8f\u5217\u5316\u9519\u8bef\u7684\u8be6\u7ec6\u5b57\u6bb5\u4fe1\u606f<\/p>\n<pre><code class=\"kotlin\">fun List&lt;JsonMappingException.Reference&gt;.toPath(): String {\n    val sb = StringBuilder()\n    this.mapIndexed { index, reference -&gt;\n        sb.append(\n            if (index == 0 || reference.fieldName == null) {\n                reference.fieldName ?: &quot;[${reference.index}]&quot;\n            } else {\n                &quot;.${reference.fieldName}&quot;\n            }\n        )\n    }\n    return sb.toString()\n}<\/code><\/pre>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4e0b\u9762\u7684\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u4f1a\u8f93\u51fa\uff0c<code>a.type<\/code>\uff0c\u6765\u544a\u77e5\u524d\u7aef\uff0c<code>a<\/code> \u5bf9\u8c61\u4e0b\u7684 <code>type<\/code> \u7f3a\u5931\u3002<\/p>\n<pre><code class=\"kotlin\">data class A(val name: String, val type: String)\ndata class B(val name: String, val a: A)\n\nval test = JacksonObject().put(&quot;name&quot;, &quot;b&quot;).put(&quot;a&quot;, JacksonObject().put(&quot;name&quot;, &quot;a&quot;))\ntry {\n    test.convert&lt;B&gt;()\n} catch (e: IllegalArgumentException) {\n    val cause = e.cause\n    if (cause is MissingKotlinParameterException) {\n        println(cause.path.toPath())\n    }\n}<\/code><\/pre>\n<p>\u9664\u4e86\u4e0a\u9762\u7684\u5904\u7406 <code>JsonMappingException<\/code> \u8fd8\u8981\u5355\u72ec\u62ff\u51fa\u6765\u5904\u7406\uff0c\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u8bf4\u660e\u4e86\u5904\u7406\u7684\u601d\u8def<\/p>\n<pre><code class=\"kotlin\">is JsonMappingException -&gt; {\n    val subCause = cause.cause\n    if (subCause is InnerExp) {\n        \/\/ \u53d1\u751f\u4e8e class init \u629b\u51fa\u5f02\u5e38\n        subCause.result\n    } else if (subCause is DateTimeParseException) {\n        RequestResult.error(&quot;&quot;&quot;\u5b57\u6bb5&quot;{}&quot;\u65f6\u95f4\u503c\u9519\u8bef:&quot;{}&quot;&quot;&quot;&quot;, cause.path.toPath(), subCause.parsedString)\n    } else {\n        log.warn(&quot;{}&quot;, request.requestURI, exception)\n        RequestResult.error(&quot;&quot;&quot;\u5b57\u6bb5&quot;{}&quot;\u503c\u9519\u8bef:&quot;{}&quot;&quot;&quot;&quot;, cause.path.toPath())\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u5904\u7406\u5f02\u5e38\u7684\u65b9\u5f0f\uff0c\u5f88\u660e\u663e\u80fd\u591f\u5927\u5927\u51cf\u5c11\u6392\u67e5\u95ee\u9898\u7684\u96be\u5ea6\u3002<\/p>\n<h3>3 NoHandlerFoundException<\/h3>\n<p>\u5f53\u5730\u5740\u627e\u4e0d\u5230\u4ea7\u751f\u7684\u5f02\u5e38\uff0c\u5982\u679c\u4e0d\u5904\u7406\uff0c\u5c06\u8fd4\u56de 404\u3002<\/p>\n<h3>4 MethodArgumentNotValidException<\/h3>\n<p>\u5f53 spring boot valid \u6821\u9a8c\u5931\u8d25\u4ea7\u751f\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u6846\u67b6\u4e2d\uff0c\u5c06\u4f1a\u83b7\u53d6\u5230\u6821\u9a8c\u5931\u8d25\u7684\u4fe1\u606f\uff0c\u7136\u540e\u4ee5 <code>RequestResult<\/code> \u7684\u65b9\u5f0f\u8fd4\u56de\uff0c\u51cf\u5c11\u524d\u7aef\u7684\u5904\u7406\u5de5\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u5904\u7406\uff0c\u524d\u7aef\u662f\u65e0\u6cd5\u83b7\u53d6\u5230\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f\u7684\uff0c\u90a3\u4e48\u6821\u9a8c\u6240\u80fd\u4ea7\u751f\u7684\u4f5c\u7528\u5c31\u4f1a\u5927\u6253\u6298\u6263\u3002<\/p>\n<blockquote><p>\u672c\u6587\u53c2\u4e0e\u4e86<a href=\"https:\/\/segmentfault.com\/a\/1190000042453662\">\u601d\u5426\u6280\u672f\u5f81\u6587<\/a>\uff0c\u6b22\u8fce\u6b63\u5728\u9605\u8bfb\u7684\u4f60\u4e5f\u52a0\u5165\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042510081\">https:\/\/segmentfault.com\/a\/1190000042510081<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6e90\u7801\u5730\u5740 \u5728\u7cfb\u7edf\u4e2d\uff0c\u5f02\u5e38\u88ab\u5206\u4e3a\u4e86\u4e24\u79cd\uff0c\u4e00\u79cd\u662f\u5df2\u77e5\u5f02\u5e38\uff0c\u4e00\u79cd\u662f\u672a\u77e5\u5f02\u5e38\u3002 \u5df2\u77e5\u5f02\u5e38\u5728\u6846\u67b6\u4e2d\u88ab\u5c01\u88c5\u4e3a cn.vte [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006518","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006518","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"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006518"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006518\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}