使用resttemplate从代码中读取请求超时。但同样的请求在邮递员中通过

问题描述 投票:0回答:1

这是一个很大的要求。但它在邮递员中有效,并在一分钟后返回。 其余模板超时已设置为 5 分钟。读取和连接超时。不知何故,这适用于较小的请求。我还进行了调试,我可以看到正在应用超时设置。

但是订单通过并到达我们的目标系统,并且订单被放置在那里。但我们从未收到回复,因此我们的系统显示订单失败,因为没有收到回复。通过邮递员也可以进行同样的操作。我检查了标题,一切都一样

final String responseStr = restTemplate.postForObject(url, httpEntity, String.class);
<alias name="defaultSimpleClientHttpRequestFactory" alias="simpleClientHttpRequestFactory"/>
<bean id="defaultSimpleClientHttpRequestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory" scope="prototype">
    <property name="connectTimeout" value="${connection.timeout}"/>
    <property name="readTimeout" value="${read.timeout}"/>
</bean>

根据最佳实践,我们必须使用休息模板创建器。 但是这个问题是由此造成的吗?这是所有现有代码。值得重构吗?因为我们已经尝试了一些选项(bufferRequestBody to false),这导致了其他奇怪的问题。它在本地有效,但在更高的环境中也无效

或者是由于没有使用自定义消息转换器造成的?我们应该朝这个方向发展吗?由于我们使用的是 json 字符串请求,因此类似于下面的内容

<property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg value="application"/>
                            <constructor-arg value="json"/>
                        </bean>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg value="application"/>
                            <constructor-arg value="octet-stream"/>
                        </bean>
                    </list>
                </property>
            </bean>
        </list>
    </property>

或者这应该是由请求拦截器引起的?

或者通过错误处理程序?我调试了这个流程,看看它到底在哪里读取超时。我可以看到执行方法运行良好,但稍后在错误处理程序中。

org.springframework.web.client.DefaultResponseErrorHandler

@Override
      public boolean hasError(ClientHttpResponse response) throws IOException {
            int rawStatusCode = response.getRawStatusCode();
            HttpStatus statusCode = HttpStatus.resolve(rawStatusCode);
            return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode));
      }

response.getRawStatusCode()

超时

有人在使用休息模板时遇到过类似的情况吗?请分享或建议。预先感谢

java rest web-services resttemplate
1个回答
0
投票

就我而言,这是因为 Windows 更新。我需要更新 Windows,重新启动电脑,然后它就可以工作了。但我不知道根本原因。

© www.soinside.com 2019 - 2024. All rights reserved.