Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring集成-http出站网关自定义头_Http_Header_Spring Integration_Outbound - Fatal编程技术网

Spring集成-http出站网关自定义头

Spring集成-http出站网关自定义头,http,header,spring-integration,outbound,Http,Header,Spring Integration,Outbound,我有一个java对象,我想在http出站网关上将其作为自定义头传递给我的请求。下面是一个片段 <int:gateway id="service" service-interface="MyService" default-request-channel="requestChannel" default-reply-channel="replyChannel"> <int:method name="doSomething" payload-expression="#arg

我有一个java对象,我想在http出站网关上将其作为自定义头传递给我的请求。下面是一个片段

<int:gateway id="service" service-interface="MyService" default-request-channel="requestChannel" default-reply-channel="replyChannel">
    <int:method name="doSomething" payload-expression="#args[0] + ',' + #args[1]">
        <int:header name="method_name" value="login"/>
        <int:header name="service_identifier" value="myService"/>
        </int:method>                
</int:gateway>

<int:header-enricher input-channel="requestChannel" output-channel="gatewayChannel">
       <int:header name="user_context" expression="T(UserContextHolder).getContext()"/>
</int:header-enricher>

<int-http:outbound-gateway request-channel="gatewayChannel" url="myURL" mapped-request-headers="user_context, service_identifier, method_name, HTTP_REQUEST_HEADERS"
          http-method="POST" reply-channel="replyChannel"/>
我遇到的问题是标题用户上下文未映射到标题中。从日志中,我可以看到DefaultHttpHeaderMapper正在请求转换器或转换服务。见下文-

09:54:59,488 - WARN main      org.springframework.integration.http.support.DefaultHttpHeaderMapper - Header 'X-    user_context' with value 'UserContextImpl@5e3ca754' 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>)
请问我该怎么做


谢谢

标准HTTP头采用key:value格式,key和value都是字符串。 您尝试将对象作为HTTP头值发送,这不是很明智,而且几乎不可能,因为头的大小可能有一些限制—例如8KB Apache默认限制

您有三种选择:

考虑不使用HTTP出站网关,而是使用JMS,这是我认为最好的

添加transformer,它将UserContext序列化为字符串,如果它是相对较短的字符串,那么就可以了,在另一种情况下,我不推荐使用它

实现自定义转换器UserContext->String,如spring参考文档数据类型通道配置部分所述:
09:54:59,488 - WARN main      org.springframework.integration.http.support.DefaultHttpHeaderMapper - Header 'X-    user_context' with value 'UserContextImpl@5e3ca754' 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>)