直接上代碼:
1、定義配置類
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class HttpApiConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory) { return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000); factory.setConnectTimeout(5000); return factory; } }
2、定義服務
import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.Map; @Service public class HttpUtils { private final RestTemplate restTemplate; public HttpUtils(RestTemplate restTemplate) { this.restTemplate = restTemplate; } // Get請求 public String Get(String url, Map<String, Object> params) { String uri = buildUri(url, params); ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class); return response.getBody(); } // Post請求(JSON請求頭) public String JPost(String url, Map<String, Object> params) { HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, jsonHeaderBuilder()); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); return response.getBody(); } // Post請求(URL請求頭) public String UPost(String url, MultiValueMap<String, String> params) { HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, urlHeaderBuilder()); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); return response.getBody(); } // 拼接Url和參數 private String buildUri(String url, Map<String, Object> params) { StringBuilder sb = new StringBuilder(url); sb.append("?"); for (Map.Entry<String, Object> entry : params.entrySet()) { sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } // 構建Url請求頭 private HttpHeaders urlHeaderBuilder() { HttpHeaders h = new HttpHeaders(); h.setContentType(MediaType.APPLICATION_FORM_URLENCODED); return h; } // 構建Json請求頭 private HttpHeaders jsonHeaderBuilder() { HttpHeaders h = new HttpHeaders(); h.setContentType(MediaType.APPLICATION_JSON); return h; } }
3、實例(參考)
POST請求,參數帶在URL上
MultiValueMap<String, String> requestPara = new LinkedMultiValueMap<>(); requestPara.add("id", id); requestPara.add("name", name); StringBuilder sb = new StringBuilder(baseUrl); sb.append("/oh/yeah"); String response = httpUtils.UPost(sb.toString(), requestPara);
GET和JPOST請求是常用的,參數添加與UPost類似,類型變一下。
Map<String, Object> requestPara = new HashMap<>(); requestPara.put("id", id); requestPara.put("name", name); StringBuilder sb = new StringBuilder(baseUrl); sb.append("/oh/yeah");