1

我正在尝试设置基于 Spring 集成的 Restful 服务。我试图避免为请求和响应创建单独的对象,而是将 HashMap 用于简单的功能,例如服务时的isUserAvailableForRegistration

例如

HashMap<String, String> isUserAvailableForRegistration(HashMap<String,String> request)

以下是集成相关的上下文信息:

<bean class="org.springframework.integration.http.inbound.UriPathHandlerMapping"/>

<int-http:inbound-gateway id="inboundGateway"
    request-channel="incoming" reply-channel="outgoing" 
    supported-methods="POST" reply-timeout="5000"
    path="/customer/isUserAvailable" />

<int:channel id="incoming" />
<int:channel id="outgoing" />

<int:service-activator id="activator" input-channel="incoming" ref="customerManagementGateway"
    method="isUserAvailableForRegistration" output-channel="outgoing" requires-reply="true" />

<int:object-to-json-transformer id="outgoingJsonConverter"
    input-channel="outgoing" />

<int:json-to-object-transformer id="incomingJsonConverter" input-channel="incoming" type="java.util.HashMap"  />

虽然我可以成功测试我可以成功地将 HashMap 编组为 JSON,但我缺少将 JSON 数据解组为 HashMap 的配置。

我得到以下异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.integration.dispatcher.AggregateMessageDeliveryException: All attempts to deliver Message to MessageHandlers failed. Multiple causes:
    failed to transform message
    org.springframework.expression.AccessException: Problem invoking method: public java.util.Map com.testapp.customer.CustomerManagementServiceActivator.isUserAvailableForRegistration(java.util.Map)
See below for the stacktrace of the first cause.
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

org.springframework.integration.dispatcher.AggregateMessageDeliveryException: All attempts to deliver Message to MessageHandlers failed. Multiple causes:
    failed to transform message
    org.springframework.expression.AccessException: Problem invoking method: public java.util.Map com.testapp.customer.CustomerManagementServiceActivator.isUserAvailableForRegistration(java.util.Map)
See below for the stacktrace of the first cause.
    org.springframework.integration.dispatcher.UnicastingDispatcher.handleExceptions(UnicastingDispatcher.java:164)
    org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:127)
    org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
    org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
    org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
    org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
    org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
    org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:233)
    org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:207)
    org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:378)
    org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:95)
    org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

org.springframework.integration.transformer.MessageTransformationException: failed to transform message
    org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:44)
    org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:67)
    org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:97)
    org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
    org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:114)
    org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
    org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
    org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
    org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
    org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
    org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:233)
    org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:207)
    org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:378)
    org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:95)
    org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.ClassCastException: [B cannot be cast to java.lang.String
    org.springframework.integration.json.JsonToObjectTransformer.transformPayload(JsonToObjectTransformer.java:30)
    org.springframework.integration.transformer.AbstractPayloadTransformer.doTransform(AbstractPayloadTransformer.java:33)
    org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)
    org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:67)
    org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:97)
    org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:73)
    org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:114)
    org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:101)
    org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
    org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288)
    org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318)
    org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239)
    org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:233)
    org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:207)
    org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.doHandleRequest(HttpRequestHandlingEndpointSupport.java:378)
    org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway.handleRequest(HttpRequestHandlingMessagingGateway.java:95)
    org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

任何人都可以帮我弄清楚可能出了什么问题吗?

我已经仔细检查了标题,因此我确信这是配置或杰克逊对解组到 HashMap 的支持。谢谢你的时间。

4

2 回答 2

4

正如 Biju 提到的,集成图不正确,但在消息到达 json-to-object-transformer 之前就抛出了错误。正确修复集成图后,我仍然收到此错误。在 spring 集成论坛中搜索后,我发现在 int-http:inbound-gateway 网关中提到 request-payload-type 解决了这个问题。

所以正确的配置是

    <int-http:inbound-gateway id="inboundGateway"
     request-channel="incoming" reply-channel="outgoing" 
     supported-methods="POST" reply-timeout="5000"
     path="/customer/isUserAvailable" request-payload-type="String" />
于 2012-03-02T21:57:22.737 回答
0

看起来您没有正确连接不同的端点和通道 -

这是您拥有的流程:

httpgateway->incoming(c)->customerManagementGateway->outgoing(c)->outgoingJsonConverter
                        ->incomingJsonConverter

incomingJsonConverter 和 customerManagementGateway 似乎都在消耗传入通道

不过,这可能不是错误消息的主要原因,我认为您需要将字节数组转换为 json 转换器的 String 有效负载:尝试在您的 inputchannel 和 jsonconverter 之间引入此转换器:JsonToObjectTransformer:

<int:object-to-string-transformer input-channel="in" output-channel="out"/>
于 2012-02-03T22:18:56.840 回答