无法使用 int-http:outbound-gateway 映射 content-type header

Cannot map content-type header using int-http:outbound-gateway

我有一个 http:inbound-gateway,接收 json 消息,进行一些充实并使用 http:outbound-gateway 将其发送到端点以调用休息服务json 负载。

Inbound-gw 收到 content-type header,它在消息 header 上设置,但是当使用 [=54= 将有效负载发送到其余服务时] 出现以下错误:415 Unsupported Media Type

我检查了日志,出现以下警告:

WARN DefaultHttpHeaderMapper:951 - Header 'content-type' 值 'application/json' 将不会被设置,因为它不是字符串并且没有可用的转换器。考虑使用 ConversionService 注册转换器(例如,)

这很奇怪,因为 content-type header 设置了值 application/json 而我使用 mapped-request-headers="HTTP_REQUEST_HEADERS"。我正在使用 SI 4.3。1.RELEASE,知道吗?

这是 http:inbound-gw

<int-http:inbound-gateway id="restHttpInboundGateway"
        request-channel="restHttpInboundChannel" path="/services"
        supported-methods="GET,POST,PUT,DELETE,PATCH,HEAD"
        reply-channel="restHttpOutboundChannel" 
        mapped-request-headers="http_requestMethod,Content-Length,Accept,Connection, Content-Type">
        <int-http:request-mapping consumes="application/json,application/xml"
            produces="application/json,application/xml" />
    </int-http:inbound-gateway>

这是 outbound-gw

<int-http:outbound-gateway id="restHttpOutboundGateway"
        request-channel="restHttpOutboundGatewayChannel" reply-channel="restHttpOutboundChannel"
        url-expression="https://localhost:8443/service/rest/contacts/1" mapped-request-headers="HTTP_REQUEST_HEADERS"
        http-method-expression="PUT" expected-response-type="java.lang.String"
        charset="UTF-8"/>

这是 outbound-gw 之前记录的消息:

2016-10-08 10:07:04,634 INFO serviceMessages:194 - GenericMessage [payload=byte[76], headers={content-length=76, replyChannel =org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@44237f19,errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@44237f19,content-type=application/json,connection=keep-alive,id=79012bea-263b-0f48 -6e96-5fc832c08da6,接受=[text/plain,/],时间戳=1475932024630}]

这看起来像是一个错误; header 映射器应该始终在 Spring 集成消息中映射 Content-Type to/from contentType。出站适配器需要 contentType.

但是,在入站端执行此操作的代码正在寻找 Content-Type (case-sensitive) 并从 Spring MVC 获取 content-type。也许有些事情已经改变了。

看来我们需要做这个映射测试case-insensitive。

同时,您可以添加一个 header enricher 来复制 header...

<header-enricher ...>
    <header name="contentType" expression="headers['content-type']" />
</header-enricher>

和一个header-filter删除content-typeheader(消除警告日志)。

DefaultHttpHeaderMapper 中还有另一个错误,当它没有随 ConversionService 一起提供时,因此 MimeType 对于您的 content-type header 可以' 转换为字符串。

您可以为 <int-http:outbound-gateway> 创建 DefaultHttpHeaderMapper.outboundMapper() bean 而不是 mapped-request-headers。或者,再次:re-map content-type 自身:

<header name="content-type" expression="headers['content-type'].toString()" override="true"/>