Http网关和Json变形金刚:剥离的标题

时间:2014-08-23 04:31:42

标签: java json spring-integration

将Spring Integration 4.0.3.RELEASE与Jackson2一起使用,我试图将Json Transformers与http网关结合使用。

我的路径看起来类似于Spring Integration文档中引用的amqp适配器,但是使用http网关(1):

  

...->对象到JSON-变压器 - > HTTP的出站网关---->

     

----> HTTP的入站网关 - > JSON到对象的变压器 - >

客户端:     

<int:chain id="messageTransformerChain" input-channel="toServer" output-channel="messagesToServer">
    ...
    <int:object-to-json-transformer />
</int:chain>

<int-http:outbound-gateway id="httpOutToSever"
    request-channel="messagesToServer"
    reply-channel="replyFromServer"
    url="${server.endpoint}"
    http-method="POST"
    extract-request-payload="true"
    expected-response-type="java.lang.String" />

服务器:

<int-http:inbound-gateway id="httpInboundFromClientGateway"
    supported-methods="GET, POST"
    request-channel="fromClientChannel" />

<int:chain input-channel="fromClientChannel">
    <int:json-to-object-transformer />
    ...
</int:chain>

我得到的例外是(我认为相关的是粗体):

  

2014-08-22T23:58:21.640-0400警告[http-bio-8080-exec-2]   o.s.i.h.i.HttpRequestHandlingMessagingGateway失败发生在   网关sendAndReceive   org.springframework.integration.transformer.MessageTransformationException:   无法转换消息   org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:44)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:68)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:170)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:131)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:101)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:255)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:109)   〜[spring-messaging-4.0.6.RELEASE.jar:4.0.6.RELEASE] at   org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:144)   〜[spring-messaging-4.0.6.RELEASE.jar:4.0.6.RELEASE] at   org.springframework.messaging.core.GenericMessagingTemplate.doSendAndReceive(GenericMessagingTemplate.java:44)   〜[spring-messaging-4.0.6.RELEASE.jar:4.0.6.RELEASE] at   org.springframework.messaging.core.AbstractMessagingTemplate.sendAndReceive(AbstractMessagingTemplate.java:75)   〜[spring-messaging-4.0.6.RELEASE.jar:4.0.6.RELEASE] at   org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:250)   [spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:224)   [spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.actualDoHandleRequest(HttpRequestHandlingEndpointSupport.java:473)   [spring-integration-http-4.0.3.RELEASE.jar:na] at   org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:370)   [spring-integration-http-4.0.3.RELEASE.jar:na] at   org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:101)   [spring-integration-http-4.0.3.RELEASE.jar:na] at   org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)   [spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE] at   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)    [spring-webmvc-4.0.6.RELEASE.jar:4.0.6.RELEASE]引起:   java.lang.IllegalArgumentException:无法解析&#39; json__TypeId __&#39;   in&#39; javaTypes&#39;。在   org.springframework.integration.support.json.AbstractJacksonJsonObjectMapper.createJavaType(AbstractJacksonJsonObjectMapper.java:67)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.support.json.Jackson2JsonObjectMapper.extractJavaType(Jackson2JsonObjectMapper.java:119)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.support.json.Jackson2JsonObjectMapper.extractJavaType(Jackson2JsonObjectMapper.java:44)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.support.json.AbstractJacksonJsonObjectMapper.fromJson(AbstractJacksonJsonObjectMapper.java:60)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.json.JsonToObjectTransformer.doTransform(JsonToObjectTransformer.java:81)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] at   org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)   〜[spring-integration-core-4.0.3.RELEASE.jar:na] ... 102个常见帧   省略

当我调试时,我在客户端看到json__TypeId__标头已从传出消息中删除。当我将JsonHeaders.HEADERS添加到DefaultHttpHeaderMapper并使用此映射器注入网关时,我看到当映射器工作完成时,Json标头存在,但是当我最终返回{{1}时} RestTemplate.doWithRequest不再包含Json。当然,我得到了相同的异常服务器端。

也许我的配置不正确,但是当该标题被剥离时,我不确定服务器端如何能够进行requestHeaders转换。

我不希望明确指出特定Mappers上的类型,因为它们可能会有所不同。有没有办法配置支持这条路径所需的组件?

- 参考文献 -

(1)http://docs.spring.io/spring-integration/reference/html/messaging-transformation-chapter.html#transformer滚动到Json Transformers小节

1 个答案:

答案 0 :(得分:1)

使用出站http适配器,用户标头(在他的情况下是json类型信息)通过HTTP传送为X-<header> ...

<int-http:outbound-gateway request-channel="requestChannel" 
                           url="http://localhost:18080/http/receiveGateway"
                           http-method="POST"
                           mapped-request-headers="foo"
                           expected-response-type="java.lang.String"/>

POST /http/receiveGateway HTTP/1.1
Accept: text/plain, */*
X-foo: bar

在服务器端,X-未被移除,因此您必须将其映射为x ...

<int-http:inbound-gateway request-channel="receiveChannel"
                      path="/receiveGateway"
                      mapped-request-headers="x-foo"
                      supported-methods="POST"/>

然后,您需要在变换器<header-enricher/>之前<chain/>设置标题(减去'x - ')......

<int:header-enricher>
    <int:header name="json__TypeId__" expression="headers['x-json__TypeId__']" />
    ...
</int:header-enricher>

(注意小写x)。

编辑:

或者,在发送方,您可以将映射器配置为不为自定义标头添加前缀...

<bean id="mapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="outboundHeaderNames" value="foo,bar" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>
相关问题