需要導入HttpClient依賴
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
用以下代碼替換原來RestTemplate請求的代碼(以發(fā)送客服消息為例):
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
HttpPost httpPost =new HttpPost(url);
StringEntity entity =new StringEntity(json, Consts.UTF_8);
httpPost.setEntity(entity);
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient httpClient = builder.build();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
String responseJson = EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8);
大功告成!