Spring Integration - http 出站网关自定义标头

发布于 2024-12-10 18:46:33 字数 1643 浏览 0 评论 0原文

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

,其中 UserContext 可能是一个 java 对象

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

我遇到的问题是标头 user_context 未映射到标头中。从日志中,我可以看到 DefaultHttpHeaderMapper 正在请求 Converter 或 ConversionService。请参阅下文 -

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

请问我该怎么做?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望她远 2024-12-17 18:46:33

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

您有三个选择:

  1. 考虑不使用 HTTP 出站网关并使用 JMS(我认为最好的一个)

  2. 添加将 UserContext 序列化为 String 的转换器(如果它是相对较短的字符串,那就没问题,在其他情况下我不推荐它)

  3. 按照描述实现自定义转换器 UserContext->String在 spring 参考文档的数据类型通道配置部分:
    http://static.springsource.org/spring-integration/reference/ htmlsingle/#通道配置

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文