Announcement Announcement Module
Collapse
No announcement yet.
byte[] to json json-to-object-transformer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • byte[] to json json-to-object-transformer

    I'm trying to set up a simple intergration to get a rabbitmq message which is json, into mongodb. My setup is having issues with the json-to-object-transformer. What I'm expecting to happen is the transformer to convert the json message into a object called User but I'm getting the following error.

    Caused by: java.lang.ClassCastException: [B cannot be cast to java.lang.String

    Should I be putting the message into the queue differently or is my configuration below missing something.

    How can I convert the payload?

    <amqp:inbound-channel-adapter channel="amqpInboundChannel" queue-names="request.all" connection-factory="rabbitConnFactory" />

    <int:channel id="amqpInboundChannel" />

    <int:json-to-object-transformer input-channel="amqpInboundChannel" type="com.app.User" output-channel="cr" />

    <int:channel id="cr"></int:channel>

    <int:service-activator input-channel="cr" ref="ampqHandler" output-channel="nullChannel" method="handleMessage" />

    <bean id="ampqHandler" class="com.jbhunt.app.MessageHandler" />

    MessageHandler class

    package com.jbhunt.app;

    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.data.mongodb.core.MongoTemplat e;

    public class MessageHandler {

    protected Logger logger = Logger.getLogger("integration");
    @Autowired
    private MongoTemplate mongoTemplate;

    public void handleMessage(User request) {
    logger.info("Received: " + request.getFirstname());

    mongoTemplate.save(request,"all");
    }

    }

    JSON Message Example/ RabbitMq Payload
    {"firstname" : "yong", "lastname" : "mook kim", "age" : 30 }


    Full stack
    DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (BlockingQueueConsumer.java:nextMessage:185) Retrieving delivery for Consumer: tag=[amq.ctag-Aq396eCVyGb8koainwH5BP], channel=Cached Rabbit Channel: AMQChannel(amqp://[email protected]:5672/,1), acknowledgeMode=AUTO local queue size=1
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (BlockingQueueConsumer.java:handle:157) Received message: (Body:'[B@60232c(byte[60])'; ID:null; Content:null; Headers:{}; Exchange:; RoutingKey:request.all; Reply:null; DeliveryMode:NON_PERSISTENT; DeliveryTag:4662)
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractHeaderMapper.java:shouldMapHeader:217) headerName=[amqp_receivedRoutingKey] WILL be mapped, matched pattern=amqp_receivedRoutingKey
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractHeaderMapper.java:shouldMapHeader:217) headerName=[amqp_deliveryMode] WILL be mapped, matched pattern=amqp_deliveryMode
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractHeaderMapper.java:shouldMapHeader:217) headerName=[amqp_redelivered] WILL be mapped, matched pattern=amqp_redelivered
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractHeaderMapper.java:shouldMapHeader:217) headerName=[amqp_deliveryTag] WILL be mapped, matched pattern=amqp_deliveryTag
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractMessageChannel.javareSend:224) preSend on channel 'amqpInboundChannel', message: [Payload=[B@60232c][Headers={timestamp=1345326568616, id=df8a1e55-88c2-4445-807e-121a733ef8b8, amqp_receivedRoutingKey=request.all, amqp_deliveryMode=NON_PERSISTENT, amqp_redelivered=true, amqp_deliveryTag=4662}]
    [DEBUG] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractMessageHandler.java:handleMessage:67) org.springframework.integration.transformer.Messag eTransformingHandler#0 received message: [Payload=[B@60232c][Headers={timestamp=1345326568616, id=df8a1e55-88c2-4445-807e-121a733ef8b8, amqp_receivedRoutingKey=request.all, amqp_deliveryMode=NON_PERSISTENT, amqp_redelivered=true, amqp_deliveryTag=4662}]
    [ WARN] [SimpleAsyncTaskExecutor-1 04:49:28] (AbstractMessageListenerContainer.java:invokeError Handler:428) Execution of Rabbit message listener failed, and no ErrorHandler has been set.
    org.springframework.amqp.rabbit.listener.ListenerE xecutionFailedException: Listener threw exception
    at org.springframework.amqp.rabbit.listener.AbstractM essageListenerContainer.wrapToListenerExecutionFai ledExceptionIfNeeded(AbstractMessageListenerContai ner.java:589)
    at org.springframework.amqp.rabbit.listener.AbstractM essageListenerContainer.doInvokeListener(AbstractM essageListenerContainer.java:528)
    at org.springframework.amqp.rabbit.listener.AbstractM essageListenerContainer.invokeListener(AbstractMes sageListenerContainer.java:471)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.access$001(SimpleMessageList enerContainer.java:57)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer$1.invokeListener(SimpleMessa geListenerContainer.java:106)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.invokeListener(SimpleMessage ListenerContainer.java:587)
    at org.springframework.amqp.rabbit.listener.AbstractM essageListenerContainer.executeListener(AbstractMe ssageListenerContainer.java:451)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.doReceiveAndExecute(SimpleMe ssageListenerContainer.java:463)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.receiveAndExecute(SimpleMess ageListenerContainer.java:447)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer.access$200(SimpleMessageList enerContainer.java:57)
    at org.springframework.amqp.rabbit.listener.SimpleMes sageListenerContainer$AsyncMessageProcessingConsum er.run(SimpleMessageListenerContainer.java:532)
    at java.lang.Thread.run(Unknown Source)
    Caused by: org.springframework.integration.transformer.Messag eTransformationException: failed to transform message
    at org.springframework.integration.transformer.Abstra ctTransformer.transform(AbstractTransformer.java:4 4)
    at org.springframework.integration.transformer.Messag eTransformingHandler.handleRequestMessage(MessageT ransformingHandler.java:67)
    at org.springframework.integration.handler.AbstractRe plyProducingMessageHandler.handleMessageInternal(A bstractReplyProducingMessageHandler.java:97)
    at org.springframework.integration.handler.AbstractMe ssageHandler.handleMessage(AbstractMessageHandler. java:73)
    at org.springframework.integration.dispatcher.Unicast ingDispatcher.doDispatch(UnicastingDispatcher.java :115)
    at org.springframework.integration.dispatcher.Unicast ingDispatcher.dispatch(UnicastingDispatcher.java:1 02)
    at org.springframework.integration.channel.AbstractSu bscribableChannel.doSend(AbstractSubscribableChann el.java:77)
    at org.springframework.integration.channel.AbstractMe ssageChannel.send(AbstractMessageChannel.java:157)
    at org.springframework.integration.channel.AbstractMe ssageChannel.send(AbstractMessageChannel.java:128)
    at org.springframework.integration.core.MessagingTemp late.doSend(MessagingTemplate.java:288)
    at org.springframework.integration.core.MessagingTemp late.send(MessagingTemplate.java:149)
    at org.springframework.integration.endpoint.MessagePr oducerSupport.sendMessage(MessageProducerSupport.j ava:92)
    at org.springframework.integration.amqp.inbound.AmqpI nboundChannelAdapter.access$200(AmqpInboundChannel Adapter.java:39)
    at org.springframework.integration.amqp.inbound.AmqpI nboundChannelAdapter$1.onMessage(AmqpInboundChanne lAdapter.java:73)
    at org.springframework.amqp.rabbit.listener.AbstractM essageListenerContainer.doInvokeListener(AbstractM essageListenerContainer.java:526)
    ... 10 more
    Caused by: java.lang.ClassCastException: [B cannot be cast to java.lang.String
    at org.springframework.integration.json.JsonToObjectT ransformer.transformPayload(JsonToObjectTransforme r.java:30)
    at org.springframework.integration.transformer.Abstra ctPayloadTransformer.doTransform(AbstractPayloadTr ansformer.java:33)
    at org.springframework.integration.transformer.Abstra ctTransformer.transform(AbstractTransformer.java:3 3)
    ... 24 more

  • #2
    Please use [ code ] ... [ /code ] tags around code and config (no spaces in brackets).

    If your client sets the Content-type to "text/plain", the inbound adapter will automatically convert the message to String.

    If you can't change the client, you either need to add a byte[] to String transformer, or you can add a byte[] to String converter and set the datatype on amqpInboundChannel to java.lang.String.

    The transformer can be as simple as

    Code:
    	<int:transformer id="serverBytes2String"
    		input-channel="amqpInboundChannel"
    		output-channel="toJsonTransformer"
    		expression="new String(payload)"/>
    But, the simplest solution is to have the client set the contentType on the message properties.

    Comment


    • #3
      Setting the content type on the message did the trick. Thanks for the quick reply and example.

      Comment

      Working...
      X