{"id":11872,"date":"2023-08-17T13:43:32","date_gmt":"2023-08-17T05:43:32","guid":{"rendered":"http:\/\/92it.top\/?p=11872"},"modified":"2023-08-18T11:10:06","modified_gmt":"2023-08-18T03:10:06","slug":"spring%e4%b9%8bresttemplate%e6%8e%a8%e8%8d%90","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=11872","title":{"rendered":"Spring\u4e4bRestTemplate(\u63a8\u8350)"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/blog.csdn.net\/D1842501760\/article\/details\/124216538\">https:\/\/blog.csdn.net\/D1842501760\/article\/details\/124216538<\/a><\/p>\n\n\n\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>RestTemplate\u662f\u7531Spring\u6846\u67b6\u63d0\u4f9b\u7684\u4e00\u4e2a\u53ef\u7528\u4e8e\u5e94\u7528\u4e2d\u8c03\u7528rest\u670d\u52a1\u7684\u7c7b\u5b83\u7b80\u5316\u4e86\u4e0ehttp\u670d\u52a1\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u7edf\u4e00\u4e86RESTFul\u7684\u6807\u51c6\uff0c\u5c01\u88c5\u4e86http\u8fde\u63a5\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4f20\u5165url\u53ca\u5176\u8fd4\u56de\u503c\u7c7b\u578b\u5373\u53ef\u3002\u76f8\u8f83\u4e8e\u4e4b\u524d\u5e38\u7528\u7684HttpClient\uff0cRestTemplate\u662f\u4e00\u79cd\u66f4\u4e3a\u4f18\u96c5\u7684\u8c03\u7528RESTFul\u670d\u52a1\u7684\u65b9\u5f0f\u3002<\/li><li>\u5728Spring\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u7b2c\u4e09\u65b9REST\u670d\u52a1\u4e0e\u4f7f\u7528Spring RestTemplate\u7c7b\u6709\u5173\u3002RestTemplate\u7c7b\u7684\u8bbe\u8ba1\u539f\u5219\u4e0e\u8bb8\u591a\u5176\u4ed6Spring\u7684\u6a21\u677f\u7c7b(\u4f8b\u5982JdbcTemplate)\u76f8\u540c\uff0c\u4e3a\u6267\u884c\u590d\u6742\u4efb\u52a1\u63d0\u4f9b\u4e86\u4e00\u79cd\u5177\u6709\u9ed8\u8ba4\u884c\u4e3a\u7684\u7b80\u5316\u65b9\u6cd5\u3002<\/li><li>RestTemplate\u9ed8\u8ba4\u4f9d\u8d56JDK\u63d0\u4f9b\u4e86http\u8fde\u63a5\u7684\u80fd\u529b\uff08HttpURLConnection\uff09\uff0c\u5982\u679c\u6709\u9700\u8981\u7684\u8bdd\u4e5f\u53ef\u4ee5\u901a\u8fc7setRequestFactory\u65b9\u6cd5\u66ff\u6362\u4e3a\u4f8b\u5982Apache HttpCompoent\u3001Netty\u6216OKHttp\u7b49\u5176\u4ed6Http libaray\u3002<\/li><li>\u8003\u8651\u5230\u4e86RestTemplate\u7c7b\u662f\u4e3a\u4e86\u8c03\u7528REST\u670d\u52a1\u800c\u8bbe\u8ba1\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u4e3b\u8981\u65b9\u6cd5\u4e0eREST\u7684\u57fa\u7840\u7d27\u5bc6\u76f8\u8fde\u5c31\u4e0d\u8db3\u4e3a\u5947\u4e86\uff0c\u540e\u8005\u65f6HTTP\u534f\u8bae\u7684\u65b9\u6cd5\uff1aHEAD\u3001GET\u3001POST\u3001PUT\u3001DELETE\u3001OPTIONS\u4f8b\u5982\uff0cRestTemplate\u7c7b\u5177\u6709headForHeaders()\u3001getForObject()\u3001putForObject()\uff0cput()\u548cdelete()\u7b49\u65b9\u6cd5\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u521b\u5efa<code>RestTemplate<\/code><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u56e0\u4e3aRestTemplate\u662fSpirng\u6846\u67b6\u63d0\u4f9b\u7684\u6240\u4ee5\u53ea\u8981\u662f\u4e00\u4e2aSpringboot\u9879\u76ee\u5c31\u4e0d\u7528\u8003\u8651\u5bfc\u5305\u7684\u95ee\u9898\uff0c\u8fd9\u4e9b\u90fd\u662f\u63d0\u4f9b\u597d\u7684\u3002<\/p>\n\n\n\n<p>\u200b\u4f46\u662fSpring\u5e76\u6ca1\u6709\u5c06\u5176\u52a0\u5165SpringBean\u5bb9\u5668\u4e2d\uff0c\u9700\u8981\u6211\u4eec\u624b\u52a8\u52a0\u5165\uff0c\u56e0\u4e3a\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aSpringboot\u914d\u7f6e\u7c7b\uff0c\u518d\u5728\u914d\u7f6e\u7c7b\u4e2d\u5c06\u6211\u4eec\u7684RestTemlate\u6ce8\u518c\u5230Bean\u5bb9\u5668\u4e2d<\/p>\n\n\n\n<p><strong>\u65b9\u6cd5\u4e00<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528<code>Springboot<\/code>\u63d0\u4f9b\u7684<code>RestTemplateBuilder<\/code>\u6784\u9020\u7c7b\u6765\u6784\u9020\u4e00\u4e2a<code>RestTemplate<\/code>\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e00\u4e9b\u8fde\u63a5\u53c2\u6570\uff0c\u5982\uff1a\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0c\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\uff0c\u8fd8\u6709\u8ba4\u8bc1\u4fe1\u606f\u7b49<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-831x1024.png\" alt=\"\" class=\"wp-image-11876\" width=\"287\" height=\"353\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-831x1024.png 831w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-244x300.png 244w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-768x946.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-830x1022.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-230x283.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-350x431.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154-480x591.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-154.png 1148w\" sizes=\"(max-width: 287px) 100vw, 287px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class WebConfiguration {\n    @Bean\n    public RestTemplate restTemplate(RestTemplateBuilder builder){\n        return builder\n                \/\/\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\n                .setConnectTimeout(Duration.ofSeconds(5000))\n                \/\/\u8bbe\u7f6e\u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\n                .setReadTimeout(Duration.ofSeconds(5000))\n                \/\/\u8bbe\u7f6e\u8ba4\u8bc1\u4fe1\u606f\n                .basicAuthentication(\"username\",\"password\")\n                \/\/\u8bbe\u7f6e\u6839\u8def\u5f84\n                .rootUri(\"https:\/\/api.test.com\/\")\n                \/\/\u6784\u5efa\n                .build();\n    }\n}\n<\/pre>\n\n\n\n<p><strong>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u62e6\u622a\u5668<\/strong><\/p>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u62e6\u622a\u5668\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Slf4j\npublic class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {\n    @Override\n    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {\n\n        \/\/\u6253\u5370\u8bf7\u6c42\u660e\u7ec6\n        logRequestDetails(request,body);\n        ClientHttpResponse response = execution.execute(request, body);\n        \/\/\u6253\u5370\u54cd\u5e94\u660e\u7ec6\n        logResponseDetails(response);\n        \n        return response;\n    }\n\n    private void logRequestDetails(HttpRequest request, byte[] body){\n        log.debug(\"Headers:{}\",request.getHeaders());\n        log.debug(\"body:{}\",new String(body, StandardCharsets.UTF_8));\n        log.debug(\"{}:{}\",request.getMethod(),request.getMethodValue());\n    }\n\n    private void logResponseDetails(ClientHttpResponse response) throws IOException {\n        log.debug(\"Status code : {}\",response.getStatusCode());\n        log.debug(\"Status text : {}\",response.getStatusText());\n        log.debug(\"Headers : {}\",response.getHeaders());\n        log.debug(\"Response body: {}\", StreamUtils.copyToString(response.getBody(),StandardCharsets.UTF_8));\n    }\n}\n<\/pre>\n\n\n\n<p>\u4f7f\u7528<code>RestTemplateBuilder<\/code>\u6784\u9020\u7c7b\uff0c\u6dfb\u52a0\u81ea\u5b9a\u4e49\u62e6\u622a\u5668\uff0c\u6784\u9020\u5e26\u6709\u81ea\u5b9a\u4e49\u62e6\u622a\u5668\u7684<code>RestTemplate<\/code>\u5b9e\u4f8b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class WebConfiguration {\n\n    @Bean\n    public RestTemplate restTemplate(RestTemplateBuilder builder){\n        return builder\n                .additionalInterceptors(new CustomClientHttpRequestInterceptor())\n                \/\/\u6784\u5efa\n                .build();\n    }    \n}\n<\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u8bf7\u6c42\u786e\u5b9e\u7ecf\u8fc7\u4e86\u62e6\u622a\u5668\uff0c\u6ce8\u518c\u6210\u529f\uff08\u6ce8\u610f\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u6d41\u53ea\u4f1a\u88ab\u8bfb\u53d6\u4e00\u6b21\uff0c\u8fd9\u91cc\u6211\u4eec\u8bfb\u53d6\u4e86response\u540e\u8fd4\u56de\u7684response\u5c31\u8bfb\u53d6\u4e0d\u5230\u521a\u521a\u8bfb\u8fc7\u7684\u5185\u5bb9\u4e86\uff09<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-1024x193.png\" alt=\"\" class=\"wp-image-11877\" width=\"603\" height=\"113\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-1024x193.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-300x56.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-768x145.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-830x156.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-230x43.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-350x66.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155-480x90.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-155.png 1318w\" sizes=\"(max-width: 603px) 100vw, 603px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u65b9\u6cd5\u4e8c<\/strong><\/p>\n\n\n\n<p>\u200b\u4f7f\u7528<code>RestTemplate<\/code>\u6784\u9020\u65b9\u6cd5\u6784\u9020\u4e00\u4e2a<code>RestTemlate<\/code>\uff0c\u867d\u7136\u4e0d\u80fd\u50cf<code>RestTemplate<\/code>\u6784\u9020\u7c7b\u90a3\u6837\u66f4\u8be6\u7ec6\u3001\u66f4\u591a\u6837\u7684\u914d\u7f6e\u53c2\u6570\uff0c\u4f46\u662f<code>RestTemplate<\/code>\u6784\u9020\u65b9\u6cd5\u5728\u4e00\u822c\u60c5\u51b5\u662f\u591f\u7528\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"994\" height=\"146\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156.png\" alt=\"\" class=\"wp-image-11878\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156.png 994w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156-300x44.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156-768x113.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156-830x122.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156-230x34.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156-350x51.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-156-480x71.png 480w\" sizes=\"(max-width: 994px) 100vw, 994px\" \/><\/figure>\n\n\n\n<ul><li>\u65e0\u53c2\u6784\u9020 \u5168\u90e8\u53c2\u6570\u9ed8\u8ba4<\/li><\/ul>\n\n\n\n<ul><li>\u6307\u5b9a<code>ClientHttpRequestFactory<\/code> \u7684\u6784\u9020\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u81ea\u5df1\u5b9e\u73b0\u7684<code>ClientHttpRequestFactory<\/code>\uff08\u5ba2\u6237\u7aef<code>http<\/code>\u8bf7\u6c42\u5de5\u5382\uff09\u5176\u4ed6\u7684\u4e0e\u65e0\u53c2\u6784\u9020\u76f8\u540c\u3002<ul><li><code>ClientHttpRequestFactory<\/code><\/li><\/ul><\/li><\/ul>\n\n\n\n<ul><li>\u6307\u5b9a<code>List&lt;HttpMessageConverter&lt;?&gt;&gt;<\/code>\u7684\u6784\u9020\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9a\u81ea\u5df1\u662f\u5b9e\u73b0\u7684<code>HttpMessageConverter<\/code>\uff08<code>Http<\/code>\u6d88\u606f\u8f6c\u6362\u5668\uff09\u4f20\u5165\u5176\u4ed6\u4e0e\u65e0\u53c2\u6784\u9020\u76f8\u540c\u3002<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class WebConfiguration {\n\n    @Bean\n    public RestTemplate restTemplate(){\n        return new RestTemplate();\n    }\n    \n}\n<\/pre>\n\n\n\n<p>\u4e24\u8005\u65b9\u6cd5\u90fd\u53ef\u4f7f\u7528\uff0c\u524d\u8005\u63d0\u4f9b\u4e86\u591a\u6837\u7684\u81ea\u5b9a\u4e49\u53c2\u6570\u7684\u9009\u62e9\uff0c\u53ef\u4ee5\u5c06<code>RestTemplate<\/code>\u914d\u7f6e\u7684\u66f4\u4e3a\u5b8c\u5584\uff0c\u540e\u8005\u5219\u7b80\u5316\u4e86\u914d\u7f6e\u867d\u7136\u914d\u7f6e\u591a\u6837\u6027\u4e0d\u5982\u524d\u8005\uff0c\u4f46\u662f\u65e5\u5e38\u4f7f\u7528\u8c03\u7528\u4e9b<code>API<\/code>\u8fd8\u662f\u8db3\u4ee5\u4f7f\u7528<\/p>\n\n\n\n<p><strong><code>RestTemplate API<\/code>\u4f7f\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u4f7f\u7528<code>RestTemplate<\/code>\u524d\u5148\u8ba9\u6211\u4eec\u770b\u770b<code>RestTemplate<\/code>\u6709\u54ea\u4e9b<code>API<\/code><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-988x1024.png\" alt=\"\" class=\"wp-image-11882\" width=\"401\" height=\"415\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-988x1024.png 988w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-289x300.png 289w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-768x796.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-830x860.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-230x238.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-350x363.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158-480x498.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-158.png 1316w\" sizes=\"(max-width: 401px) 100vw, 401px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-1024x396.png\" alt=\"\" class=\"wp-image-11881\" width=\"403\" height=\"155\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-1024x396.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-300x116.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-768x297.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-830x321.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-230x89.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-350x135.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157-480x186.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-157.png 1308w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/figure><\/div>\n\n\n\n<p>\u200b\u76f8\u4fe1\u5927\u5bb6\u770b\u5230\u8fd9\u4e48\u591a\u65b9\u6cd5\uff0c\u4e00\u5b9a\u5f88\u5934\u5927\uff0c\u4f46\u662f\u6211\u4eec\u4ed4\u7ec6\u770b\u4e0a\u8ff0\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u51fa\u4e3b\u8981\u7684\u51e0\u79cd\u65b9\u6cd5\u662f(\u8fd9\u91cc\u53ea\u8ba8\u8bbaHttp\u8bf7\u6c42\u7684)\uff1a<\/p>\n\n\n\n<ul><li><code>GET<\/code><\/li><li><code>POST<\/code><\/li><li><code>PUT<\/code><\/li><li><code>DELETE<\/code><\/li><li><code>HEAD<\/code><\/li><li><code>OPTIONS<\/code><\/li><li><code>EXCHANGE<\/code><\/li><li><code>EXECUTE<\/code><\/li><\/ul>\n\n\n\n<p><strong>GET<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u56fe\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0<code>RestTemlate<\/code>\u53d1\u9001<code>GET<\/code>\u8bf7\u6c42\u7684\u65b9\u6cd5\u6709\u4e24\u79cd<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public &lt;T> T getForObject(...)\n\npublic &lt;T> ResponseEntity&lt;T> getForEntity(...)\n\n<\/pre>\n\n\n\n<p><code><strong>getForEntity()<\/strong><\/code><\/p>\n\n\n\n<p>\u540e\u7f00\u5e26\u6709<code>Entity<\/code>\u7684\u65b9\u6cd5\u90fd\u4ee3\u8868\u8fd4\u56de\u4e00\u4e2a<code>ResponseEntity&lt;T&gt;<\/code>\uff0c<code>ResponseEntity&lt;T&gt;<\/code>\u662fSpring\u5bf9<code>HTTP<\/code>\u8bf7\u6c42\u54cd\u5e94\u7684\u5c01\u88c5\uff0c\u5305\u62ec\u4e86\u51e0\u4e2a\u91cd\u8981\u7684\u5143\u7d20\uff0c\u5982\u54cd\u5e94\u7801\uff0c<code>contentType\u3001contentLength<\/code>\u3001\u54cd\u5e94\u6d88\u606f\u4f53\u7b49<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-159.png\" alt=\"\" class=\"wp-image-11884\" width=\"406\" height=\"64\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-159.png 670w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-159-300x47.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-159-230x36.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-159-350x55.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-159-480x76.png 480w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/figure><\/div>\n\n\n\n<p>\u200b\u901a\u8fc7\u5b83\u7ee7\u627f\u7236\u7c7b(<code>HttpEntity&lt;T&gt;<\/code>)\u7684<code>getHeader()<\/code>\u65b9\u6cd5\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6<code>contentType\u3001contentLength<\/code>\u3001\u54cd\u5e94\u6d88\u606f\u4f53\u7b49\u3002\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void queryWeather() {\n\n        ResponseEntity&lt;Object> forEntity = restTemplate.getForEntity(\"https:\/\/restapi.amap.com\/v3\/weather\/weatherInfo?city=510100&amp;key=e7a5fa943f706602033b6b329c49fbc6\", Object.class);\n        System.out.println(\"\u72b6\u6001\u7801:\"+forEntity.getStatusCode());\n        System.out.println(\"\u72b6\u6001\u7801\u5185\u5bb9:\"+forEntity.getStatusCodeValue());\n        HttpHeaders headers = forEntity.getHeaders();\n        System.out.println(\"\u54cd\u5e94\u5934:\"+headers);\n        Object body = forEntity.getBody();\n        System.out.println(\"\u54cd\u5e94\u5185\u5bb9:\"+body);\n    }\n<\/pre>\n\n\n\n<p>\u200b\u8be5\u4f8b\u5b50\u4e2d<code>getForEntity()<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u6211\u8981\u8c03\u7528\u670d\u52a1\u7684<code>URL<\/code>\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5219\u4e3a\u54cd\u5e94\u5185\u5bb9\u7684\u7c7b\u7684\u7c7b\u578b\uff08Java\u561b \u4e07\u7269\u7686\u5bf9\u8c61\uff09\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570 \u4ee3\u8868\u7740\u8c03\u7528\u670d\u52a1\u65f6\u7684\u4f20\u53c2\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"147\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-1024x147.png\" alt=\"\" class=\"wp-image-11885\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-1024x147.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-300x43.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-768x110.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-830x119.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-230x33.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-350x50.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160-480x69.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-160.png 1312w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\u53ef\u4ee5\u4f7f\u7528key-value\u7684map\u6765\u4f20\u5165\u53c2\u6570<\/p>\n\n\n\n<p>\u200bget\u8bf7\u6c42\u4e5f\u53ef\u901a\u8fc7\u5411\u5728url\u4e0a\u6dfb\u52a0\u67e5\u8be2\u53c2\u6570\u6765\u53d1\u9001\u5e26\u6709\u8bf7\u6c42\u7684\u53c2\u6570<\/p>\n\n\n\n<p><code><strong>getForObject()<\/strong><\/code><\/p>\n\n\n\n<p>\u76f8\u6bd4\u4e8e\u524d\u8005<code>getForEntity()<\/code>\u8be5\u65b9\u6cd5\u5219\u662f\uff0c\u66f4\u504f\u5411\u4e8e\u76f4\u63a5\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u7684\uff0c\u56e0\u4e3a\u4ed6\u76f4\u63a5\u8fd4\u56de\u54cd\u5e94\u5b9e\u4f53\u7684<code>body<\/code>\uff08\u54cd\u5e94\u5185\u5bb9\uff09\uff0c\u3002\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void queryWeather() {\n        \n        Object body = restTemplate.getForObject(\"https:\/\/restapi.amap.com\/v3\/weather\/weatherInfo?city=510100&amp;key=e7a5fa943f706602033b6b329c49fbc6\", Object.class);\n        System.out.println(body);\n    }\n<\/pre>\n\n\n\n<p>\u65b9\u6cd5\u53c2\u6570\u7b7e\u540d\u4e0e`getForEntity()`\u57fa\u672c\u4e00\u81f4\u3002<\/p>\n\n\n\n<p>\u5f53\u4f60\u53ea\u9700\u8981\u8fd4\u56de\u7684\u54cd\u5e94\u5185\u5bb9\u65f6\uff0c\u4f7f\u7528<code>getForObject()<\/code>\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u4f46\u5f53\u4f60\u9700\u8981\u83b7\u5f97\u66f4\u8be6\u7ec6\u7684\u54cd\u5e94\u4fe1\u606f\uff0c\u5982\u54cd\u5e94\u5934\u4e2d\u7684\u4fe1\u606f\uff0c\u4f60\u5c31\u53ea\u80fd\u9009\u62e9<code>getForEntity()<\/code>\u4e86\u3002<\/p>\n\n\n\n<p><strong>POST<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u200b <code>POST<\/code>\u8bf7\u6c42\u6709\u5982\u4e0b\u4e09\u79cd\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public URI postForLocation(...)\n\npublic &lt;T> T postForObject(...)\n\npublic &lt;T> ResponseEntity&lt;T> postForEntity(...)<\/pre>\n\n\n\n<p>\u540e\u4e24\u79cd\u7528\u6cd5\u4e0e<code>GET<\/code>\u57fa\u672c\u4e00\u81f4\u4e0d\u505a\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u8fd9\u91cc\u7740\u91cd\u4ecb\u7ecd<code>postForLocation()<\/code><\/p>\n\n\n\n<p><code><strong>postForEntity()<\/strong><\/code><\/p>\n\n\n\n<p>\u8be5\u65b9\u6cd5\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u4e3a\u8c03\u7528\u670d\u52a1\u7684\u5730\u5740\uff08URL\uff09<\/p>\n\n\n\n<p>\u200b \u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u4e0a\u4f20\u7684\u53c2\u6570(json\u683c\u5f0f\u63d0\u4ea4)<\/p>\n\n\n\n<p>\u200b \u7b2c\u4e09\u4e2a\u8868\u793a\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9\u7684\u5177\u4f53\u7c7b\u578b<\/p>\n\n\n\n<p>\u200b \u7b2c\u56db\u4e2a\u53c2\u6570\u4e5f\u7528\u4e8e\u6307\u5b9a\u53c2\u6570\uff08\u5728URL\u4e2d\u6dfb\u52a0\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\n    public void queryWeather() {\n        User user = new User();\n        user.setName(\"\u9c81\u5927\u5e08\");\n        ResponseEntity&lt;Object> objectResponseEntity = restTemplate.postForEntity(\"https:\/\/restapi.amap.com\/v3\/weather\/weatherInfo?city=510100&amp;key=e7a5fa943f706602033b6b329c49fbc6\", user, Object.class);\n        System.out.println(\"\u6d88\u606f\u54cd\u5e94\u5185\u5bb9:\"+objectResponseEntity.getBody());\n    }\n<\/pre>\n\n\n\n<p><code><strong>postForObject()<\/strong><\/code><\/p>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\u4e0e<code>getForObject<\/code>\u7c7b\u4f3c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u4f20\u5165\u5bf9\u8c61\u53c2\u6570\uff08\u4f20\u5165\u65b9\u5f0f\u4e0e<code>postForEntity()<\/code>\u76f8\u540c\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void queryWeather() {\n        User user = new User();\n        user.setName(\"\u9c81\u5927\u5e08\");\n        ResponseEntity&lt;Object> objectResponseEntity = restTemplate.postForEntity(\"https:\/\/httpbin.org\/post\", user, Object.class);\n        MediaType contentType = objectResponseEntity.getHeaders().getContentType();\n        System.out.println(contentType);\n        System.out.println(\"\u6d88\u606f\u54cd\u5e94\u5185\u5bb9:\"+objectResponseEntity.getBody());\n    }<\/pre>\n\n\n\n<p><code><strong>postForLocation()<\/strong><\/code><\/p>\n\n\n\n<p><code>postForLocation<\/code>\u4f20\u53c2\u7528\u6cd5\u4e0e\u524d\u4e24\u8005\u4e00\u81f4\uff0c\u53ea\u4e0d\u8fc7\u8fd4\u56de\u4ece\u5b9e\u4f53\u53d8\u6210\u4e86\u4e00\u4e2a<code>URL<\/code>\uff0c\u56e0\u6b64\u5b83\u4e0d\u9700\u8981\u6307\u5b9a\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void queryWeather() {\n        User user = new User();\n        user.setName(\"\u9c81\u5927\u5e08\");\n        URI uri = restTemplate.postForLocation(\"https:\/\/httpbin.org\/post\", user);\n        System.out.println(uri);\n    }\n<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u53ea\u9700\u8981\u670d\u52a1\u63d0\u4f9b\u8005\u8fd4\u56de\u4e00\u4e2a URI \u5373\u53ef\uff0c\u8be5<code>URI<\/code>\u8fd4\u56de\u503c\u4f53\u73b0\u7684\u662f\uff1a\u7528\u4e8e\u63d0\u4ea4\u5b8c\u6210\u6570\u636e\u4e4b\u540e\u7684\u9875\u9762\u8df3\u8f6c\uff0c\u6216\u6570\u636e\u63d0\u4ea4\u5b8c\u6210\u4e4b\u540e\u7684\u4e0b\u4e00\u6b65\u6570\u636e\u64cd\u4f5c<code>URI<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528POST\u4ee5\u8868\u5355\u65b9\u5f0f\u63d0\u4ea4<\/strong><\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u7740\u91cd\u8bf4\u4e00\u4e0b\uff0c\u5982\u4f55\u81ea\u5df1\u5c01\u88c5\u4e00\u4e2a\u8bf7\u6c42\u4f53\u3002<\/p>\n\n\n\n<p>\u200b \u6211\u4eec\u9700\u8981\u7528\u5230\u5982\u4e0b\u51e0\u4e2a\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">HttpHeaders\n\nMultiValueMap&lt;K,V>\n\nHttpEntity&lt;T>\n<\/pre>\n\n\n\n<p><code><strong>HttpHeaders<\/strong><\/code><\/p>\n\n\n\n<p>\u6545\u540d\u601d\u610f\uff0c\u5c31\u662f\u7528\u6765\u5c01\u88c5Http\u8bf7\u6c42\u7684\u8bf7\u6c42\u5934\u7684\uff0c\u8fd9\u91cc\u6211\u4eec\u8981\u8bbe\u7f6e\u4ed6\u7684<code>ContentType<\/code>\u4e3a**<code>MediaType.APPLICATION_FORM_URLENCODED<\/code>**\u4ee5\u4f7f\u5f97\u6211\u4eec\u63d0\u4ea4\u7684\u53c2\u6570\u662f\u4ee5Form(\u8868\u5355)\u7684\u5f62\u5f0f\u63d0\u4ea4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u8bbe\u7f6e\u8bf7\u6c42\u5934, x-www-form-urlencoded\u683c\u5f0f\u7684\u6570\u636e\nHttpHeaders httpHeaders = new HttpHeaders();\n\n\/\/\u8fd9\u91cc\u6307\u5b9a\u53c2\u6570\u4ee5UTF-8\u7f16\u7801\u683c\u5f0f\u4f20\u8f93\nMediaType mediaType = new MediaType(MediaType.APPLICATION_FORM_URLENCODED, UTF_8);\nhttpHeaders.setContentType(mediaType);\n        \n\/\/\u63d0\u4ea4\u53c2\u6570\u8bbe\u7f6e\nMultiValueMap&lt;String, String> map = new LinkedMultiValueMap&lt;>();\nmap.add(\"name\",\"\u9c81\u5927\u5e08\");\n<\/pre>\n\n\n\n<p><code><strong>MultiValueMap&lt;K,V&gt;<\/strong><\/code><\/p>\n\n\n\n<p>\u8be5\u7c7b\u662f\u7528\u6765\u5c01\u88c5\u8bf7\u6c42\u53c2\u6570\u7684\uff0c\u662f\u4ee5<code>key-value<\/code>\u7684\u5f62\u5f0f\u5c01\u88c5\u4f46\u662f\u4ee5\u5355\u4e2akey\u5bf9\u5e94\u591a\u4e2avalue\u7684\u683c\u5f0f\u4f20\u8f93(\u4e5f\u5c31\u662f\u662f\u4ee5\u5355\u4e2a<code>key:[value...]<\/code>\u7684\u683c\u5f0f\u4f20\u8f93\u7684)\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u63d0\u4ea4\u53c2\u6570\u8bbe\u7f6e\nMultiValueMap&lt;String, String> map = new LinkedMultiValueMap&lt;>();\nmap.add(\"name\",\"\u9c81\u5927\u5e08\");\n<\/pre>\n\n\n\n<p>\u5982\u679c\u50cf\u4f20\u8f93\u5355\u4e2a<code>key<\/code>\u5bf9\u5e94\u5355\u4e2a<code>value<\/code>\u4f7f\u7528\u666e\u901a\u7684<code>Map<\/code>\u4f20\u53c2\u5373\u53ef<\/p>\n\n\n\n<p><code><strong>HttpEntity&lt;T&gt;<\/strong><\/code><\/p>\n\n\n\n<p>\u8be5\u7c7b\u662f\u7528\u6765\u5c01\u88c5\u8bf7\u6c42\u7684\uff0c\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u5c06\u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u4f53\u5c01\u88c5\u5728\u4e00\u8d77\u6210\u4e3a\u4e00\u4e2a\u8bf7\u6c42\u5b9e\u4f53 T\u7528\u6765\u6307\u5b9a\u7528\u6765\u5c01\u88c5\u53c2\u6570\u7684\u5bb9\u5668\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u7ec4\u88c5\u8bf7\u6c42\u4f53\nHttpEntity&lt;MultiValueMap&lt;String, String>> request = new HttpEntity&lt;>(map, httpHeaders);<\/pre>\n\n\n\n<p><strong>\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u81ea\u5df1\u5c01\u88c5\u4e00\u4e2a\u4ee5form\u5f62\u5f0f\u63d0\u4ea4\u53c2\u6570\u7684<code>POST<\/code>\u8bf7\u6c42\u4e86\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\n    void contextLoads() {\n        \/\/\u8bf7\u6c42\u5730\u5740\n        String url = \"https:\/\/httpbin.org\/post\";\n\n        \/\/\u8bbe\u7f6e\u8bf7\u6c42\u5934, x-www-form-urlencoded\u683c\u5f0f\u7684\u6570\u636e\n        HttpHeaders httpHeaders = new HttpHeaders();\n        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);\n\n        \/\/\u63d0\u4ea4\u53c2\u6570\u8bbe\u7f6e\n        MultiValueMap&lt;String, String> map = new LinkedMultiValueMap&lt;>();\n        map.add(\"name\",\"\u9c81\u5927\u5e08\");\n\n        \/\/\u7ec4\u88c5\u8bf7\u6c42\u4f53\n        HttpEntity&lt;MultiValueMap&lt;String, String>> request = new HttpEntity&lt;>(map, httpHeaders);\n\n\n        \/\/\u53d1\u9001post\u8bf7\u6c42\u5e76\u6253\u5370\u7ed3\u679c \u4ee5String\u7c7b\u578b\u63a5\u6536\u54cd\u5e94\u7ed3\u679cJSON\u5b57\u7b26\u4e32\n        String s = restTemplate.postForObject(url, request, String.class);\n        System.out.println(s);\n    }\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"58\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-1024x58.png\" alt=\"\" class=\"wp-image-11887\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-1024x58.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-300x17.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-768x43.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-830x47.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-230x13.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-350x20.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161-480x27.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-161.png 1316w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u200b \u901a\u8fc7\u62e6\u622a\u5668\u62e6\u622a\u4e86\u8bf7\u6c42\u5e76\u5bf9\u8bf7\u6c42\u5934\u8fdb\u884c\u62c6\u5305\uff0c\u53ef\u4ee5\u53d1\u73b0<code>ContentType<\/code>\u5df2\u7ecf\u88ab\u4fee\u6539\u6210\u4e86<code>x-www-form-urlencoded<\/code>\u683c\u5f0f\u4e86\u3002<\/p>\n\n\n\n<p><strong>PUT<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u200b <code>PUT<\/code>\u8bf7\u6c42\u7684\u65b9\u6cd5\u53ea\u6709\u4e00\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void put()<\/pre>\n\n\n\n<p><code><strong>PUT()<\/strong><\/code><\/p>\n\n\n\n<p>\u200b\u4f7f\u7528\u65b9\u6cd5\u4e0e<code>postForEntity()<\/code>\u53c2\u6570\u57fa\u672c\u4e00\u81f4\uff0c\u53ea\u662f<code>put<\/code>\u65b9\u6cd5\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u4e5f\u5c31\u4e0d\u5fc5\u53bb\u8bbe\u7f6e\u54cd\u5e94\u5185\u5bb9\u7684\u7c7b\u578b\u4e86\uff09\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\n    void contextLoads() {\n        \/\/\u8bf7\u6c42\u5730\u5740\n        String url = \"http:\/\/httpbin.org\/put\";\n        User user = new User();\n        user.setName(\"\u9c81\u5927\u5e08\");\n        restTemplate.put(url,user);\n    }\n\n<\/pre>\n\n\n\n<p><strong>DELETE<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e0e<code>PUT<\/code>\u4e00\u6837\uff0c<code>DELETE<\/code>\u65b9\u6cd5\u53ea\u6709\u4e00\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void delete()\n<\/pre>\n\n\n\n<p><code><strong>delete()<\/strong><\/code><\/p>\n\n\n\n<p><code>delete()<\/code>\u53ef\u4ee5\u6307\u5b9a<code>url<\/code>\u4e2d\u7684\u4e2d\u7684\u53c2\u6570\uff0c\u4f46\u662f<code>RestTemplate<\/code>\u7684<code>delete()<\/code>\u65b9\u6cd5\u662f\u4e0d\u652f\u6301\u4e0a\u4f20<code>requestBody<\/code>\u7684\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void contextLoads() {\n    \/\/\u8bf7\u6c42\u5730\u5740\n    String url = \"http:\/\/httpbin.org\/delete\";\n    restTemplate.delete(url);\n}\n<\/pre>\n\n\n\n<p><strong>HEADER<\/strong><\/p>\n\n\n\n<p>\u200b<code>HEADER<\/code>\u4e5f\u53ea\u6709\u4e00\u7c7b\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public HttpHeaders headForHeaders()\n<\/pre>\n\n\n\n<p>\u4e3b\u8981\u7528\u6765\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6\u54cd\u5e94\u5934\u90e8\u4fe1\u606f\uff0c\u4f46\u662f\u50cf<code>DELETE<\/code>\u3001<code>PUT<\/code>\u8fd9\u7c7b\u6ca1\u6709\u54cd\u5e94\u7684\u65b9\u6cd5\uff0c\u662f\u4e0d\u80fd\u4f7f\u7528\u8be5\u65b9\u6cd5\u7684\uff08\u56e0\u4e3a\u6ca1\u6709\u54cd\u5e94\u4e5f\u5c31\u6ca1\u6709\u54cd\u5e94\u5934\u4e86\uff09\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\n    void contextLoads() {\n        \/\/\u8bf7\u6c42\u5730\u5740\n        String url = \"http:\/\/httpbin.org\/get\";\n        HttpHeaders httpHeaders = restTemplate.headForHeaders(url);\n        System.out.println(httpHeaders);\n    }\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-1024x59.png\" alt=\"\" class=\"wp-image-11889\" width=\"668\" height=\"38\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-1024x59.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-300x17.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-768x44.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-830x48.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-230x13.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-350x20.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162-480x28.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-162.png 1312w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><\/figure><\/div>\n\n\n\n<p><strong>OPTIONS<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public Set&lt;HttpMethod> optionsForAllow()\n<\/pre>\n\n\n\n<p>\u8be5\u65b9\u6cd5\u7684\u4e3b\u8981\u7528\u6765\u5224\u65ad\u8be5\u670d\u52a1\u5730\u5740\uff0c\u80fd\u591f\u4f7f\u7528\u90a3\u79cd\u65b9\u6cd5\u53bb\u6267\u884c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\t@Test\n    void contextLoads() {\n        \/\/\u8bf7\u6c42\u5730\u5740\n        String url = \"http:\/\/httpbin.org\/get\";\n        Set&lt;HttpMethod> httpMethods = restTemplate.optionsForAllow(url);\n        System.out.println(httpMethods);\n    }\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-163.png\" alt=\"\" class=\"wp-image-11890\" width=\"242\" height=\"49\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-163.png 404w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-163-300x61.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-163-230x47.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-163-350x71.png 350w\" sizes=\"(max-width: 242px) 100vw, 242px\" \/><\/figure><\/div>\n\n\n\n<p><strong>EXCHANGE<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;T> ResponseEntity&lt;T> exchange()\n<\/pre>\n\n\n\n<p>\u200b\u8be5\u63a5\u53e3\u4e0e\u5176\u4ed6\u63a5\u53e3\u4e0d\u540c<\/p>\n\n\n\n<ul><li>\u8be5\u65b9\u6cd5\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u8bf7\u6c42\u7684\u65b9\u6cd5\uff08get,post,put\u7b49\uff09<\/li><li>\u53ef\u4ee5\u5728\u8bf7\u6c42\u4e2d\u589e\u52a0body\u4ee5\u53ca\u5934\u4fe1\u606f\uff0c\u5176\u5185\u5bb9\u901a\u8fc7\u53c2\u6570HttpEntity requestEntity\u63cf\u8ff0<\/li><li>exchange\u652f\u6301\u2019\u542b\u53c2\u6570\u7684\u7c7b\u578b(\u5373\u6cdb\u578b)&#8217;\u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\uff0c\u8be5\u7279\u6027\u901a\u8fc7ParameterizedTypeReferenceresponseType \u63cf\u8ff0<\/li><\/ul>\n\n\n\n<p>\u200b\u8be5\u65b9\u6cd5\u652f\u6301\u4e94\u4e2a\u53c2\u6570<\/p>\n\n\n\n<ul><li>\u7b2c\u4e00\u4e2a\u662f\u670d\u52a1\u5730\u5740<\/li><li>\u7b2c\u4e8c\u4e2a\u662f\u8bf7\u6c42\u65b9\u6cd5<\/li><li>\u7b2c\u4e09\u4e2a\u662f\u5199\u5165\u7684\u8bf7\u6c42\u5b9e\u4f53<\/li><li>\u7b2c\u56db\u4e2a\u662f\u54cd\u5e94\u5185\u5bb9\u7684\u7c7b\u578b<\/li><li>\u7b2c\u4e94\u4e2a\u662f\u6269\u5c55\u6a21\u677f\u7684\u53d8\u91cf\u6216\u5305\u542b<code>URI<\/code>\u6a21\u677f\u53d8\u91cf\u7684\u6620\u5c04<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\nvoid contextLoads() {\n    \/\/\u8bf7\u6c42\u5730\u5740\n    String url = \"http:\/\/httpbin.org\/post\";\n    User user = new User();\n    user.setName(\"\u5f6d\u4e8e\u664f\");\n    HttpHeaders httpHeaders = new HttpHeaders();\n    httpHeaders.setContentType(MediaType.APPLICATION_JSON);\n    HttpEntity&lt;User> userHttpEntity = new HttpEntity&lt;>(user, httpHeaders);\n    ResponseEntity&lt;Object> exchange = restTemplate.exchange(url, HttpMethod.POST, userHttpEntity, Object.class);\n    System.out.println(exchange);\n}\n<\/pre>\n\n\n\n<p>\u200b \u4e0a\u8ff0\u4ee3\u7801\u6a21\u62df\u4e86\u4e00\u4e2a\u7b80\u5355\u7684<code>POST<\/code>\u8bf7\u6c42 <strong>\u53ef\u4ee5\u7406\u89e3\u4e3a\u53ef\u4ee5\u52a8\u6001\u7684\u6307\u5b9a\u8bf7\u6c42\u65b9\u6cd5\u548c\u8bf7\u6c42\u5b9e\u4f53\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/strong><\/p>\n\n\n\n<p>\u54cd\u5e94\u5b9e\u4f53<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"210\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-1024x210.png\" alt=\"\" class=\"wp-image-11891\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-1024x210.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-300x62.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-768x158.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-830x171.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-230x47.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-350x72.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164-480x99.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-164.png 1314w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>EXECUTE<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;T> T execute()\n<\/pre>\n\n\n\n<p>\u200b\u8be5\u65b9\u6cd5\u5c31\u662f\u6267\u884c\u8bf7\u6c42\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u4e0a\u8ff0\u7684\u6240\u6709\u65b9\u6cd5\u7684\u6700\u540e\u6267\u884c\u90fd\u662f\u8c03\u7528\u7684\u8be5\u65b9\u6cd5\u6267\u884c\uff0c\u6240\u4ee5\u4ed6\u5728<code>RestTemplate<\/code>\u4e2d\u5341\u5206\u91cd\u8981<\/p>\n\n\n\n<ul><li>\u670d\u52a1\u5730\u5740<\/li><li>\u8bf7\u6c42\u7684\u65b9\u6cd5<\/li><li>\u51c6\u5907\u8bf7\u6c42\u7684\u5bf9\u8c61\uff08<code>requestCallback<\/code>\uff09<\/li><li>\u4ece\u54cd\u5e94\u4e2d\u63d0\u53d6\u8fd4\u56de\u503c\u7684\u5bf9\u8c61<\/li><li>\u6269\u5c55\u6a21\u677f\u7684\u53d8\u91cf\u6216\u5305\u542b<code>URI<\/code>\u6a21\u677f\u53d8\u91cf\u7684\u6620\u5c04<\/li><\/ul>\n\n\n\n<p><code>execute()<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Override\n\t@Nullable\n\tpublic &lt;T> T execute(String url, HttpMethod method, @Nullable RequestCallback requestCallback,\n\t\t\t@Nullable ResponseExtractor&lt;T> responseExtractor, Object... uriVariables) throws RestClientException {\n\n\t\tURI expanded = getUriTemplateHandler().expand(url, uriVariables);\n\t\treturn doExecute(expanded, method, requestCallback, responseExtractor);\n\t}\n\n<\/pre>\n\n\n\n<p>\u200b\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0<code>execute()<\/code>\u65b9\u6cd5\u53ea\u662f\u5c06\u6211\u4eec\u4f20\u5165\u7684<code>String<\/code>\u7c7b\u578b\u7684<code>URL<\/code>\u8f6c\u6362\u4e3a\u4e86<code>URL<\/code>\u7c7b\u578b\uff0c\u6700\u540e\u6267\u884c\u8bf7\u6c42\u662f\u7531<code>doExecute()<\/code>\u65b9\u6cd5<\/p>\n\n\n\n<p><code><strong>doExecute()<\/strong><\/code><\/p>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u4e86\u89e3\u4e24\u4e2a\u7c7b\uff1a<code>RequestCallback<\/code>\u548c<code>ResPonseExtractor<\/code><\/p>\n\n\n\n<p>\u200b<code>RequestCallback<\/code>: <strong>\u7528\u4e8e\u64cd\u4f5c\u8bf7\u6c42\u5934\u548cbody\uff0c\u5728\u8bf7\u6c42\u53d1\u51fa\u524d\u6267\u884c\u3002\u4e0d\u9700\u8981\u5173\u5fc3\u5173\u95ed\u8bf7\u6c42\u6216\u5904\u7406\u9519\u8bef\uff1a\u8fd9\u90fd\u5c06\u7531RestTemplate\u5904\u7406\u3002<\/strong><\/p>\n\n\n\n<p>\u200b\u8be5\u63a5\u53e3\u6709\u4e24\u4e2a\u5b9e\u73b0\u7c7b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-1024x130.png\" alt=\"\" class=\"wp-image-11892\" width=\"514\" height=\"65\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-1024x130.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-300x38.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-768x97.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-830x105.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-230x29.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-350x44.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165-480x61.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-165.png 1186w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/figure><\/div>\n\n\n\n<p><code>ResPonseExtractor<\/code>: <strong>\u89e3\u6790HTTP\u54cd\u5e94\u7684\u6570\u636e\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u62c5\u5fc3\u5f02\u5e38\u548c\u8d44\u6e90\u7684\u5173\u95ed\u3002<\/strong><\/p>\n\n\n\n<p>\u200b\u8be5\u63a5\u53e3\u5728<code>RestTemplate<\/code>\u4e2d\u540c\u6837\u6709\u4e24\u4e2a\u5b9e\u73b0\u7c7b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-1024x129.png\" alt=\"\" class=\"wp-image-11893\" width=\"538\" height=\"67\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-1024x129.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-300x38.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-768x97.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-830x104.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-230x29.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-350x44.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166-480x60.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-166.png 1320w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\nvoid contextLoads() {\n    \/\/\u8bf7\u6c42\u5730\u5740\n    String url = \"http:\/\/httpbin.org\/post\";\n    User user = new User();\n    user.setName(\"\u5f6d\u4e8e\u664f\");\n    HttpHeaders httpHeaders = new HttpHeaders();\n    httpHeaders.setContentType(MediaType.APPLICATION_JSON);\n    HttpEntity&lt;User> userHttpEntity = new HttpEntity&lt;>(user, httpHeaders);\n    ResponseEntity&lt;Object> execute = restTemplate.execute(url, HttpMethod.POST, restTemplate.httpEntityCallback(userHttpEntity), restTemplate.responseEntityExtractor(Object.class));\n    System.out.println(execute);\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-1024x197.png\" alt=\"\" class=\"wp-image-11894\" width=\"559\" height=\"107\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-1024x197.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-300x58.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-768x148.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-830x160.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-230x44.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-350x67.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167-480x92.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-167.png 1332w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u89e3\u60d1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u524d\u9762\u6211\u4eec\u4ecb\u7ecd\u65b9\u6cd5\u7684\u65f6\u5019\u53d1\u73b0\u6709\u4e2a\u4e00\u4e2a\u53ef\u53d8\u53c2\u6570\uff0c\u90a3\u4e2a\u53c2\u6570\u88ab\u63cf\u8ff0\u6210\u4e86<strong>\u6269\u5c55\u6a21\u677f\u7684\u53d8\u91cf\u6216\u662f\u5305\u542b<code>URI<\/code>\u6a21\u677f\u53d8\u91cf\u7684\u6620\u5c04<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u6765\u7b80\u5355\u770b\u4e00\u4e0b\u8fd9\u4e2a\u53c2\u6570\uff0c\u6211\u4eec\u77e5\u9053\u8bf7\u6c42\u4f20\u53c2\u53ef\u4ee5\u901a\u8fc7<code>url<\/code>\u62fc\u63a5\u53c2\u6570\u7684\u65b9\u5f0f\u4f20\u53c2\uff0c\u62fc\u63a5\u53c2\u6570\u4e5f\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u8def\u5f84\u4e2d\u5d4c\u5165\u5360\u4f4d\u7684\u683c\u5f0f\uff08http:\/\/httpbin.org\/{1}\/post\uff09\u4e5f\u53eb\u6a21\u677f\u6620\u5c04\n\u672b\u5c3e\u6dfb\u52a0Key-value\u683c\u5f0f\uff08http:\/\/httpbin.org\/post?name=\"\u5f6d\u4e8e\u664f\"\uff09\u5373\u6269\u5c55\u6a21\u677f\u7684\u53d8\u91cf\n<\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u6700\u540e\u4e00\u53c2\u6570\u4f20\u5165map\u65f6\u4f1a\u4ee5<code>key-value<\/code>\u7684\u683c\u5f0f\u62fc\u63a5\u5728<code>URL<\/code>\u540e\uff08\u901a\u4fd7\u7684\u8bf4\u5c31\u662f\u8fd9\u6837\u8bbe\u7f6e\u7684\u53d8\u91cf\u4f1a\u8ddf\u7740<code>URL<\/code>\u8def\u5f84\u540e\u9762\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u200bhttp:\/\/httpbin.org\/post?name=\"\u5f6d\u4e8e\u664f\"<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\nvoid contextLoads() {\n    \/\/\u8bf7\u6c42\u5730\u5740\n    String url = \"http:\/\/httpbin.org\/get\";\n    HashMap&lt;String, String> map = new HashMap&lt;>();\n    map.put(\"name\",\"\u5f6d\u4e8e\u664f\");\n    Object forObject = restTemplate.getForObject(url, Object.class, map);\n    System.out.println(forObject);\n}\n<\/pre>\n\n\n\n<p>\u5f53\u6211\u4eec\u4f20\u5165\u7b80\u5355\u7684\u5bf9\u8c61\u5982String\uff0cInteger\u65f6\u4e14\u8def\u5f84\u4e2d\u6709\u5d4c\u5165\u7684\u5360\u4f4d\u7b26\u65f6\u5c31\u4f1a\u4ee3\u66ff\u8c03\u7528URL\u4e2d\u5360\u4f4d\u7b26<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Test\nvoid contextLoads() {\n    \/\/\u8bf7\u6c42\u5730\u5740\n    String url = \"http:\/\/httpbin.org\/{2}\/get\";\n    HashMap&lt;String, String> map = new HashMap&lt;>();\n    Object forObject = restTemplate.getForObject(url, Object.class, 99);\n    System.out.println(forObject);\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-168.png\" alt=\"\" class=\"wp-image-11895\" width=\"346\" height=\"52\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-168.png 524w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-168-300x45.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-168-230x34.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-168-350x52.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/08\/\u56fe\u7247-168-480x71.png 480w\" sizes=\"(max-width: 346px) 100vw, 346px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/blog.csdn.net\/D1842501760\/article\/details\/12 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11872"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11872"}],"version-history":[{"count":11,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11872\/revisions"}],"predecessor-version":[{"id":11941,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11872\/revisions\/11941"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}