Spring RestTemplate响应时间慢

时间:2013-06-24 13:48:48

标签: java spring web-services rest

在Spring 3.2上尝试使用RestTemplate POST对象时,平均响应时间为8秒

使用curl

time curl -X POST -H "Content-Type: application/xml" -T request.xml https://x.y.com:20000/rest

平均时间约为4秒。我不明白为什么。

我的配置:

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>x.y.z.Request</value>
                <value>x.y.z.Response</value>
                <value>x.y.z.AnotherRequest</value>
                <value>x.y.z.AnotherResponse</value>
            </list>
        </property>
    </bean>


<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"
        scope="prototype">
        <constructor-arg>
            <bean
                class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
                <property name="readTimeout" value="${application.urlReadTimeout}" />
                <property name="connectTimeout" value="${application.urlConnectionTimeout}" />
            </bean>
        </constructor-arg>

        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                    <property name="marshaller" ref="jaxb2Marshaller" />
                    <property name="unmarshaller" ref="jaxb2Marshaller" />
                </bean>
                <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter" />
            </list>
        </property>
    </bean>

然后我简单地自动装配它:

    @Autowired
    RestTemplate restTemplate;

public Response getXml(Request request){

    Response response = restTemplate.postForObject(httpUrl,request, Response.class);
}

P.S:作为替代方案,我尝试使用JaxB解析Request / Response对象,并使用org.apache.http.client.HttpClient发送它,平均时间约为7秒,这远远不够好。

1 个答案:

答案 0 :(得分:1)

可能是ipv4 / 6问题。试试

curl -4 -X POST -H "Content-Type: application/xml" -T request.xml https://x.y.com:20000/rest

如果它适合您,请使用param运行您的java cmd

-Djava.net.preferIPv4Stack=true