Directly on the code:
1, the class definition configuration
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, the definition of service
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(); HttpHeaders new new HttpHeaders H = (); Private HttpHeaders urlHeaderBuilder () { // build Url request header } h.setContentType (MediaType.APPLICATION_FORM_URLENCODED); return H; } // build Json request header Private HttpHeaders jsonHeaderBuilder () { HttpHeaders new new HttpHeaders H = (); h.setContentType (MediaType.APPLICATION_JSON); return H ; } }
3, Example (Reference)
POST request, the URL parameter with the
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);
JPOST GET request and are commonly used, parameters are added and UPost Similarly, what type of change.
Map<String, Object> requestPara = new HashMap<>(); requestPara.put("id", id); requestPara.put("name", name); StringBuilder sb = new StringBuilder(baseUrl); sb.append("/oh/yeah");