java 单例模板_Spring Boot单例RestTemplate接受标头不断被附加到

在Spring Boot应用中,使用单例RestTemplate调用多个REST服务时,发现Accept Header不断被附加,导致请求错误。日志显示Accept Header列表过度增长,可能引发400 Bad Request错误。问题是由于RestTemplate作为单例bean,同一实例被多次使用,Header信息没有正确清除。寻求解决方案。
摘要由CSDN通过智能技术生成

我正在使用Spring Boot(1.3.0.RELEASE)。我需要使用RestTemplate调用许多不同的REST

Web服务。显然,我不想每次调用都创建一个RestTemplate的新实例,因此我为RestTemplate的一个实例创建了一个配置,如下所示:

@Bean

public RestTemplate restTemplate() {

return new RestTemplate();

}

上面的代码被自动装配成许多类,需要调用外部REST服务…有很多!有些要获取,有些要执行POST

USAGE ------------------------

ResponseEntity response =

getRestTemplate().exchange(getUri(), HttpMethod.GET, request, String.class);

USAGE------------------------

getRestTemplate().getMessageConverters().add(new StringHttpMessageConverter());

String response = getRestTemplate().getForObject(getUri(), String.class, toc);

------------------------

因此,我注意到“接受Http标头”存在问题。它似乎保持增长,即它被附加。我没有在代码中设置任何Accept标头!这就是我在日志中看到的。

[http-nio-9080-exec-1] o.s.web.client.RestTemplate

: Setting request Accept header to [text/plain, application/json,

application/*+json, text/plain, text/plain, text/plain, text/plain,

text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, text/plain, */*, */*, */*, */*, */*, */*, */*, */*,

*/*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*,

*/*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*,

*/*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*,

*/*, */*, */*, */*, */*, */*]

这可能会增长,最终会得到400(错误请求)

org.springframework.web.client.HttpClientErrorException: 400 Bad Request

at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]

at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641) ~[spring-web-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597) ~[spring-web-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557) ~[spring-web-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]

at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264) ~[spring-web-4.2.3.RELEASE.jar!/:4.2.3.RELEASE]

那么将RestTemplate声明为单例bean实例的问题吗?即,由于可以调用许多Web服务,因此使用了相同的RestTemplate,因此Accept

Header保持增长吗?

有人可以帮我解决这个问题吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值