Announcement Announcement Module
Collapse
No announcement yet.
JsonMessageConverter for Generics Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JsonMessageConverter for Generics

    Hi,

    Our question is around the usage of JsonMessageConverter for objects that uses generics. We have created our own message class that uses generics <T> for the payload. For example:

    public class AMQPMessage<T> {
    private T payload;

    //////// some other stuff...
    }

    We have tested our generic message class using jackson API directly, something like

    objectMapper.readValue(src, new TypeReference<AMQPMessage<CustomerMessage>>());

    and is working perfectly fine.

    But when we use Spring AMQP provided JsonMessageConverter we get class cast exception, not able to cast Map to our provided message class.
    What we understand here is that JsonMessageConverter and other involved Spring AMQP classes are not able to resolve generics <T> and uses Map as default.

    We know it is something related to DefaultJavaTypeMapper and MapperClass but we don't know exactly how to use them. Please shed some light on this issue and let us know how to fix it with an example code.

    Thanks in advance

  • #2
    Can you attach a full stack trace of the exception?

    Comment


    • #3
      Sure, here is the stack trace:

      -------------------------------------------------------------------------------
      Test set: com.mycompany.producer.MessageProducerTest
      -------------------------------------------------------------------------------
      Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.021 sec <<< FAILURE!
      testMessageProducer(com.mycompany.producer.Message ProducerTest) Time elapsed: 1.994 sec <<< ERROR!
      java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.mycompany.producer.MyMessage
      at com.mycompany.producer.MessageProducerTest.testGen ericMessageConversion(MessageProducerTest.java:86)
      at com.mycompany.producer.MessageProducerTest.testMes sageProducer(MessageProducerTest.java:65)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:45)
      at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
      at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:42)
      at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
      at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:28)
      at org.junit.internal.runners.statements.RunAfters.ev aluate(RunAfters.java:30)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunne r.java:263)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:68)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:47)
      at org.junit.runners.ParentRunner$3.run(ParentRunner. java:231)
      at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:60)
      at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:229)
      at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:50)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:222)
      at org.junit.runners.ParentRunner.run(ParentRunner.ja va:300)
      at org.apache.maven.surefire.junit4.JUnit4TestSet.exe cute(JUnit4TestSet.java:62)
      at org.apache.maven.surefire.suite.AbstractDirectoryT estSuite.executeTestSet(AbstractDirectoryTestSuite .java:140)
      at org.apache.maven.surefire.suite.AbstractDirectoryT estSuite.execute(AbstractDirectoryTestSuite.java:1 27)
      at org.apache.maven.surefire.Surefire.run(Surefire.ja va:177)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.maven.surefire.booter.SurefireBooter.ru nSuitesInProcess(SurefireBooter.java:345)
      at org.apache.maven.surefire.booter.SurefireBooter.ma in(SurefireBooter.java:1009)

      Comment


      • #4
        Thanks; the converter doesn't currently support the jackson methods that use TypeReference; please open an Improvement JIRA issue here https://jira.springsource.org/browse/INT

        In the meantime, you can write a custom converter; just extend AbstractMessageConverter and implement createMessage() and fromMessage().

        Comment

        Working...
        X