Spring integration Http出站网关正在处理标头值
我们在项目中使用Spring集成。我在Spring integration Http出站网关正在处理标头值,spring-integration,Spring Integration,我们在项目中使用Spring集成。我在http:outbound gateway中遇到了一个奇怪的问题。为了执行rest服务,我们需要传递以下标头。 1) Accept=application/vnd.dsths.services-v1+xml 2) Content Type=application/xml 奇怪的是,返回的响应并不总是唯一的,在开发环境中,返回xml响应(Content-Type=application/vnd.dsths.services-v1+xml),而在客户端环境中,返
http:outbound gateway
中遇到了一个奇怪的问题。为了执行rest服务,我们需要传递以下标头。
1) Accept=application/vnd.dsths.services-v1+xml
2) Content Type=application/xml
奇怪的是,返回的响应并不总是唯一的,在开发环境中,返回xml响应(Content-Type=application/vnd.dsths.services-v1+xml
),而在客户端环境中,返回json响应(Content-Type=application/vnd.dsths.services-v1+json
)。我通过打开DEBUG
验证了日志文件,发现org.springframework.web.client.restemplate
正在将请求接受头设置为[text/plain,application/json,application/*+json,*/`*]
2017-07-10 16:17:11,563 DEBUG [org.springframework.web.client.RestTemplate] (ajp-/10.226.55.163:8009-1) Setting request Accept header to [text/plain, application/json, application/*+json, */*]
我可以通过在客户端环境中将accept=*/*
的值重写为accept=application/vnd.dsths.services-v1+xml
来克服这个问题(请注意,这个头不是实际的“accept
”头)
这里的问题是为什么
http:outbound-gateway
行为异常并操纵报头值?为什么Spring集成无法识别头和接受“
和接受”
之间的差异?我的修正正确吗?不确定区别的问题是什么,但根据RFC-2616,HTTP头不区分大小写:
和DefaultHttpHeaderMapper
遵循以下建议:
private void setHttpHeader(HttpHeaders target, String name, Object value) {
if (ACCEPT.equalsIgnoreCase(name)) {
if (value instanceof Collection<?>) {
正如您所看到的,该逻辑基于提供的HttpMessageConverter
,老实说,它是正确的。Accept
头正是客户端可以从服务器处理的内容
如果您不喜欢这样的行为,并且您确信在您的客户机中,您可以将
restemplate
注入http:outbound gateway
,但已经只使用所需的HttpMessageConverter
好了。谢谢你的解释。我认为MessageBuilder.removeHeader(“accept”)应该能够工作,以便Spring框架能够识别正确的头accept
。这有意义吗?或者你预见到了其他问题吗????你完全不理解我。我是否谈论删除一些标题?我将向您解释Accept
的值是如何存在的,以及如何克服这些值。Spring集成中的Accept
头没有任何效果,它在RestTemplate
中被覆盖。
public void doWithRequest(ClientHttpRequest request) throws IOException {
if (this.responseType != null) {
Class<?> responseClass = null;
if (this.responseType instanceof Class) {
responseClass = (Class<?>) this.responseType;
}
List<MediaType> allSupportedMediaTypes = new ArrayList<>();
for (HttpMessageConverter<?> converter : getMessageConverters()) {
if (responseClass != null) {
if (converter.canRead(responseClass, null)) {
allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter));
}
}
else if (converter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter;
if (genericConverter.canRead(this.responseType, null, null)) {
allSupportedMediaTypes.addAll(getSupportedMediaTypes(converter));
}
}
}
if (!allSupportedMediaTypes.isEmpty()) {
MediaType.sortBySpecificity(allSupportedMediaTypes);
if (logger.isDebugEnabled()) {
logger.debug("Setting request Accept header to " + allSupportedMediaTypes);
}
request.getHeaders().setAccept(allSupportedMediaTypes);
}
}
}