RestTemplate使用SimpleClientHttpRequestFactory抛出I / O身份验证错误

时间:2016-10-28 17:51:34

标签: java resttemplate

我需要使用代理通过Spring RestTemplate连接一些Rest API。我在Java6和Java7上观察到了一些有趣的行为。  下面是我的代码看起来像

Bean Defination:

<bean id="myRestTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                    <property name="objectMapper" ref="myJacksonObjectMapper" />
                    <property name="supportedMediaTypes">
                        <list>
                            <util:constant static-field="org.springframework.http.MediaType.APPLICATION_JSON"/>
                            <util:constant static-field="org.springframework.http.MediaType.TEXT_PLAIN"/>
                            <util:constant static-field="org.springframework.http.MediaType.TEXT_HTML"/>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
        <property name="requestFactory">
            <bean class="my.util.SimpleClientHttpRequestFactoryWithTimeout">
                <property name="connectTimeout" value="500">
                </property>
                <property name="readTimeout" value="500">
                </property>
            </bean>
        </property>
    </bean>

Java Call:

 SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
    InetSocketAddress address = new InetSocketAddress("myproxy", "8080");
                Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
                simpleClientHttpRequestFactory.setProxy(proxy);
                restTemplate.setRequestFactory(simpleClientHttpRequestFactory);

ResponseEntity responseEntity = restTemplate.exchange("restapiurl", requestType, requestEntity, responseClass);

代码在Java7上运行良好,但会导致I / O异常身份验证失败。我已经在两个jre的cacerts中添加了所需的证书。

-I/O error: Authentication failure; nested exception is java.io.IOException: Authentication failure

请帮我解决java 6上的问题。

0 个答案:

没有答案