Spring REST 模板接受标头

Nik*_*eja 6 java rest spring spring-mvc resttemplate

在对我们的一项 REST 服务进行一些负载测试期间,当负载增加时,我们开始看到 Spring 的 REST 模板的这些类型的日志:

在并发负载下,3-4小时后,http请求的Accept头变成

DEBUG: org.springframework.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, text/plain, text/plain,<and so on>, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, */*, <and so on>]
Run Code Online (Sandbox Code Playgroud)

最终,所有使用 RestTemplate 对此服务的调用都以 400 错误(错误请求)开始失败

被调用的 REST 服务接受一个字符串作为输入并具有以下签名

@RequestMapping(value = "/findRecordById", method = {RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public String findRecordById(@RequestBody String id) {//method body}
Run Code Online (Sandbox Code Playgroud)

我们正在向此服务发送 POST 类型的请求,请求内容为“someId”形式,例如“123”

在轻负载下,调用服务没有问题。

令人费解的是text/plain, */*不断添加到 REST 模板的接受标头列表中。为什么会发生这种情况?

REST 模板 bean 声明是这样的:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <constructor-arg>
            <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
                <property name="readTimeout">
                    <value>90000</value>
                 </property>
                <property name="httpClient" ref="restHttpClient" />
            </bean>
        </constructor-arg>
    </bean>

    <bean id="restHttpClient"  class="org.apache.http.impl.client.DefaultHttpClient">
          <constructor-arg> 
            <bean class="org.apache.http.impl.conn.PoolingClientConnectionManager">
                <property name="defaultMaxPerRoute">
                    <value>100000</value>
                 </property>
                <property name="maxTotal">
                    <value>100000</value>
                 </property>                 

            </bean>
          </constructor-arg>
    </bean>
Run Code Online (Sandbox Code Playgroud)

请求是如何创建的:

String postParams = "\"" + id + "\"";

String postResp = restTemplate.postForObject("findRecordById",postParams, String.class);
Run Code Online (Sandbox Code Playgroud)

gur*_*wal 0

你能尝试一下这个吗:

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

String postParams = "\"" + id + "\"";

String postResp = restTemplate.postForObject("findRecordById",postParams, String.class);
Run Code Online (Sandbox Code Playgroud)