RestTemplate 的配置方法与基本使用方法

xml 文件中的配置如下(基于 HttpClient):


    
    

    <bean id="pollingConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
        <property name="maxTotal" value="${httpClientPool.maxTotal}"/>
        <property name="defaultMaxPerRoute" value="${httpClientPool.defaultMaxPerRoute}"/>
    bean>

    <bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create">
        <property name="connectionManager" ref="pollingConnectionManager"/>
        <property name="retryHandler">
            <bean class="org.apache.http.impl.client.DefaultHttpRequestRetryHandler">
                <constructor-arg value="2"/>
                <constructor-arg value="true"/>
            bean>
        property>
        <property name="defaultHeaders">
            <list>
                <bean class="org.apache.http.message.BasicHeader">
                    <constructor-arg value="User-Agent"/>
                    <constructor-arg
                            value="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"/>
                bean>
                <bean class="org.apache.http.message.BasicHeader">
                    <constructor-arg value="Accept-Encoding"/>
                    <constructor-arg value="gzip,deflate"/>
                bean>
                <bean class="org.apache.http.message.BasicHeader">
                    <constructor-arg value="Accept-Language"/>
                    <constructor-arg value="zh-CN"/>
                bean>
            list>
        property>
    bean>

    <bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build"/>

    <bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
        <constructor-arg ref="httpClient"/>
        <property name="connectTimeout" value="${httpClient.connectTimeout}"/>
        <property name="readTimeout" value="${httpClient.readTimeout}"/>
    bean>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <constructor-arg ref="clientHttpRequestFactory"/>
        <property name="errorHandler">
            <bean class="org.springframework.web.client.DefaultResponseErrorHandler"/>
        property>
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"/>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8value>
                        list>
                    property>
                bean>
            list>
        property>
    bean>
    

更多关于使用方法及配置的内容可参考:

1、RestTemplate实践
http://liuxing.info/2015/05/21/RestTemplate%E5%AE%9E%E8%B7%B5/

2、Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
http://www.cnblogs.com/tomcatandjerry/p/5899722.html

3、Consuming a RESTful Web Service
https://spring.io/guides/gs/consuming-rest/


你可能感兴趣的:(JavaWeb基础)