用于Http Outbound Gateway的RestTemplate正在设置它自己的头值而不是提供一个

时间:2018-01-24 12:22:24

标签: spring-integration

我们正在项目中使用Spring Integration 4,而我正在使用http:outbound-gateway来访问REST服务。运行代码时出现以下错误:

resulted in 400 (null)

奇怪的是,我观察到的是org.springframework.web.client.RestTemplate正在设置请求接受标头[text/plain, application/json, application/*+json, */*]而不是标头Accept的标头值。为什么会发生这种情况以及如何防止这种情况发生?

我的出站网关:

<int-http:outbound-gateway  url-expression="headers.restResourceUrl"
                                    http-method-expression="headers.httpMethod"
                                    expected-response-type="java.lang.String" >
</int-http:outbound-gateway>

以下是日志文件:

2018-01-24 17:42:10,089 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[Accept], value=application/vnd.dsths.services-v8+xml
2018-01-24 17:42:10,089 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[sequencesize] WILL NOT be mapped
2018-01-24 17:42:10,089 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[originalpayload] WILL NOT be mapped
2018-01-24 17:42:10,090 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[httpmethod] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[authorization] WILL be mapped, matched pattern=authorization
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[Authorization], value=Basic cmF2aV9rOnJhdmlAMTIzNA==
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[replychannel] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[businessarea] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[legacysystem] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[amisys-api-version] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[correlationid] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[id] WILL NOT be mapped
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[content-type] WILL be mapped, matched pattern=content-type
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[Content-Type], value=application/vnd.dsths.services-v8+xml
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[user-agent] WILL be mapped, matched pattern=user-agent
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] setting headerName=[user-agent], value=Java/1.6
2018-01-24 17:42:10,091 DEBUG main [org.springframework.integration.http.support.DefaultHttpHeaderMapper] headerName=[timestamp] WILL NOT be mapped
2018-01-24 17:42:10,128 DEBUG main [org.springframework.web.client.RestTemplate] Created POST request for "http://10.166.120.97:9000/exetersoa/secured/rest/brokers/search/"
2018-01-24 17:42:10,133 DEBUG main [org.springframework.web.client.RestTemplate] Setting request Accept header to [text/plain, application/json, application/*+json, */*]
2018-01-24 17:42:10,135 DEBUG main [org.springframework.web.client.RestTemplate] Writing [] as "application/vnd.dsths.services-v8+xml" using [org.springframework.http.converter.StringHttpMessageConverter@19cc697c]
2018-01-24 17:42:10,840 DEBUG main [org.springframework.web.client.RestTemplate] POST request for "http://10.166.120.97:9000/exetersoa/secured/rest/brokers/search/" resulted in 400 (null); invoking error handler
2018-01-24 17:42:10,846 DEBUG main [org.springframework.integration.router.HeaderValueRouter] org.springframework.messaging.MessageHandlingException: HTTP request execution failed for URI [http://10.166.120.97:9000/exetersoa/secured/rest/brokers/search/]; nested exception is org.springframework.web.client.HttpClientErrorException: 400 null

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

AcceptHeaderRequestCallback.doWithRequest()

中设置了if (this.responseType != null) { ... for (HttpMessageConverter<?> converter : getMessageConverters()) { ... } if (!allSupportedMediaTypes.isEmpty()) { MediaType.sortBySpecificity(allSupportedMediaTypes); if (logger.isDebugEnabled()) { logger.debug("Setting request Accept header to " + allSupportedMediaTypes); } request.getHeaders().setAccept(allSupportedMediaTypes); } } HTTP标头
HttpMessageConverter

因此,它会迭代已配置的allSupportedMediaTypes并独立于您之前提供的值收集RestTemplate。这是正确的行为。您希望某个对象作为响应,Accept必须能够转换为它。为此,它需要知道允许服务器构建响应的HttpMessageConverter类型,因此它将能够找到适当的application/vnd.dsths.services-v8+xml来构建回复对象。

如果您真的只使用HttpRequestExecutingMessageHandler,则应通过以下方式重新配置/** * Set a list of {@link HttpMessageConverter}s to be used by the underlying {@link RestTemplate}. * Converters configured via this method will override the default converters. * @param messageConverters The message converters. * @see RestTemplate#setMessageConverters(java.util.List) */ public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) {

Accept

仅针对特定MarshallingHttpMessageConverter值的转换器。我猜这可能是Taxi*