Spring Boot入門教程(五十): httpclient
阿新 • • 發佈:2018-11-08
在開發中經常會遇到呼叫第三方的介面,這裡介紹一下在spring boot中如何使用httpclient來發送get和post請求。
- 引入依賴
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
- application.properties
#最大連線數
http.maxTotal = 100
#併發數
http.defaultMaxPerRoute = 20
#建立連線的最長時間
http.connectTimeout=1000
#從連線池中獲取到連線的最長時間
http.connectionRequestTimeout=500
#資料傳輸的最長時間
http.socketTimeout=10000
http.validateAfterInactivity=1000
- Configuration
import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HttpClientConfiguration { @Value("${http.maxTotal}") private Integer maxTotal; @Value("${http.defaultMaxPerRoute}") private Integer defaultMaxPerRoute; @Value("${http.connectTimeout}") private Integer connectTimeout; @Value("${http.connectionRequestTimeout}") private Integer connectionRequestTimeout; @Value("${http.socketTimeout}") private Integer socketTimeout; @Value("${http.validateAfterInactivity}") private Integer validateAfterInactivity; @Bean public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager(){ PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(maxTotal); connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); connectionManager.setValidateAfterInactivity(validateAfterInactivity); return connectionManager; } @Bean public HttpClientBuilder httpClientBuilder(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager){ HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager); return httpClientBuilder; } @Bean public CloseableHttpClient closeableHttpClient(HttpClientBuilder httpClientBuilder){ return httpClientBuilder.build(); } @Bean public RequestConfig.Builder builder(){ RequestConfig.Builder builder = RequestConfig.custom(); return builder.setConnectTimeout(connectTimeout) .setConnectionRequestTimeout(connectionRequestTimeout) .setSocketTimeout(socketTimeout); } @Bean public RequestConfig requestConfig(RequestConfig.Builder builder){ return builder.build(); } }
- HttpClient
import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; @Component public class HttpClient { /** 預設字符集 */ public static final String DEFAULT_CHARSET = "UTF-8"; @Autowired private CloseableHttpClient closeableHttpClient; @Autowired private RequestConfig config; /** * 用於JSON格式的API呼叫 * @param url url地址 * @param requestParameter 請求引數 * @param clazz 介面返回值的型別 * @return * @throws Exception */ public <T> T doGet(String url, Map<String, Object> requestParameter, Class<T> clazz) throws Exception { String responseJson = this.doGet(url, requestParameter); T response = JSONObject.parseObject(responseJson, clazz); return response; } public String doGet(String url, Map<String, Object> requestParameter) throws Exception { URIBuilder uriBuilder = new URIBuilder(url); if (requestParameter != null) { for (Map.Entry<String, Object> entry : requestParameter.entrySet()) { uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); } } return this.doGet(uriBuilder.build().toString()); } public String doGet(String url) throws Exception { HttpGet httpGet = new HttpGet(url); httpGet.setConfig(config); httpGet.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); CloseableHttpResponse response = this.closeableHttpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new Exception("api request exception, http reponse code:" + statusCode); } return EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET); } public <T> T doPost(String url, Map<String, Object> requestParameter, Class<T> clazz) throws Exception { HttpResponse httpResponse = this.doPost(url, requestParameter); int statusCode = httpResponse.getCode(); if (statusCode != HttpStatus.SC_OK) { throw new Exception("api request exception, http reponse code:" + statusCode); } T response = JSONObject.parseObject(httpResponse.getBody(), clazz); return response; } public HttpResponse doPost(String url, Map<String, Object> requestParameter) throws Exception { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(config); httpPost.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); if (requestParameter != null) { String requestBody = JSONObject.toJSONString(requestParameter); StringEntity postEntity = new StringEntity(requestBody, "UTF-8"); httpPost.setEntity(postEntity); } CloseableHttpResponse response = this.closeableHttpClient.execute(httpPost); // 對請求的響應進行簡單的包裝成自定義的型別 return new HttpResponse(response.getStatusLine().getStatusCode(), EntityUtils.toString( response.getEntity(), DEFAULT_CHARSET)); } public HttpResponse doPost(String url) throws Exception { return this.doPost(url, null); } /** * 封住請求的響應碼和響應的內容 */ public class HttpResponse { /** http status */ private Integer code; /** http response content */ private String body; public HttpResponse() { } public HttpResponse(Integer code, String body) { this.code = code; this.body = body; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } } }
- User
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@RequiredArgsConstructor
public class User {
private Long id;
private String username;
}
- controller
@RestController
public class TestController {
@Autowired
private HttpClient httpClient;
@GetMapping("testGet")
public String testGet() throws Exception {
return httpClient.doGet("http://www.baidu.com");
}
@PostMapping("tesPost")
public User tesPost() throws Exception {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", "1");
paramMap.put("username", "mengda");
User user = httpClient.doPost("http://localhost:8080/doPost", paramMap, User.class);
return user;
}
@PostMapping("doPost")
public User doPost(@RequestBody User user) {
User newUser = new User(user.getId(), user.getUsername() + "y");
return newUser;
}
}