Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RestTemplate获取重定向url返回;嵌套的异常是org.apache.http.client.ClientProtocolException“;轮流_Java_Spring Boot_Resttemplate_Http Get_Http Redirect - Fatal编程技术网

Java RestTemplate获取重定向url返回;嵌套的异常是org.apache.http.client.ClientProtocolException“;轮流

Java RestTemplate获取重定向url返回;嵌套的异常是org.apache.http.client.ClientProtocolException“;轮流,java,spring-boot,resttemplate,http-get,http-redirect,Java,Spring Boot,Resttemplate,Http Get,Http Redirect,如果有人知道调试这个的方法,这也是很好的。我会做的。但目前我在调试这个方面没有任何进展 我有一个url,比如url-a,它总是返回302。但是,当我在spring引导项目中使用当前RestTemplate配置(粘贴在当前配置下)以10的循环运行URL-A时,或者我会得到以下异常。也就是说,我也可以说,我得到了成功响应的get请求 org.springframework.web.client.ResourceAccessException: I/O error on GET request for

如果有人知道调试这个的方法,这也是很好的。我会做的。但目前我在调试这个方面没有任何进展

我有一个url,比如url-a,它总是返回302。但是,当我在spring引导项目中使用当前RestTemplate配置(粘贴在当前配置下)以10的循环运行URL-A时,或者我会得到以下异常。也就是说,我也可以说,我得到了成功响应的get请求

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "URL-A": null; nested exception is org.apache.http.client.ClientProtocolException
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:673)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:319)
....
...
...
Caused by: org.apache.http.client.ClientProtocolException: null
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:89)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:659)
... 96 common frames omitted                                   
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:151)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 102 common frames omitted
使用的当前RestTemplate配置:

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();

    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

    return objectMapper;
}

@Bean
public RestTemplate getRestTemplate() {
    RestTemplate newTemplate = new RestTemplate();
    newTemplate.setRequestFactory(this.getHttpComponentsClientHttpRequestFactory());
    ObjectMapper objectMapper = this.objectMapper();
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(jackson2HttpMessageConverter);
    messageConverters.add(new ByteArrayHttpMessageConverter());
    newTemplate.setMessageConverters(messageConverters);
    return newTemplate;
}

private HttpComponentsClientHttpRequestFactory getHttpComponentsClientHttpRequestFactory() {
    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
    poolingHttpClientConnectionManager.setMaxTotal(maxTotal);
    poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPerRoute);
    HttpClient client = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
    factory.setConnectTimeout(connectTimeout);
    factory.setReadTimeout(readTimeout);
    return factory;
}
@Bean
公共对象映射器对象映射器(){
ObjectMapper ObjectMapper=新的ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
configure(空bean上的SerializationFeature.FAIL\u,false);
objectMapper.configure(在未知属性上反序列化feature.FAIL,false);
configure(已忽略属性上的反序列化功能.FAIL\u,false);
objectMapper.configure(反序列化功能。子类型无效时失败,错误);
enable(反序列化功能。接受\u空\u字符串\u作为\u空\u对象);
返回对象映射器;
}
@豆子
公共RestTemplate getRestTemplate(){
RestTemplate newTemplate=新RestTemplate();
newTemplate.setRequestFactory(this.getHttpComponentsClientHttpRequestFactory());
ObjectMapper ObjectMapper=this.ObjectMapper();
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter=新MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.setObjectMapper(objectMapper);

List我认为RestTemplate有问题,为什么不试试默认的RestTemplate呢?默认情况下,删除连接工厂?但我需要连接池。如果我从rest模板中删除工厂,它会工作。默认情况下,RestTemplate也会有连接池,我不确定你为什么要自定义@2请更正我的错误。此页面说明resttemplate默认情况下不使用连接池。您对删除
newTemplate.setRequestFactory(This.getHttpComponentsClientHttpRequestFactory())的评论
救了我,非常感谢雷尼尔!我相信RestTemplate有问题,为什么不试试默认的RestTemplate
?默认情况下,删除连接工厂?但我需要连接池。如果我从rest模板中删除工厂,它就会工作。默认情况下,RestTemplate也会有连接池,我不知道您为什么要自定义它@2如果我错了,请纠正我。此页面说resttemplate默认情况下不使用连接池。您关于删除
newTemplate.setRequestFactory(This.getHttpComponentsClientHttpRequestFactory());
救了我,非常感谢Renil!