【问题标题】:Spring Integration http inbound gateway mapped response headers - being duplicated?Spring Integration http 入站网关映射响应标头 - 被重复?
【发布时间】:2012-07-14 06:09:30
【问题描述】:

我正在尝试为图像托管服务设置响应的内容类型,所以我有一个如下所示的 http:inbound-gateway:

<int-http:inbound-gateway request-channel="receiveChannel"
                          reply-channel="responseChannel"
                          path="/profile/photo"
                          mapped-response-headers="Content-Type"
                          message-mappers="messageConverterList"
                          supported-methods="GET"/>

...和一个看起来像这样的服务激活器:

<int:service-activator 
    input-channel="receiveChannel" 
    output-channel="imageResponseChannel"
    expression="@profileService.getPhoto(payload.userId)"/>

... 返回 jpeg 图像数据的 byte[]。我有一个看起来像这样的标题丰富器:

<int:header-enricher
    input-channel="imageResponseChannel"
    output-channel="responseChannel">
    <int:header
        name="Content-Type"
        expression="'image/jpeg'"/>
</int:header-enricher>

但是当我运行项目时,我会收到这样的标题响应:

< HTTP/1.1 200 OK
< Set-Cookie: JSESSIONID=9uw5c136fia6s9ivxgivy1yc;Path=/
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Content-Type: image/jpeg
< Content-Type: application/octet-stream
< Content-Length: 6563
< Server: Jetty(8.1.3.v20120416)

注意 content-type 是重复的,但据我了解,mapped-response-headers 应该从消息中获取标头,而不是从有效负载中确定它。

有什么想法吗?提前致谢!

*编辑:我更改了入站网关以引用下面的自定义消息映射器,但我仍然得到相同的结果。

<util:list id="messageConverterList">
    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list><value>image/jpeg</value></list>
        </property>
    </bean>
</util:list>

*EDIT 2:原来它应该是消息转换器,但我没有收到错误,因为我修改了目标文件而不是 src。我进行了更正,这是我得到的输出:

< HTTP/1.1 200 OK
< Content-Type: image/jpeg
< Content-Type: image/jpeg
< Content-Length: 209582
< Server: Jetty(6.1.10)

更好,但并不完美。这是一个简单的问题重现项目:http://dl.dropbox.com/u/92800052/http.tar.gz

你可以运行它

mvn package jetty:run 

并使用

查看输出
curl -v http://localhost:8080/http/photo > /dev/null

【问题讨论】:

    标签: content-type spring-integration


    【解决方案1】:

    默认情况下,为入站网关注册了几个 HttpMessageConverter(来自核心 Spring Web 库)。其中之一是 ByteArrayHttpMessageConverter,它的默认内容类型是“application/octet-stream”。您能否尝试在网关上设置“消息转换器”属性,使其引用包含单个 ByteArrayHttpMessageConverter 实例的列表类型 bean(例如,您可以使用 util:list) - 而不是依赖默认转换器,然后在该实例上,将supportedMediaTypes 属性设置为“image/jpeg”。

    AFAIK,ByteArrayHttpMessageConverter 不应尝试设置 Content-Type 标头,如果已经存在(即来自映射的响应标头),但我只想检查并查看该更改是否确实绕过了重复。

    另外,能否请您告诉我们您使用的是哪个版本的 Spring Integration?

    谢谢, 标记

    【讨论】:

    • 当然。我正在使用 2.1.3.RELEASE。我添加的代码和结果一起在 EDIT 中(因为我无法在评论中添加代码)。
    • 你能再尝试一件事吗......在标题丰富器中,你能用小写而不是大写来表示“内容类型”吗?
    • 我只是尝试了标头丰富器的小写内容类型,然后在网关中每次都具有相同的结果(重复的 Content-Type HTTP 标头)。我可以创建项目的净化版本并将其发布在 github 上。你认为这会有所帮助吗?
    • 是的,请这样做。这应该有助于我们调试并找出重复标头的添加位置。
    • 我没有忘记这件事。原来我修改了目标文件而不是 src,所以一旦我重新打包,我制作的 mod 就不算数。 content-type 仍然重复,但它是相同的值。问题已更新。我将使用已清理项目的 Dropbox 链接发表评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多