vasanth
vasanth

Reputation: 130

Spring Integration: HTTP Inbound Gateway - using JSON to Object transformer for HashMap

I am trying to setup a Spring Integration based Restful service. I am trying to avoid creating individual objects for request and response but instead use a HashMap for simple functions such as isUserAvailableForRegistration at service.

e.g

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

The following is the integration related context information:

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

While I can successfully test that I could marshall HashMap to JSON successfully, I am missing the configuration to unmarshall JSON data into HashMap.

I get the following exception:

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)

Can anybody help me with figuring out what could possible be wrong ?

I have double checked the headers, hence i am sure that I it's something with the configuration or Jackson's support for unmarshalling to HashMap. Thanks for your time.

Upvotes: 1

Views: 11838

Answers (2)

vasanth
vasanth

Reputation: 130

As Biju mentioned the integration graph wasnt correct, but the error was thrown even before the message reached json-to-object-transformer. After fixing the integration graph correctly i still was getting this error. after searching in spring integration forum, i found that mentioning request-payload-type in the int-http:inbound-gateway gateway solved the problem.

So the correct configuration is

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

Upvotes: 4

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

Doesn't look like you have connected the different endpoints and channels correctly -

This is the flow that you have:

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

Both incomingJsonConverter and customerManagementGateway seem to be consuming off incoming channel

This may not be the main reason for the error message though, I think you need to convert the byte array to a String payload for the json transformer: try introducing this transformer also between your inputchannel and jsonconverter: JsonToObjectTransformer:

<int:object-to-string-transformer input-channel="in" output-channel="out"/>

Upvotes: 0

Related Questions