直接上代码:
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");