{"id":911,"date":"2023-03-11T17:02:45","date_gmt":"2023-03-11T09:02:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=911"},"modified":"2023-04-29T15:50:26","modified_gmt":"2023-04-29T07:50:26","slug":"spring-boot-use-resttemplate-to-send-get-request-but-cannot-obtain-parameters","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-boot-use-resttemplate-to-send-get-request-but-cannot-obtain-parameters\/","title":{"rendered":"Spring Boot\u4f7f\u7528RestTemplate\u53d1\u9001get\u8bf7\u6c42\uff0c\u83b7\u53d6\u4e0d\u5230\u53c2\u6570\u7684\u95ee\u9898"},"content":{"rendered":"<h2>\u9519\u8bef\u6848\u4f8b<\/h2>\n<p>\u4f7f\u7528<code>RestTemplate<\/code>\u53d1\u9001GET\u8bf7\u6c42\uff0c\u53d1\u73b0\u540e\u7aef\u63a5\u6536\u4e0d\u5230\u8bf7\u6c42\u53c2\u6570<\/p>\n<pre><code class=\"language-java\">String url = &quot;http:\/\/api.xxx.xxx&quot;;\n\/\/\u6a21\u62df\u8bf7\u6c42\u53c2\u6570\nMap map = new HashMap&lt;&gt;();\nmap.put(&quot;name&quot;, &quot;Joe.Ye&quot;);\nmap.put(&quot;age&quot;, &quot;18&quot;);\n\nString result = restTemplate.getForObject(url, String.class, map);<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u53d1\u73b0\u4e00\u76f4\u62ff\u4e0d\u5230name\u548cage<\/p>\n<h2>\u6b63\u786e\u59ff\u52bf<\/h2>\n<h3>URL\u62fc\u63a5query\u53c2\u6570<\/h3>\n<pre><code class=\"language-java\">String url = &quot;http:\/\/api.xxx.xxx?name={name}&amp;age={age}&quot;;<\/code><\/pre>\n<h3>UriCompoentBuilder\u65b9\u5f0f\uff08\u4f18\u96c5\u3001\u63a8\u8350\uff09<\/h3>\n<pre><code class=\"language-java\">HttpHeaders headers = new HttpHeaders();\nheaders.add(&quot;accept&quot;, &quot;*\/*&quot;);\nHttpEntity&lt;JSONObject&gt; httpEntity = new HttpEntity&lt;JSONObject&gt;(null, headers);\n\nUriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)\n        .queryParam(&quot;id&quot;, &quot;10&quot;);\n\nResponseEntity&lt;JSONObject&gt; response = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, httpEntity, JSONObject.class);<\/code><\/pre>\n<pre><code class=\"language-java\">String url = &quot;https:\/\/www.appblog.cn&quot;;\nUriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);\nString url2 = builder.queryParam(&quot;name&quot;, &quot;Joe.Ye&quot;).build().encode().toString();\nSystem.out.println(&quot;url2 = &quot; + url2); \/\/https:\/\/www.appblog.cn?name=Joe.Ye\n\nMap&lt;String,String&gt; param = Maps.newHashMap();\nparam.put(&quot;name&quot;, &quot;Joe.Ye&quot;);\nparam.put(&quot;age&quot;, &quot;18&quot;);\nparam.entrySet().stream().forEach(o -&gt; builder.queryParam(o.getKey(),o.getValue()));\nString url3 = builder.build().encode().toString(); \/\/https:\/\/www.appblog.cn?name=Joe.Ye&amp;age=18\nSystem.out.println(&quot;url3 = &quot; + url3);\n\n\/\/ResponseEntity&lt;String&gt; rsp = restTemplate.getForEntity(url3, String.class);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9519\u8bef\u6848\u4f8b \u4f7f\u7528RestTemplate\u53d1\u9001GET\u8bf7\u6c42\uff0c\u53d1\u73b0\u540e\u7aef\u63a5\u6536\u4e0d\u5230\u8bf7\u6c42\u53c2\u6570 String url = &#038;q [&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],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-resttemplate"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/911","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=911"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/911\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}