spring框架结合RestTemplate实现远程服务调用

一、jar包依赖


    org.apache.httpcomponents
    httpclient
pom.xml

二、配置文件


class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"
    destroy-method="shutdown">
    
    
    
    





class="org.apache.http.impl.client.HttpClientBuilder">
    
    




class="org.apache.http.impl.client.CloseableHttpClient"
    factory-bean="httpClientBuilder" factory-method="build" scope="prototype" />


class="org.apache.http.client.config.RequestConfig.Builder">
    
    
    
    
    
    
    
    

class="org.apache.http.client.config.RequestConfig"
    factory-bean="requestConfigBuilder" factory-method="build" />

class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
    
    
    





class="org.springframework.web.client.RestTemplate">
    
    
        class="org.springframework.web.client.DefaultResponseErrorHandler" />
    
    
        
            
            
            
            <bean
                class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.StringHttpMessageConverter">
                
                    
                        text/plain;charset=UTF-8
                        application/json;charset=UTF-8
                    
                
            
        
    
spring-demo.xml
#######################The config for HttpClient###################
#最大连接数
http.maxTotal=300
#并发数
http.defaultMaxPerRoute=50
#创建连接的最长时间
http.connectTimeout=1000
#从连接池中获取到连接的最长时间
http.connectionRequestTimeout=500
#数据传输的最长时间
http.socketTimeout=20000
#提交请求前测试连接是否可用
http.staleConnectionCheckEnabled=true
#######################The config for HttpClient###################
spring-demo.properties

三、工具类

import java.util.Collections;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class RestTemplateUtils {
    
    
    private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateUtils.class);
    
    @Autowired
    private RestTemplate restTemplate;

    /**
     * post请求.
     *
     * @param url
     * @param formParams
     * @return TODO:请对所有【参数】还有【返回结果】进行说明
     */
    public  String doPostForEntity(String url, Object formParams){
        //设置head  
        HttpHeaders requestHeaders = new HttpHeaders();   
        requestHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity request=new HttpEntity(formParams, requestHeaders);
        ResponseEntity response = restTemplate.postForEntity(url,
                request, String.class);
        if(StringUtils.isNotEmpty(response.getStatusCode().toString())){
            LOGGER.info("restemplate post return status code :"+response.getStatusCode().toString());
        }
        String jsonObject = null;
        if (response != null) {
            jsonObject = response.getBody();
        }
        return jsonObject;
    }
}
RestTemplateUtils.java

 

剩下的就是业务代码层调用工具类来实现具体的业务了。

 

你可能感兴趣的:(spring框架结合RestTemplate实现远程服务调用)