简体   繁体   中英

Spring Integration - http outbound gateway custom headers

I have java object that I would like to pass as a custom header to my request on the http outbound gateway. Below is a snippet

<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"/>

Where UserContext could be a java object

UserContext implements Serializable {
    String userId;
    RequestParameters params;
    ScopeEnum scope;
    ....
}

The problem I have is header user_context is not mapped in the header. From the logs, I can see that the DefaultHttpHeaderMapper is requesting for a Converter or ConversionService. See below -

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>)

How do I do this please ?

Thanks!

Standard HTTP headers are in key:value format and both key and value are strings. You try to send object as a HTTP header value which is not very wise (and almost impossible because there may be some limits on the size of headers - for example 8KB Apache default limit).

You have three options:

  1. Consider not using HTTP outbound gateway and use JMS instead (the best one in my opinion)

  2. Add transformer which will serialize UserContext to String (if it was relatively short string it would be ok, in the other case I'd not recommend it)

  3. Implement custom converter UserContext->String as described in section Datatype Channel Configuration of the spring reference documentation: http://static.springsource.org/spring-integration/reference/htmlsingle/#channel-configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM