以下是请求样例:
RestTemplate 配置:
package cn.rojao.config;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
*
* RestTemplate配置类
*
* @author lk
* @email lk@qq.com
* @version 创建时间:2018年6月7日 上午10:26:22
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(CloseableHttpClient httpClient){
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setReadTimeout(1800000);//单位为ms,30分钟分钟
factory.setConnectTimeout(180000);//单位为ms,3分钟
factory.setConnectionRequestTimeout(900000);//获取连接时间15分钟
return factory;
}
@Bean
public HttpClientBuilder httpClientBuilder(HttpClientConnectionManager connectionManager) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(connectionManager);
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3,true));//重试次数,默认三次
List<Header> defaultHeaders = new ArrayList<Header>();
defaultHeaders.add(new BasicHeader("Content-Type", "text/plain;charset=UTF-8"));
httpClientBuilder.setDefaultHeaders(defaultHeaders);
return httpClientBuilder;
}
@Bean
public CloseableHttpClient httpClient(HttpClientBuilder httpClientBuilder) {
CloseableHttpClient httpClient = httpClientBuilder.build();
return httpClient;
}
@Bean
public HttpClientConnectionManager httpClientConnectionManager() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(800);
connectionManager.setDefaultMaxPerRoute(500);
return connectionManager;
}
}
RestTemplate 使用实例:
package cn.rojao.config;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import cn.rojao.entity.User;
import cn.rojao.util.JsonChangeUtil;
@Component
public class schedule {
@Autowired
RestTemplate restTemplate;
@Scheduled(fixedDelay = 3000)
public void restTest(){
String url = "http://10.10.1.77:9091/";
String url1 = "http://10.10.1.77:9091/{ceshi}";
String url2 = "http://10.10.1.77:9091/{test}";
User user = new User();
user.setName("ceshi");
user.setAge("12");
String userJson = JsonChangeUtil.getJson(user);
HttpHeaders headers = this.prepareHttpHeader();
HttpEntity<String> entity = new HttpEntity<>(userJson, headers);
//get方式请求
/**
* 无参数形式
*/
ResponseEntity<String> responseEntity1 = restTemplate.getForEntity(url, String.class);
String cesh1 = restTemplate.getForObject(url, String.class);
/**
* 带参数形式
*/
ResponseEntity<String> responseEntity2 = restTemplate.getForEntity(url1, String.class,"getAdv");
String cesh2 = restTemplate.getForObject(url1, String.class, "getAdv");
/**
* 带参数用map封装的模式
*/
HashMap<String, String> ceshiMap = new HashMap<String, String>();
ceshiMap.put("test", "getAdv");
ResponseEntity<String> responseEntity3 = restTemplate.getForEntity(url1, String.class,ceshiMap);
String cesh3 = restTemplate.getForObject(url1, String.class, ceshiMap);
//POST方式请求
/**
* 方法的第一参数表示要调用的服务的地址
* 方法的第二个参数表示上传的参数
* 方法的第三个参数表示返回的消息体的数据类型
*/
ResponseEntity<String> responseEntity4 = restTemplate.postForEntity(url, entity, String.class);
String cesh4 = restTemplate.postForObject(url1, entity, String.class);
/**
* 带参数的模式
*/
ResponseEntity<String> responseEntity5 = restTemplate.postForEntity(url1, entity, String.class, "getAdv");
String cesh5 = restTemplate.postForObject(url1, entity, String.class, "getAdv");
/**
* 带参数用map封装的模式
*/
HashMap<String, String> ceshiMap1 = new HashMap<>();
ceshiMap.put("test", "getAdv");
ResponseEntity<String> responseEntity6 = restTemplate.postForEntity(url2, entity, String.class, ceshiMap1);
String cesh6 = restTemplate.postForObject(url2, entity, String.class, ceshiMap1);
System.out.println(responseEntity1.getBody());
}
public HttpHeaders prepareHttpHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-type", "text/plain;charset=UTF-8");
headers.add("Accept-Charset", "UTF-8");
return headers;
}
}
pom.xml需要引入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.3</version>
</dependency>