{"id":10021003,"date":"2025-09-16T12:12:14","date_gmt":"2025-09-16T04:12:14","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021003"},"modified":"2025-09-16T12:12:14","modified_gmt":"2025-09-16T04:12:14","slug":"%e7%94%a8-java-%e6%9e%84%e5%bb%ba%e5%81%a5%e5%a3%ae-rest-api-%e7%9a%84-4-%e4%b8%aa%e5%85%b3%e9%94%ae%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021003","title":{"rendered":"\u7528 Java \u6784\u5efa\u5065\u58ee REST API \u7684 4 \u4e2a\u5173\u952e\u6280\u5de7"},"content":{"rendered":"<p>\u60f3\u8ba9\u4f60\u7684 Java REST API \u66f4\u9760\u8c31\uff1f\u5173\u952e\u5c31\u5728<strong>\u7edf\u4e00\u7684\u8d44\u6e90\u547d\u540d<\/strong>\u3001<strong>\u6613\u7ef4\u62a4\u7684\u7248\u672c\u63a7\u5236<\/strong>\u3001<strong>\u624e\u5b9e\u7684\u5b89\u5168\u9632\u62a4<\/strong>\u548c<strong>\u89c4\u8303\u7684\u5f02\u5e38\u5904\u7406<\/strong>\u8fd9\u56db\u4ef6\u4e8b\u4e0a\u3002<\/p>\n<p>\u6784\u5efa\u4e00\u4e2a\u9760\u8c31\u7684 Java REST API\uff0c\u53ef\u4e0d\u6b62\u662f\u61c2\u70b9 HTTP \u8bf7\u6c42\u54cd\u5e94\u90a3\u4e48\u7b80\u5355\u3002\u8bbe\u8ba1\u5408\u7406\u3001\u597d\u7ef4\u62a4\u3001\u591f\u5b89\u5168\u2014\u2014\u8fd9\u4e9b\u624d\u662f\u6838\u5fc3\u3002\u672c\u6587\u5c31\u7ed9\u5927\u5bb6\u5206\u4eab 4 \u4e2a\u5173\u952e\u6280\u5de7\uff0c\u5e2e\u4f60\u628a API \u6253\u78e8\u5f97\u66f4\u4e13\u4e1a\u3002\u4e0d\u8fc7\u6709\u4e2a\u524d\u63d0\uff1a\u4f60\u5f97\u5148\u4e86\u89e3 Richardson \u6210\u719f\u5ea6\u6a21\u578b\uff0c\u81f3\u5c11\u8981\u638c\u63e1\u5230 Level 2\uff08\u8fd9\u662f\u597d API \u7684\u5165\u95e8\u6807\u51c6\uff09\u3002\u8981\u662f\u5fd8\u4e86\u8fd9\u6a21\u578b\u8bb2\u5565\uff0c\u63a8\u8350\u770b\u770b Martin Fowler \u7684\u6587\u7ae0\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=ZN%2BGU%2B%2BH1lN2Rnu5apz9ew%3D%3D.12wv%2Fu%2BogcASsJcvtb1LdZF67%2B8GiieZT%2BZWEvquP%2FSUQmKUHtE96PfoSkXVNKKSvXU%2BTQo8sCDF5gYFe8QfEg%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Richardson Maturity Model<\/a>\u3002<\/p>\n<p>\u4e0d\u591a\u8bf4\uff0c\u54b1\u4eec\u76f4\u63a5\u4e0a\u6280\u5de7\u3002\u4e3a\u4e86\u65b9\u4fbf\u4e3e\u4f8b\uff0c\u54b1\u4eec\u5c31\u62ff\u201c\u63a2\u9669\uff08Expedition\uff09\u201d\u8fd9\u4e2a\u9886\u57df\u6765\u5c55\u5f00\u3002\u4e0d\u7528\u7ea0\u7ed3\u5b9e\u4f53\u548c\u5c42\u7ea7\u7684\u7ec6\u8282\uff0c\u5148\u60f3\u8c61\u54b1\u4eec\u6709\u8fd9\u4e48\u4e2a\u5b9e\u4f53\u7c7b\uff1a<\/p>\n<pre><code class=\"Java\">public class Expedition {\n    private String name;\n    private String location;\n    private LocalDate date;\n\n    public Expedition(String name, String location, LocalDate date) {\n        this.name = name;\n        this.location = location;\n        this.date = date;\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public String getLocation() {\n        return location;\n    }\n\n    public LocalDate getDate() {\n        return date;\n    }\n}<\/code><\/pre>\n<h2>1. \u672f\u8bed\u7edf\u4e00\uff0c\u8d44\u6e90\u547d\u540d\u6709\u8bb2\u7a76<\/h2>\n<p>\u597d\u7684 REST API \u8bbe\u8ba1\uff0c\u7b2c\u4e00\u6b65\u5c31\u662f<strong>\u672f\u8bed\u8981\u7edf\u4e00<\/strong>\uff0c\u670d\u52a1\u7684\u201c\u8bcd\u6c47\u8868\u201d\u5f97\u68b3\u7406\u6e05\u695a\u3002\u5efa\u8bae\u5148\u5b9a\u4e00\u4e2a\u901a\u7528\u7684\u547d\u540d\u89c4\u8303\uff0c\u518d\u6162\u6162\u7ec6\u5316\u5230\u5177\u4f53\u573a\u666f\u3002\u53ef\u4ee5\u8ddf\u7740\u9886\u57df\u9a71\u52a8\u8bbe\u8ba1\uff08DDD\uff09\u7684\u601d\u8def\u6765\uff1a\u5148\u6293\u4e3b\u9886\u57df\uff0c\u518d\u62c6\u5206\u5b50\u9886\u57df\u3002<\/p>\n<p>\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u539f\u5219\uff1a<strong>\u8d44\u6e90\u540d\u7528\u590d\u6570\u540d\u8bcd<\/strong>\u3002\u6bd4\u5982\uff1a<\/p>\n<ul>\n<li><code>GET \/expeditions<\/code> &#8211; \u83b7\u53d6\u6240\u6709\u63a2\u9669\u6d3b\u52a8<\/li>\n<li><code>GET \/expeditions\/{id}<\/code> &#8211; \u6839\u636e ID \u83b7\u53d6\u5355\u4e2a\u63a2\u9669\u6d3b\u52a8<\/li>\n<\/ul>\n<p>\u793a\u4f8b\u4ee3\u7801\u957f\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"Java\">@Path(&quot;expeditions&quot;)\npublic class ExpeditionResource {\n\n    @GET\n    public List&lt;Expedition&gt; list() {\n        \/\/ \u5b9e\u73b0\u903b\u8f91\n    }\n\n    @GET\n    @Path(&quot;\/{id}&quot;)\n    public Expedition get(@PathParam(&quot;id&quot;) String id) {\n        \/\/ \u5b9e\u73b0\u903b\u8f91\n    }\n\n    @GET\n    @Path(&quot;\/search&quot;)\n    public List&lt;Expedition&gt; mine() {\n        \/\/ \u5b9e\u73b0\u903b\u8f91\n    }\n}<\/code><\/pre>\n<p>\u8981\u662f\u60f3\u4e86\u89e3\u66f4\u8be6\u7ec6\u7684\u547d\u540d\u89c4\u8303\uff0c\u53ef\u4ee5\u770b\u770b\u300aREST API Design Rulebook\u300b\uff08\u300aREST API \u8bbe\u8ba1\u624b\u518c\u300b\uff09\u3002<\/p>\n<h2>2. \u53ef\u7ef4\u62a4\u3001\u53ef\u6269\u5c55\uff0c\u6587\u6863\u522b\u843d\u4e0b<\/h2>\n<p>\u968f\u7740 API \u590d\u6742\u5ea6\u4e0a\u5347\uff0c<strong>\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027<\/strong>\u5c31\u6210\u4e86\u5173\u952e\u3002\u8981\u4fdd\u8bc1\u53ef\u7ef4\u62a4\uff0c\u9996\u5148\u5f97\u628a\u6587\u6863\u505a\u597d\u2014\u2014\u867d\u7136\u5f00\u53d1\u8005\u5927\u591a\u4e0d\u7231\u5199\u6587\u6863\uff0c\u4f46\u8fd9\u73a9\u610f\u513f\u771f\u7684\u7f3a\u4e00\u4e0d\u53ef\u3002OpenAPI \u5c31\u662f\u4e2a\u597d\u5de5\u5177\uff0c\u80fd\u81ea\u52a8\u751f\u6210\u548c\u5b8c\u5584\u6587\u6863\uff0c\u5177\u4f53\u53ef\u4ee5\u53bb<a href=\"https:\/\/link.segmentfault.com\/?enc=KVD5EGfpBo%2FyEo0WcSdDAg%3D%3D.qyEQj%2FUGKoJ0tktvWgtmAMDDtSYnErEiBZYVJtxlRQA%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"> OpenAPI \u5b98\u7f51<\/a>\u770b\u770b\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u91cd\u70b9\u662f<strong>\u7248\u672c\u63a7\u5236<\/strong>\u3002\u7248\u672c\u63a7\u5236\u80fd\u4fdd\u8bc1\u5411\u540e\u517c\u5bb9\uff0c\u8ba9 API \u8fed\u4ee3\u66f4\u5e73\u6ed1\uff1a\u65b0\u65e7\u7248\u672c\u53ef\u4ee5\u540c\u65f6\u8fd0\u884c\uff0c\u7528\u6237\u4ec0\u4e48\u65f6\u5019\u8fc1\u79fb\u5230\u65b0\u7248\u672c\u90fd\u65b9\u4fbf\u3002\u5728 Java \u91cc\u5b9e\u73b0\u4e5f\u7b80\u5355\uff1a\u7ed9\u6bcf\u4e2a\u7248\u672c\u5efa\u72ec\u7acb\u7684\u5305\uff0c\u518d\u7528\u9002\u914d\u5c42\u5904\u7406\u4e0d\u540c\u7248\u672c\u95f4\u7684\u4ea4\u4e92\u5c31\u884c\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"Java\">\/\/ v1\u7248\u672c\u7684API\npackage os.expert.demo.expeditions.v1;\n@Path(&quot;\/api\/v1\/expeditions&quot;)\npublic class ExpeditionResource {\n    \/\/ \u5b9e\u73b0\u903b\u8f91\n}\n\n\/\/ v2\u7248\u672c\u7684API\npackage os.expert.demo.expeditions.v2;\n@Path(&quot;\/api\/v2\/expeditions&quot;)\npublic class ExpeditionResource {\n    \/\/ \u5b9e\u73b0\u903b\u8f91\n}<\/code><\/pre>\n<h2>3. \u5b89\u5168\u7b2c\u4e00\uff1a\u6c38\u8fdc\u522b\u76f8\u4fe1\u7528\u6237\u8f93\u5165<\/h2>\n<p>\u5b89\u5168\u662f\u6240\u6709 API \u7684\u5e95\u7ebf\u3002\u6838\u5fc3\u539f\u5219\u5c31\u4e00\u6761\uff1a<strong>\u6c38\u8fdc\u522b\u76f8\u4fe1\u7528\u6237<\/strong>\uff0c\u5fc5\u987b\u9a8c\u8bc1\u4ed6\u4eec\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u8bf7\u6c42\u7684\u8d44\u6e90\u3002\u4e00\u4e2a\u5b9e\u7528\u7684\u505a\u6cd5\u662f\uff1a\u901a\u8fc7\u8ba4\u8bc1\u6765\u786e\u5b9a\u7528\u6237\u80fd\u8bbf\u95ee\u54ea\u4e9b\u63a2\u9669\u6d3b\u52a8\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u7528\u6237\u81ea\u5df1\u4f20\u7684 ID\u3002<\/p>\n<p>\u6bd4\u5982\u8fd9\u6837\u8bbe\u8ba1\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"Java\">@GET\n@Path(&quot;\/my-expeditions&quot;)\npublic List&lt;Expedition&gt; myExpeditions() {\n    \/\/ \u4e0d\u7528\u8981ID\uff0c\u56e0\u4e3a\u7528\u6237\u5df2\u7ecf\u8fc7\u8ba4\u8bc1\u4e86\n    \/\/ \u5b9e\u73b0\u903b\u8f91\n}<\/code><\/pre>\n<p>\u4e0d\u5149\u662f\u67e5\u8be2\uff0c\u7f16\u8f91\u3001\u5220\u9664\u8fd9\u4e9b\u64cd\u4f5c\u4e5f\u4e00\u6837\u2014\u2014<strong>\u5148\u9a8c\u6743\u9650\uff0c\u518d\u505a\u64cd\u4f5c<\/strong>\u3002<\/p>\n<h2>4. \u5f02\u5e38\u5904\u7406\u8981\u89c4\u8303\uff0cHTTP \u72b6\u6001\u7801\u7528\u5bf9<\/h2>\n<p>\u6700\u540e\uff0c\u597d\u7684 API \u5f97\u6709<strong>\u89c4\u8303\u7684\u5f02\u5e38\u5904\u7406<\/strong>\uff0c\u628a\u9519\u8bef\u5bf9\u5e94\u5230\u6b63\u786e\u7684 HTTP \u72b6\u6001\u7801\u4e0a\u3002\u6bd4\u5982\uff0c\u8981\u662f\u67e5\u4e0d\u5230\u67d0\u4e2a\u63a2\u9669\u6d3b\u52a8\uff0c\u5c31\u8be5\u8fd4\u56de<code>404 Not Found<\/code>\u2014\u2014\u8fd9\u6837 Java \u4ee3\u7801\u548c REST API \u7684\u8bed\u4e49\u624d\u80fd\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p>\u53ef\u4ee5\u7528\u5f02\u5e38\u6620\u5c04\u5668\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"Java\">@Provider\npublic class ExpeditionNotFoundExceptionMapper implements ExceptionMapper&lt;ExpeditionNotFoundException&gt; {\n\n    @Override\n    public Response toResponse(ExpeditionNotFoundException exception) {\n        return Response.status(Response.Status.NOT_FOUND).entity(exception.getMessage()).build();\n    }\n}<\/code><\/pre>\n<h2>\u603b\u7ed3\u4e00\u4e0b<\/h2>\n<p>\u6253\u9020\u9760\u8c31\u7684 REST API\uff0c\u5176\u5b9e\u5c31\u662f\u8fd9\u51e0\u6b65\uff1a<\/p>\n<ol>\n<li><strong>\u6253\u597d\u57fa\u7840<\/strong>\uff1a\u5148\u641e\u61c2 Richardson \u6210\u719f\u5ea6\u6a21\u578b\uff1b<\/li>\n<li><strong>\u547d\u540d\u7edf\u4e00<\/strong>\uff1a\u9075\u5faa\u6e05\u6670\u4e00\u81f4\u7684\u8d44\u6e90\u547d\u540d\u89c4\u8303\uff1b<\/li>\n<li><strong>\u6ce8\u91cd\u7ef4\u62a4<\/strong>\uff1a\u505a\u597d\u7248\u672c\u63a7\u5236\uff0c\u7528 OpenAPI \u751f\u6210\u6587\u6863\uff1b<\/li>\n<li><strong>\u5b89\u5168\u4f18\u5148<\/strong>\uff1a\u52a1\u5fc5\u9a8c\u8bc1\u7528\u6237\u6743\u9650\uff1b<\/li>\n<li><strong>\u5f02\u5e38\u89c4\u8303<\/strong>\uff1a\u8fd4\u56de\u6b63\u786e\u7684 HTTP \u72b6\u6001\u7801\u3002<\/li>\n<\/ol>\n<p>\u8ddf\u7740\u8fd9\u4e9b\u6280\u5de7\u8d70\uff0c\u4f60\u5c31\u80fd\u5f00\u53d1\u51fa\u65e2\u53ef\u9760\u53c8\u597d\u7ef4\u62a4\u7684 Java REST API \u4e86\u2014\u2014\u8981\u77e5\u9053\uff0c\u54ea\u6015\u662f\u8d44\u6df1\u5f00\u53d1\u8005\uff0c\u4e5f\u5f97\u5728\u8fd9\u4e9b\u70b9\u4e0a\u591a\u4e0b\u529f\u592b\u5462\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047267140\">https:\/\/segmentfault.com\/a\/1190000047267140<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60f3\u8ba9\u4f60\u7684 Java REST API \u66f4\u9760\u8c31\uff1f\u5173\u952e\u5c31\u5728\u7edf\u4e00\u7684\u8d44\u6e90\u547d\u540d\u3001\u6613\u7ef4\u62a4\u7684\u7248\u672c\u63a7\u5236\u3001\u624e\u5b9e\u7684\u5b89\u5168\u9632\u62a4\u548c\u89c4\u8303\u7684 [&hellip;]<\/p>\n","protected":false},"author":3,"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-10021003","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\/10021003","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021003"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021003\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}