{"id":203007,"date":"2025-05-22T11:51:12","date_gmt":"2025-05-22T03:51:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203007\/"},"modified":"2025-05-22T11:51:12","modified_gmt":"2025-05-22T03:51:12","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e8%87%aa%e5%ae%9a%e4%b9%89%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203007\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p>\u5728 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\u5b58\u5728\u4e09\u79cd\u65b9\u6cd5\uff1atry-catch \u5757\u7528\u4e8e\u76f4\u63a5\u6355\u83b7\u5f02\u5e38\uff1bEither \u7c7b\u578b\u7528\u4e8e\u4f18\u96c5\u5730\u8868\u793a\u6210\u529f\u6216\u5931\u8d25\uff1bFunction.biFunction() \u5141\u8bb8\u5b9a\u4e49\u51fd\u6570\u65e2\u63a5\u53d7\u8f93\u5165\u53c8\u63a5\u53d7\u5f02\u5e38\u5904\u7406\u5668\u51fd\u6570\u3002\u5b9e\u6218\u6848\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528 Either \u7c7b\u578b\u4f18\u96c5\u5730\u5904\u7406 JSON \u89e3\u6790\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730018503671dfcc72429a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u4f7f\u7528\u5f02\u5e38\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6311\u6218\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\u65f6\u3002\u672c\u6587\u5c06\u8ba8\u8bba\u4f7f\u7528 Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u4e0d\u540c\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u4f7f\u7528 <code>try-catch<\/code> \u5757<\/strong><\/p>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u5728\u51fd\u6570\u4e2d\u4f7f\u7528 <code>try-catch<\/code> \u5757\u6355\u83b7\u5f02\u5e38\uff1a<\/p>\n<pre>\/\/ Function accepts an argument and returns an Optional\nFunction&lt;String, Optional&lt;Integer&gt;&gt; parseInteger = s -&gt; {\n    try {\n        return Optional.of(Integer.parseInt(s));\n    } catch (NumberFormatException e) {\n        return Optional.empty();\n    }\n};<\/pre>\n<p><strong>\u4f7f\u7528 <code>Either<\/code> \u7c7b\u578b<\/strong><\/p>\n<p><code>Either<\/code> \u7c7b\u578b\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u6cd5\u6765\u8868\u793a\u6210\u529f\u7684\u8c13\u8bcd\u548c\u5931\u8d25\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Either&lt;String, Integer&gt;<\/code> \u7c7b\u578b\u8868\u793a\u6210\u529f\u89e3\u6790\u7684\u6574\u6570\u6216\u89e3\u6790\u5931\u8d25\u7684\u9519\u8bef\u6d88\u606f\uff1a<\/p>\n<pre>\/\/ Function accepts an argument and returns an Either\nFunction&lt;String, Either&lt;String, Integer&gt;&gt; parseInteger = s -&gt; {\n    try {\n        return Either.right(Integer.parseInt(s));\n    } catch (NumberFormatException e) {\n        return Either.left(\"Invalid number: \" + s);\n    }\n};<\/pre>\n<p><strong>\u4f7f\u7528 <code>Function.biFunction()<\/code><\/strong><\/p>\n<p><code>Function.biFunction()<\/code> \u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u65e2\u63a5\u53d7\u8f93\u5165\u503c\u53c8\u63a5\u53d7\u5f02\u5e38\u5904\u7406\u5668\u51fd\u6570\u3002\u5f02\u5e38\u5904\u7406\u5668\u51fd\u6570\u53ef\u4ee5\u5904\u7406\u629b\u51fa\u7684\u5f02\u5e38\u5e76\u8fd4\u56de\u4e00\u4e2a\u7ed3\u679c\uff1a<\/p>\n<pre>\/\/ Function accepts an argument and an exception handler\nBiFunction&lt;String, Function&lt;Throwable, String&gt;, String&gt; parseInteger = (s, handler) -&gt; {\n    try {\n        return Integer.parseInt(s);\n    } catch (Throwable t) {\n        return handler.apply(t);\n    }\n};<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u5bf9\u8c61\u7684\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u80fd\u4f1a\u629b\u51fa <code>ParseException<\/code> \u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>Either<\/code> \u7c7b\u578b\u6765\u5904\u7406\u89e3\u6790\u7ed3\u679c\uff1a<\/p>\n<pre>Function&lt;String, Either&lt;ParseException, Person&gt;&gt; parsePerson = json -&gt; {\n    try {\n        return Either.right(new ObjectMapper().readValue(json, Person.class));\n    } catch (ParseException e) {\n        return Either.left(e);\n    }\n};<\/pre>\n<p>\u4f7f\u7528 <code>Either<\/code> \u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u5f0f\u7ba1\u9053\u4e2d\u4f18\u96c5\u5730\u5904\u7406\u89e3\u6790\u6210\u529f\u6216\u5931\u8d25\u7684\u60c5\u51b5\uff1a<\/p>\n<pre>String json = \"...\";\nEither&lt;ParseException, Person&gt; result = parsePerson.apply(json);\nresult.ifRight(person -&gt; System.out.println(\"Parsed person: \" + person))\n      .ifLeft(error -&gt; System.out.println(\"Error parsing person: \" + error.getMessage()));<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u5982\u4f55\u5904\u7406\u81ea\u5b9a\u4e49&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203007","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203007"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203007\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}