{"id":1695,"date":"2023-03-25T22:52:04","date_gmt":"2023-03-25T14:52:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1695"},"modified":"2023-04-23T21:43:53","modified_gmt":"2023-04-23T13:43:53","slug":"resttemplate-in-spring-boot-not-easy-to-use-try-retrofit","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/resttemplate-in-spring-boot-not-easy-to-use-try-retrofit\/","title":{"rendered":"Spring Boot \u4e2d\u7684 RestTemplate\u4e0d\u597d\u7528\uff1f\u8bd5\u8bd5 Retrofit\uff01"},"content":{"rendered":"<p>Retrofit\u662f\u9002\u7528\u4e8eAndroid\u548cJava\u4e14\u7c7b\u578b\u5b89\u5168\u7684HTTP\u5ba2\u6237\u7aef\uff0c\u5176\u6700\u5927\u7684\u7279\u6027\u7684\u662f\u652f\u6301\u901a\u8fc7\u63a5\u53e3\u7684\u65b9\u5f0f\u53d1\u8d77HTTP\u8bf7\u6c42\u3002\u800cspring-boot\u662f\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684Java\u5f00\u53d1\u6846\u67b6\uff0c\u4f46\u662fRetrofit\u5b98\u65b9\u6ca1\u6709\u652f\u6301\u4e0espring-boot\u6846\u67b6\u5feb\u901f\u6574\u5408\uff0c\u56e0\u6b64\u6211\u4eec\u5f00\u53d1\u4e86retrofit-spring-boot-starter\u3002<\/p>\n<p><code>retrofit-spring-boot-starter<\/code>\u5b9e\u73b0\u4e86Retrofit\u4e0espring-boot\u6846\u67b6\u5feb\u901f\u6574\u5408\uff0c\u5e76\u4e14\u652f\u6301\u4e86\u8bf8\u591a\u529f\u80fd\u589e\u5f3a\uff0c\u6781\u5927\u7b80\u5316\u5f00\u53d1\u3002<\/p>\n<p><!-- more --><\/p>\n<p>Github\u9879\u76ee\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter\">https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter<\/a><br \/>\nGitee\u9879\u76ee\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/gitee.com\/lianjiatech\/retrofit-spring-boot-starter\">https:\/\/gitee.com\/lianjiatech\/retrofit-spring-boot-starter<\/a><br \/>\n\u793a\u4f8bdemo\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/ismart-yuxi\/retrofit-spring-boot-demo\">https:\/\/github.com\/ismart-yuxi\/retrofit-spring-boot-demo<\/a><\/p>\n<h2>\u529f\u80fd\u7279\u6027<\/h2>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u81ea\u5b9a\u4e49\u6ce8\u5165OkHttpClient\" title=\"\u81ea\u5b9a\u4e49\u6ce8\u5165OkHttpClient\">\u81ea\u5b9a\u4e49\u6ce8\u5165OkHttpClient<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u6ce8\u89e3\u5f0f\u62e6\u622a\u5668\" title=\"\u6ce8\u89e3\u5f0f\u62e6\u622a\u5668\">\u6ce8\u89e3\u5f0f\u62e6\u622a\u5668<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u8fde\u63a5\u6c60\u7ba1\u7406\" title=\"\u8fde\u63a5\u6c60\u7ba1\u7406\">\u8fde\u63a5\u6c60\u7ba1\u7406<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u65e5\u5fd7\u6253\u5370\" title=\"\u65e5\u5fd7\u6253\u5370\">\u65e5\u5fd7\u6253\u5370<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u8bf7\u6c42\u91cd\u8bd5\" title=\"\u8bf7\u6c42\u91cd\u8bd5\">\u8bf7\u6c42\u91cd\u8bd5<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u9519\u8bef\u89e3\u7801\u5668\" title=\"\u9519\u8bef\u89e3\u7801\u5668\">\u9519\u8bef\u89e3\u7801\u5668<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u5168\u5c40\u62e6\u622a\u5668\" title=\"\u5168\u5c40\u62e6\u622a\u5668\">\u5168\u5c40\u62e6\u622a\u5668<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u7194\u65ad\u964d\u7ea7\" title=\"\u7194\u65ad\u964d\u7ea7\">\u7194\u65ad\u964d\u7ea7<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u5fae\u670d\u52a1\u4e4b\u95f4\u7684HTTP\u8c03\u7528\" title=\"\u5fae\u670d\u52a1\u4e4b\u95f4\u7684HTTP\u8c03\u7528\">\u5fae\u670d\u52a1\u4e4b\u95f4\u7684HTTP\u8c03\u7528<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u8c03\u7528\u9002\u914d\u5668\" title=\"\u8c03\u7528\u9002\u914d\u5668\">\u8c03\u7528\u9002\u914d\u5668<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter#\u6570\u636e\u8f6c\u6362\u5668\" title=\"\u6570\u636e\u8f6c\u6362\u5668\">\u6570\u636e\u8f6c\u6362\u5668<\/a><\/li>\n<\/ul>\n<h2>\u5feb\u901f\u4f7f\u7528<\/h2>\n<h3>\u5f15\u5165\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.github.lianjiatech&lt;\/groupId&gt;\n    &lt;artifactId&gt;retrofit-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;2.2.6&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u672c\u9879\u76ee\u4f9d\u8d56Retrofit-2.9.0\uff0cokhttp-3.14.9\uff0cokio-1.17.5\u7248\u672c\uff0c\u5982\u679c\u51b2\u7a81\uff0c\u70e6\u8bf7\u624b\u52a8\u5f15\u5165\u76f8\u5173jar\u5305\u3002\u5b8c\u6574\u4f9d\u8d56\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.github.lianjiatech&lt;\/groupId&gt;\n    &lt;artifactId&gt;retrofit-spring-boot-starter&lt;\/artifactId&gt;\n    &lt;version&gt;2.2.6&lt;\/version&gt;\n&lt;\/dependency&gt;\n &lt;dependency&gt;\n    &lt;groupId&gt;com.squareup.okhttp3&lt;\/groupId&gt;\n    &lt;artifactId&gt;logging-interceptor&lt;\/artifactId&gt;\n    &lt;version&gt;3.14.9&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.squareup.okhttp3&lt;\/groupId&gt;\n    &lt;artifactId&gt;okhttp&lt;\/artifactId&gt;\n    &lt;version&gt;3.14.9&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.squareup.okio&lt;\/groupId&gt;\n    &lt;artifactId&gt;okio&lt;\/artifactId&gt;\n    &lt;version&gt;1.17.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.squareup.retrofit2&lt;\/groupId&gt;\n    &lt;artifactId&gt;retrofit&lt;\/artifactId&gt;\n    &lt;version&gt;2.9.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.squareup.retrofit2&lt;\/groupId&gt;\n    &lt;artifactId&gt;converter-jackson&lt;\/artifactId&gt;\n    &lt;version&gt;2.9.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u5b9a\u4e49http\u63a5\u53e3<\/h3>\n<p>\u63a5\u53e3\u5fc5\u987b\u4f7f\u7528<code>@RetrofitClient<\/code>\u6ce8\u89e3\u6807\u8bb0\uff01http\u76f8\u5173\u6ce8\u89e3\u53ef\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/square.github.io\/retrofit\/\" title=\"retrofit\u5b98\u65b9\u6587\u6863\">retrofit\u5b98\u65b9\u6587\u6863<\/a><\/p>\n<pre><code class=\"language-java\">@RetrofitClient(baseUrl = &quot;${test.baseUrl}&quot;)\npublic interface HttpApi {\n\n    @GET(&quot;person&quot;)\n    Result&lt;Person&gt; getPerson(@Query(&quot;id&quot;) Long id);\n}<\/code><\/pre>\n<blockquote>\n<p>\u53cb\u60c5\u63d0\u793a\uff1a\u65b9\u6cd5\u8bf7\u6c42\u8def\u5f84\u614e\u7528<code>\/<\/code>\u5f00\u5934\u3002\u5bf9\u4e8eRetrofit\u800c\u8a00\uff0c\u5982\u679c<code>baseUrl=http:\/\/localhost:8080\/api\/test\/<\/code>\uff0c\u65b9\u6cd5\u8bf7\u6c42\u8def\u5f84\u5982\u679c\u662f<code>person<\/code>\uff0c\u5219\u8be5\u65b9\u6cd5\u5b8c\u6574\u7684\u8bf7\u6c42\u8def\u5f84\u662f\uff1a<code>http:\/\/localhost:8080\/api\/test\/person<\/code>\u3002\u800c\u65b9\u6cd5\u8bf7\u6c42\u8def\u5f84\u5982\u679c\u662f<code>\/person<\/code>\uff0c\u5219\u8be5\u65b9\u6cd5\u5b8c\u6574\u7684\u8bf7\u6c42\u8def\u5f84\u662f\uff1a<code>http:\/\/localhost:8080\/person<\/code><\/p>\n<\/blockquote>\n<h3>\u6ce8\u5165\u4f7f\u7528<\/h3>\n<p>\u5c06\u63a5\u53e3\u6ce8\u5165\u5230\u5176\u5b83Service\u4e2d\u5373\u53ef\u4f7f\u7528\uff01<\/p>\n<pre><code class=\"language-java\">@Service\npublic class TestService {\n\n    @Autowired\n    private HttpApi httpApi;\n\n    public void test() {\n        \/\/ \u901a\u8fc7httpApi\u53d1\u8d77http\u8bf7\u6c42\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u81ea\u52a8\u4f7f\u7528SpringBoot\u626b\u63cf\u8def\u5f84\u8fdb\u884c<code>retrofitClient<\/code>\u6ce8\u518c\u3002\u4f60\u4e5f\u53ef\u4ee5\u5728\u914d\u7f6e\u7c7b\u52a0\u4e0a<code>@RetrofitScan<\/code>\u624b\u5de5\u6307\u5b9a\u626b\u63cf\u8def\u5f84\u3002<\/p>\n<\/blockquote>\n<h2>HTTP\u8bf7\u6c42\u76f8\u5173\u6ce8\u89e3<\/h2>\n<p>HTTP\u8bf7\u6c42\u76f8\u5173\u6ce8\u89e3\uff0c\u5168\u90e8\u4f7f\u7528\u4e86retrofit\u539f\u751f\u6ce8\u89e3\u3002\u8be6\u7ec6\u4fe1\u606f\u53ef\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/square.github.io\/retrofit\/\" title=\"retrofit\u5b98\u65b9\u6587\u6863\">retrofit\u5b98\u65b9\u6587\u6863<\/a><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u6ce8\u89e3\u5206\u7c7b<\/th>\n<th style=\"text-align: left;\">\u652f\u6301\u7684\u6ce8\u89e3<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">\u8bf7\u6c42\u65b9\u5f0f<\/td>\n<td style=\"text-align: left;\"><code>@GET<\/code> <code>@HEAD<\/code> <code>@POST<\/code> <code>@PUT<\/code> <code>@DELETE<\/code> <code>@OPTIONS<\/code> <code>@HTTP<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u8bf7\u6c42\u5934<\/td>\n<td style=\"text-align: left;\"><code>@Header<\/code> <code>@HeaderMap<\/code> <code>@Headers<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">Query\u53c2\u6570<\/td>\n<td style=\"text-align: left;\"><code>@Query<\/code> <code>@QueryMap<\/code> <code>@QueryName<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">path\u53c2\u6570<\/td>\n<td style=\"text-align: left;\"><code>@Path<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">form-encoded\u53c2\u6570<\/td>\n<td style=\"text-align: left;\"><code>@Field<\/code> <code>@FieldMap<\/code> <code>@FormUrlEncoded<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u8bf7\u6c42\u4f53<\/td>\n<td style=\"text-align: left;\"><code>@Body<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u6587\u4ef6\u4e0a\u4f20<\/td>\n<td style=\"text-align: left;\"><code>@Multipart<\/code> <code>@Part<\/code> <code>@PartMap<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">url\u53c2\u6570<\/td>\n<td style=\"text-align: left;\"><code>@Url<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u914d\u7f6e\u9879\u8bf4\u660e<\/h2>\n<p><code>retrofit-spring-boot-starter<\/code>\u652f\u6301\u4e86\u591a\u4e2a\u53ef\u914d\u7f6e\u7684\u5c5e\u6027\uff0c\u7528\u6765\u5e94\u5bf9\u4e0d\u540c\u7684\u4e1a\u52a1\u573a\u666f\u3002\u8be6\u7ec6\u4fe1\u606f\u53ef\u53c2\u8003 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/LianjiaTech\/retrofit-spring-boot-starter\/blob\/master\/src\/test\/resources\/application.yml\" title=\"\u914d\u7f6e\u9879\u793a\u4f8b\">\u914d\u7f6e\u9879\u793a\u4f8b<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit\u662f\u9002\u7528\u4e8eAndroid\u548cJava\u4e14\u7c7b\u578b\u5b89\u5168\u7684HTTP\u5ba2\u6237\u7aef\uff0c\u5176\u6700\u5927\u7684\u7279\u6027\u7684\u662f\u652f\u6301\u901a\u8fc7\u63a5\u53e3\u7684\u65b9\u5f0f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[253,423],"class_list":["post-1695","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-resttemplate","tag-retrofit"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1695"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1695\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}