【问题标题】:spring integration inbound gateway http request headers春季集成入站网关http请求标头
【发布时间】:2014-07-31 16:22:58
【问题描述】:

我有文件存储在 MongoDB 中,可以通过 Rest 服务上传/下载/删除。我想通过 Spring Integration(+Boot+Embedded Jetty)从 MongoDB 下载文件,但我不知道如何正确设置 Content-Disposition 标头,因为下载的文件具有正确的有效负载但文件名错误(它是没有任何扩展的receiveGateway):

xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-http="http://www.springframework.org/schema/integration/http"
       xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

<bean id="byteArrayHttpMessageConverter"
      class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
</bean>

<int:channel id="http.frontend.rx"/>
<int:channel id="http.frontend.tx"/>
<int:channel id="http.backend.mysql.api.rx"/>
<int:channel id="http.backend.mysql.api.tx"/>
<int:channel id="http.backend.mongodb.api.rx"/>
<int:channel id="http.backend.mongodb.api.tx"/>
<!-- -->
<int-http:inbound-gateway
    id="frontEndToFlow"
    request-channel="http.frontend.rx"
    reply-channel="http.frontend.tx"
    path="/receiveGateway"
    mapped-request-headers="*"
    mapped-response-headers="Content-Disposition,Content-Length,Content-Type,Server,Application-Context,Content-Transfer-Encoding"
    message-converters="byteArrayHttpMessageConverter"
    supported-methods="GET,POST,PUT,DELETE"/>
<int-http:outbound-gateway
    id="FlowToBackEnd"
    request-channel="http.backend.mysql.api.tx"
    reply-channel="http.backend.mysql.api.rx"
    url="http://localhost:7070/api/{path}"
    http-method-expression="headers.http_requestMethod"
    expected-response-type="java.lang.String"
    charset="UTF-8">
    <int-http:uri-variable name="path" expression="'users/user@gmail.com/'"/>
</int-http:outbound-gateway>
<int-http:outbound-gateway
    id="springFlowToHttpBackEndMongoDB"
    request-channel="http.backend.mongodb.api.tx"
    reply-channel="http.backend.mongodb.api.rx"
    url="http://localhost:5050/api/{path}"
    http-method-expression="headers.http_requestMethod"
    message-converters="byteArrayHttpMessageConverter"
    mapped-request-headers="*"
    mapped-response-headers="Content-Disposition,Content-Length,Content-Type,Server,X-Application-Context,Content-Transfer-Encoding"
    expected-response-type="byte[]"
    charset="UTF-8">
    <int-http:uri-variable name="path" expression="'download/53cffb7c6c29a4f718627905'"/>
</int-http:outbound-gateway>
<int:service-activator
    id="inboundServiceActivator"
    input-channel="http.frontend.rx"
    output-channel="http.backend.mongodb.api.tx"
    ref="messageReaderService"
    method="printContent">
</int:service-activator>
<int:service-activator
    id="outboundServiceActivator"
    input-channel="http.backend.mongodb.api.rx"
    output-channel="http.frontend.tx"
    ref="messageReaderService"
    method="printContent">
</int:service-activator>

http 请求/响应标头

Remote Address:::1:8080
Request URL:http://localhost:8080/receiveGateway
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ro;q=0.6,nb;q=0.4
Connection:keep-alive
Cookie:m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D
DNT:1
Host:localhost:8080
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Response Headersview source
Content-Length:33331
Content-Type:application/octet-stream
Server:Jetty(8.1.15.v20140411)
X-Content-Disposition:attachment; filename="aqmAR47_460s.jpg"

Application.java

@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource("classpath:httpgateway.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

MessageReaderService.java - 用它来查看标题......这是正确的......

@Component
public class MessageReaderService {

    public Message printContent(Message message){
        System.out.println(message.getHeaders().toString());
        return message;
    }

}

System.out.println 控制台输出

    {errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2f53f6f3, http_requestMethod=GET, Cookie=m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2f53f6f3, Host=localhost:8080, User-Agent=Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36, http_requestUrl=http://localhost:8080/receiveGateway, Connection=keep-alive, Accept-Language=en-US,en;q=0.8,ro;q=0.6,nb;q=0.4, Accept-Encoding=gzip,deflate,sdch, DNT=1, Accept=[text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8], id=a5c4630e-f064-7245-8c8d-9e6706c361af, timestamp=1406907105217}
2014-08-01 18:31:45.218  WARN 8364 --- [qtp699466412-13] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-errorChannel' with value 'org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2f53f6f3' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
2014-08-01 18:31:45.218  WARN 8364 --- [qtp699466412-13] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-replyChannel' with value 'org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2f53f6f3' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
2014-08-01 18:31:45.218  WARN 8364 --- [qtp699466412-13] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-id' with value 'a5c4630e-f064-7245-8c8d-9e6706c361af' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
2014-08-01 18:31:45.219  WARN 8364 --- [qtp699466412-13] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-timestamp' with value '1406907105217' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
{errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2f53f6f3, http_requestMethod=GET, Cookie=m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@2f53f6f3, Host=localhost:8080, Content-Length=33331, User-Agent=Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36, http_requestUrl=http://localhost:8080/receiveGateway, Connection=keep-alive, Server=Jetty(8.1.15.v20140411), Accept-Language=en-US,en;q=0.8,ro;q=0.6,nb;q=0.4, Content-Disposition=attachment; filename="aqmAR47_460s.jpg", http_statusCode=200, Accept-Encoding=gzip,deflate,sdch, DNT=1, Content-Type=application/octet-stream, X-Application-Context=application:5050, Accept=[text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8], id=4c1f52c8-b5f7-067c-2d60-826242d9d2db, timestamp=1406907105236}
{errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6105c7d8, http_requestMethod=GET, Cookie=m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6105c7d8, Host=localhost:8080, User-Agent=Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36, http_requestUrl=http://localhost:8080/receiveGateway, Connection=keep-alive, Accept-Language=en-US,en;q=0.8,ro;q=0.6,nb;q=0.4, Accept-Encoding=gzip,deflate,sdch, DNT=1, Accept=[text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8], id=21994eda-55aa-c798-53bc-c406e53e878d, timestamp=1406907106040}
2014-08-01 18:31:46.041  WARN 8364 --- [qtp699466412-14] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-errorChannel' with value 'org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6105c7d8' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
2014-08-01 18:31:46.042  WARN 8364 --- [qtp699466412-14] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-replyChannel' with value 'org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6105c7d8' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
2014-08-01 18:31:46.043  WARN 8364 --- [qtp699466412-14] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-id' with value '21994eda-55aa-c798-53bc-c406e53e878d' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
2014-08-01 18:31:46.043  WARN 8364 --- [qtp699466412-14] o.s.i.h.support.DefaultHttpHeaderMapper  : Header 'X-timestamp' with value '1406907106040' will not be set since it is not a String and no Converter is available. Consider registering a Converter with ConversionService (e.g., <int:converter>)
{errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6105c7d8, http_requestMethod=GET, Cookie=m=1933:86400%7C1800|2377:small|2491:chart|3247:t|34e2:|47ba:t|1d98:t|2a03:t|745a:t|77cb:t|5cf4:t|ca3:t|54e1:t|4e71:small|e69:chart|45b9:86400%7C1800|4a01:t|4c1b:t|3eff:t; JSESSIONID=8ACC161B9338DC122564C8BAB81EF25D, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@6105c7d8, Host=localhost:8080, Content-Length=33331, User-Agent=Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36, http_requestUrl=http://localhost:8080/receiveGateway, Connection=keep-alive, Server=Jetty(8.1.15.v20140411), Accept-Language=en-US,en;q=0.8,ro;q=0.6,nb;q=0.4, Content-Disposition=attachment; filename="aqmAR47_460s.jpg", http_statusCode=200, Accept-Encoding=gzip,deflate,sdch, DNT=1, Content-Type=application/octet-stream, X-Application-Context=application:5050, Accept=[text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8], id=a048606a-f03f-89d8-e981-bb661bc7e955, timestamp=1406907106058}

【问题讨论】:

  • 请显示您如何构建Content-Disposition 标头。以及使用System.out.println(message.getHeaders().toString()); 在日志中获得的内容
  • 嗨 Artem,我已经用请求的详细信息编辑了我的问题,我尝试设置 mapped-reply-headers ...第一行通常是指服务激活器中的请求消息,而第二行是回复消息,决定用'*'映射每个标题,以确保我不会在某处丢失数据

标签: http spring-integration


【解决方案1】:

很好。我现在明白了。

首先:请提出改进​​ JIRA 问题 (https://jira.spring.io/browse/INT):Content-Disposition 确实是标准 HTTP 标头,默认情况下必须映射它。

现在你可以通过注入 DefaultHttpHeaderMapper 而不是 mapped-request-headersmapped-response-headers 来克服它。此外,您必须将setUserDefinedHeaderPrefix 配置为null

默认是X-,所以我们应该把它改成填充Content-Disposition

感谢您指出!

【讨论】:

  • 谢谢,我已经按照建议发布了 INT-3488,希望没问题。明天我会测试 DefaultHttpHeaderMapper 并告诉你。
  • 解决方案确实是注入 defaulthttpheadermapper 并定义 setUserDefinedHeaderPrefix value = "" ,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
相关资源
最近更新 更多