springboot集成RestTemplate

             RestTemplate其实是spring框架对Apache HttpClient的封装。

             对比一下用法:

             先看HttpClient

 /**
	     * 带参数的post请求 .
	     * 
	     * @param url .
	     * @param map .
	     * @return .
	     * @throws Exception .
	     */
	    public HttpResult doPost(String url, Map map) throws Exception {
	    	logger.info("doPost请求url="+url);
	        // 声明httpPost请求
	        HttpPost httpPost = new HttpPost(url);
	        // 加入配置信息
	        httpPost.setConfig(config);
	 
	        // 判断map是否为空,不为空则进行遍历,封装from表单对象
	        if (map != null) {
	            List list = new ArrayList();
	            for (Map.Entry entry : map.entrySet()) {
	                list.add(new BasicNameValuePair(entry.getKey(), (String)entry.getValue()));
	            }
	            // 构造from表单对象
	            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
	 
	            // 把表单放到post里
	            httpPost.setEntity(urlEncodedFormEntity);
	        }
	 
	        // 发起请求
	        CloseableHttpResponse response = this.httpClient.execute(httpPost);
	        String respBody=EntityUtils.toString(response.getEntity(), "UTF-8");
	        return new HttpResult(response.getStatusLine().getStatusCode(), respBody);
	    }

          再看RestTemplate

	public void postForEntity(String url, Map params) {
		
		ResponseEntity responseEntity = restTemplate.postForEntity(url,params, String.class);
		System.out.println(responseEntity.getBody());
	}

        代码简洁多了。

        开始我们的实验。

        第一步:引入依赖

         

 
		
			org.apache.httpcomponents
			httpclient
			4.5.6
		

       第二步:application.properties中配置属性

#resttempate配置
#连接池最大连接数
http.maxTotal: 100
#并发数
http.defaultMaxPerRoute: 20
#创建连接的最长时间
http.connectTimeout: 10000
#从连接池中获取到连接的最长时间,单位毫秒
http.connectionRequestTimeout: 5000
#数据传输的最长时间
http.socketTimeout: 10000
#提交请求前测试连接是否可用
http.staleConnectionCheckEnabled: true
#可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间,如果超过,释放socket重新建立
http.validateAfterInactivity: 3000000

       第三步:配置bean,RestTemplateConfig.java

/**
 * 
 */
package com.figo.springboottest.config;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
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;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @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.staleConnectionCheckEnabled}")
    private boolean staleConnectionCheckEnabled;

    @Value("${http.validateAfterInactivity}")
    private Integer validateAfterInactivity;


    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(httpRequestFactory());
    }

    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {
        return new HttpComponentsClientHttpRequestFactory(httpClient());
    }

    @Bean
    public HttpClient httpClient() {
        Registry registry = RegistryBuilder.create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        connectionManager.setMaxTotal(maxTotal); // 最大连接数
        connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);    //单个路由最大连接数
        connectionManager.setValidateAfterInactivity(validateAfterInactivity); // 最大空间时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)        //服务器返回数据(response)的时间,超过抛出read timeout
                .setConnectTimeout(connectTimeout)      //连接上服务器(握手成功)的时间,超出抛出connect timeout
                .setStaleConnectionCheckEnabled(staleConnectionCheckEnabled) // 提交前检测是否可用
                .setConnectionRequestTimeout(connectionRequestTimeout)//从连接池中获取连接的超时时间,超时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
                .build();
        return HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }

}

             第四步:单元测试

/**
 * 
 */
package com.figo.springboottest;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

/**
 * @author figo
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestTemplateTest {
	@Autowired
	RestTemplate restTemplate;

	@Test
	public void testGetForEntity() {
		ResponseEntity responseEntity = restTemplate.getForEntity("https://mobile-test.figo.com/test/",
				String.class, "张三");
		System.out.println(responseEntity.getBody());
		;
	}

	@Test
	public void testGetForObject() {
		String result = restTemplate.getForObject("https://mobile-test.figo.com/test/", String.class, "张三");
		System.out.println(result);
		;
	}

	@Test
	public void testPostForEntity() {
		ResponseEntity responseEntity = restTemplate.postForEntity("https://mobile-test.figo.com/test/",
				"张三", String.class);
		System.out.println(responseEntity.getBody());
		 
		Map params=new HashMap();
		 //postForEntity("https://mobile-test.figo.com/test/",params);
	}


	public void postForEntity(String url, Map params) {
		
		ResponseEntity responseEntity = restTemplate.postForEntity(url,params, String.class);
		System.out.println(responseEntity.getBody());
	}
	
	@Test
	public void testPostForObject() {
		String result = restTemplate.postForObject("https://mobile-test.figo.com/test/", "张三", String.class);
		System.out.println(result);
	}
	
}

 参考文章:https://www.cnblogs.com/yuexiaoyun/articles/13034028.html

你可能感兴趣的:(java开发步步为营,springboot,resttemplate)