九阴真经


RestTemplate基本请求封装

<h5>核心配置,注入RestTemplate为Bean</h5> <pre><code>package com.example.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class HttpConfig { /** * 两分钟超时时间 */ private int outtime = 2 * 60 * 1000; @Bean public RestTemplate restTemplate() { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(outtime); requestFactory.setReadTimeout(outtime); return new RestTemplate(); } } </code></pre> <h5>封装GET/POST请求</h5> <pre><code>package com.example.demo.service; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.Map; /** * Http请求Service */ @Service public class HttpService { @Autowired private RestTemplate restTemplate; /** * get请求 * * @param url 请求地址 * @param headerMap 头部信息 * @param resp 响应结果类型 * @return */ public Object get(String url, Map&lt;String, String&gt; headerMap, Class&lt;?&gt; resp) { HttpHeaders httpHeaders = new HttpHeaders(); for (Map.Entry&lt;String, String&gt; stringStringEntry : headerMap.entrySet()) { httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue()); } HttpEntity httpEntity = new HttpEntity(httpHeaders); ResponseEntity&lt;?&gt; result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, resp); return result.getBody(); } /** * post 请求 * * @param url 请求地址 * @param headerMap 头信息 * @param jsonObject 请求参数 JSON * @return JSONObject */ public JSONObject post(String url, Map&lt;String, String&gt; headerMap, JSONObject jsonObject) { HttpHeaders httpHeaders = new HttpHeaders(); for (Map.Entry&lt;String, String&gt; stringStringEntry : headerMap.entrySet()) { httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue()); } HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders); JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class); return result; } } </code></pre> <h5>测试HttpService</h5> <pre><code> /** * 测试get */ @Test public void getService() { Map&lt;String, String&gt; map = new HashMap&lt;&gt;(); map.put("abc", "23434"); String obj = (String) httpService.get("http://127.0.0.1:8080/b", map, String.class); System.out.println(obj); } /** * 测试post */ @Test public void postService() { Map&lt;String, String&gt; map = new HashMap&lt;&gt;(); map.put("abc", "54545"); JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "孙悟空"); JSONObject obj = httpService.post("http://127.0.0.1:8080/a", map, jsonObject); System.out.println(obj); }</code></pre>

页面列表

ITEM_HTML