{"id":1565,"date":"2017-09-11T22:25:25","date_gmt":"2017-09-11T14:25:25","guid":{"rendered":"http:\/\/coderbee.net\/?p=1565"},"modified":"2019-01-06T21:17:44","modified_gmt":"2019-01-06T13:17:44","slug":"feign","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20170911\/1565","title":{"rendered":"Feign"},"content":{"rendered":"<h1>\u7b80\u4ecb<\/h1>\n<p>Feign \u662f\u4e00\u4e2a Java \u5230 HTTP \u5ba2\u6237\u7aef\u7684\u7c98\u5408\u5242\u3002Feign \u7684\u76ee\u6807\u662f\u4ee5\u6700\u5c11\u7684\u5f00\u9500\u548c\u4ee3\u7801\u628a \u4f60\u7684\u4ee3\u7801\u8fde\u63a5\u5230 http api \u4e0a\u3002\u901a\u8fc7\u5b9a\u5236\u7684\u7f16\u89e3\u7801\u5668\u548c\u9519\u8bef\u5904\u7406\uff0c\u4f60\u53ef\u4ee5\u8bf7\u6c42\u4efb\u4f55\u57fa\u4e8e\u6587\u672c\u7684 http api \u3002<\/p>\n<h1>\u539f\u7406<\/h1>\n<p>\u901a\u8fc7\u5904\u7406\u6ce8\u89e3\u4fe1\u606f\u6765\u751f\u6210\u6a21\u677f\u5316\u8bf7\u6c42\u3002\u5728\u53d1\u51fa\u8bf7\u6c42\u524d\uff0c\u53c2\u6570\u76f4\u63a5\u5e94\u7528\u5230\u8fd9\u4e9b\u6a21\u677f\u4e0a\u3002\u8fd9\u9650\u5236\u4e86 Feign \u53ea\u652f\u6301\u57fa\u4e8e\u6587\u672c\u7684 api\uff0c\u8fd9\u663e\u8457\u7b80\u5316\u4e86\u7cfb\u7edf\u7684\u4e00\u4e9b\u65b9\u9762\u5982\u91cd\u653e\u8bf7\u6c42\u3002<\/p>\n<h1>\u4e3a\u4ec0\u4e48\u9009\u62e9 Feign<\/h1>\n<ul>\n<li>\n<p>\u4f9d\u8d56\u95ee\u9898\u3002\u76ee\u524d\u9879\u76ee\u7ec4\u7528\u7684\u662f Hessian \u505a\u8fdc\u7a0b\u8c03\u7528\uff0c\u7531\u4e8e Hessian \u5b58\u5728\u5bf9 jar \u5305\u7684\u4f9d\u8d56\uff0c\u7279\u522b\u662f\u4e00\u4e9b\u9879\u76ee\u5347\u7ea7\u5230 JDK 1.8\uff0c\u91c7\u7528 Maven \u6784\u5efa\uff1b\u800c\u8001\u7684\u4e00\u4e9b\u4efb\u7136\u91c7\u7528 1.6 \uff0c\u662f\u4e2a\u7b80\u5355\u7684 Eclipse \u5de5\u7a0b\uff0c\u5bfc\u81f4\u6253\u5305\u3001\u90e8\u7f72\u975e\u5e38\u9ebb\u70e6\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f9d\u8d56\u4e8e\u63a5\u53e3\uff0c\u4f7f\u7528\u7b80\u6d01\u3002\u5bf9\u4e8e\u5ba2\u6237\u7aef\uff0c\u53ea\u4f9d\u8d56\u4e8e\u63a5\u53e3\u7c7b\uff0c\u901a\u8fc7 Spring \u6ce8\u5165\u5b9e\u73b0\uff0c\u8fc1\u79fb\u57fa\u672c\u4e0d\u9700\u8981\u5f88\u5927\u7684\u6539\u52a8\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0e Spring Cloud \u96c6\u6210\u3002Feign \u672c\u8eab\u662f Spring Cloud \u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7 Ribbon \u505a\u8def\u7531\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u670d\u52a1\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>\u7cfb\u7edf\u5bf9\u6027\u80fd\u7684\u8981\u6c42\u5e76\u4e0d\u662f\u90a3\u79cd\u5f88\u4e25\u82db\u7684\uff0c\u57fa\u4e8e\u6587\u672c\u7684\u8c03\u7528\u4e5f\u65b9\u4fbf\u8c03\u8bd5\u3002<\/p>\n<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h1>\u4f7f\u7528\u4e3e\u4f8b<\/h1>\n<p>\u9996\u5148\u8981\u5b9a\u4e49 Java \u4ee3\u7801\u5230 HTTP \u8bf7\u6c42\u6a21\u677f\u7684\u6ce8\u89e3\uff0c\u5982\u4e0b\uff0c\u5728\u63a5\u53e3\u7684\u65b9\u6cd5\u4e0a\u8fdb\u884c\u3002<\/p>\n<pre><code class=\"java\">public interface HttpApi {\n    \/\/ \u63d0\u4ea4\u4e00\u4e9b\u7b80\u5355\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u590d\u5408\u5bf9\u8c61\u7684\u5217\u8868\n    @RequestLine(\"GET \/repos\/{owner}\/{repo}\/contributors\")\n    List&lt;Contributor&gt; contributors(@Param(\"owner\") String owner, @Param(\"repo\") String repo);\n\n    \/\/ \u6307\u5b9a http \u5934\u7684\u4e00\u4e9b\u5b57\u6bb5\u3002\u63d0\u4ea4\u590d\u5408\u7c7b\u578b\u7684\u53c2\u6570\n    @RequestLine(\"PUT \/repos\/contributors\/{id}\")\n    @Headers(\"Content-Type: application\/json\")\n    void putContributor(@Param(\"id\") String id, Contributor contributor);\n\n    \/\/ \u5b9a\u5236\u8bf7\u6c42\u4f53\u7684\u5185\u5bb9\n    @RequestLine(\"POST \/login\")\n    @Headers(\"Content-Type: application\/json\")\n    \/\/ json \u7684\u82b1\u62ec\u53f7\u5fc5\u987b\u8fdb\u884c\u8f6c\u4e49\n    @Body(\"%7B\\\"name\\\": \\\"{name}\\\", \\\"password\\\": \\\"{password}\\\"%7D\")\n    void login(@Param(\"name\") String name, @Param(\"password\") String password);\n}\n<\/code><\/pre>\n<p>\u8c03\u7528\uff1b<\/p>\n<pre><code class=\"java\">HttpApi httpApi = Feign.builder().decoder(new GsonDecoder()).encoder(new GsonEncoder()).target(HttpApi.class,\n                \"https:\/\/api.github.com\");\nList&lt;Contributor&gt; contributors = httpApi.contributors(\"OpenFeign\", \"feign\");\n<\/code><\/pre>\n<p>\u5982\u679c\u53c2\u6570\u6216\u8fd4\u56de\u503c\u6709\u590d\u5408\u5bf9\u8c61\uff0cFeign \u5185\u7f6e\u7684\u7f16\u89e3\u7801\u5668\u662f\u4e0d\u652f\u6301\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u57fa\u4e8e Gson \u5b9e\u73b0\u7684 feign-gson \u5e93\u3002<\/p>\n<p>\u6784\u5efa\u5668\u7684 target \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u63a5\u53e3\u7c7b\u548c\u76ee\u6807 http api \u7684\u57fa\u8def\u5f84(\u8ddf <code>@RequestLine<\/code> \u91cc\u6307\u5b9a\u7684\u8def\u5f84\u7ec4\u5408\u8d77\u6765\u5c31\u662f\u5b8c\u6574\u7684\u8bf7\u6c42\u8def\u5f84)\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u8bf7\u6c42 http api \u65f6\u5c31\u50cf\u8c03\u7528\u672c\u5730\u65b9\u6cd5\u4e00\u6837\uff1a<code>httpApi.contributors(\"OpenFeign\", \"feign\");<\/code> \u3002<\/p>\n<p><strong>\u5bf9\u4e8e\u6ca1\u6709\u5728 \u6ce8\u89e3\u91cc\u6307\u5b9a\u7684\u53c2\u6570\uff0c\u5373\u4f7f\u7528\u4e86 <code>@Param<\/code> \u6ce8\u89e3\uff0c\u90fd\u653e\u8fdb\u4e00\u4e2a Map \u91cc\uff0c\u7f16\u7801\u540e\u4f5c\u4e3a\u8bf7\u6c42\u4f53\u3002<\/strong><br \/>\n\u8fd9\u4f1a\u5bfc\u81f4\u670d\u52a1\u5668\u7aef\u6ca1\u6cd5\u628a\u53c2\u6570\u6620\u5c04\u5230\u591a\u4e2a POJO \u4e0a\u3002<\/p>\n<pre><code class=\"java\">@RequestLine(\"PUT \/repos\/contributors\/params\/{id}\")\n@Headers(\"Content-Type: application\/json\")\nvoid putContributorParam(@Param(\"id\") String id, @Param(\"contributor1\") String contributor1,\n        @Param(\"contributor2\") String contributor2);\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u9762\u7684\u65b9\u5f0f\u63d0\u4ea4\u7684 Feign \u4f1a\u628a <code>contributor1, contributor2<\/code> \u53c2\u6570\u653e\u8fdb\u4e00\u4e2a Map \u91cc\uff0c\u7136\u540e\u628a\u8fd9\u4e2a Map \u5e8f\u5217\u5316\u4e3a JSON \u4e32\u4f5c\u4e3a\u8bf7\u6c42\u4f53\u53d1\u9001\u51fa\u53bb\u3002<em>\u8fd9\u4e5f\u662f\u9ed8\u8ba4\u7684\u65b9\u5f0f\u3002<\/em><\/p>\n<p>\u5982\u679c\u9700\u8981\u4ee5 \u65b9\u5f0f\u53d1\u9001\u8bf7\u6c42\uff0c\u9700\u8981\u624b\u5de5\u5bf9\u53c2\u6570\u503c\u8fdb\u884c URLEncode \u3002<\/p>\n<pre><code class=\"java\">@RequestLine(\"PUT \/repos\/contributors\/params\/{id}\")\n@Headers(\"Content-Type: application\/x-www-form-urlencoded\")\n@Body(\"contributor1={contributor1}&amp;contributor2={contributor2}\")\nvoid putContributorParam(@Param(\"id\") String id, @Param(\"contributor1\") String contributor1,\n        @Param(\"contributor2\") String contributor2);\n<\/code><\/pre>\n<p>\u8c03\u7528\uff1a <code>httpApi.putContributorParam(\"123654\", \"+-*\/=\u4e2d\u6587&amp;contributor2=123\", \"0123457896\");<\/code><\/p>\n<pre><code>Content-Type: application\/x-www-form-urlencoded\nContent-Length: 42\n\ncontributor1=+-*\/=&amp;contributor2=0123457896\n<\/code><\/pre>\n<p>\u670d\u52a1\u7aef\u5f97\u5230\u7684\u53c2\u6570\u7684 contributor1 \u662f <code>\" -*\/=\u4e2d\u6587\"<\/code>\uff0ccontributor2 \u662f <code>\"123,0123457896\"<\/code> \u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Feign \u662f\u4e00\u4e2a Java \u5230 HTTP \u5ba2\u6237\u7aef\u7684\u7c98\u5408\u5242\u3002Feign \u7684 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20170911\/1565\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[287,288],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1565"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1565"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":1742,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1565\/revisions\/1742"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}