0

I have a scenario where I have to save the JMS messages on disk. I write the messages in JSON format using the Gson library as follows:

Gson gson = new Gson();
String json = gson.toJson(message);
bufferedWriter.write(json);

But when I try to read this message,

Message m = gson.fromJson(json, Message.class);
System.out.println(m.getJMSType());

I get an exception:

java.lang.RuntimeException: Unable to invoke no-args constructor for interface javax.jms.Message. Register an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$12.construct(ConstructorConstructor.java:210)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:207)
    at com.google.gson.Gson.fromJson(Gson.java:814)
    at com.google.gson.Gson.fromJson(Gson.java:779)
    at com.google.gson.Gson.fromJson(Gson.java:728)
    at com.google.gson.Gson.fromJson(Gson.java:700)

How do I read a JMS message from Json string?

trailblazer
  • 1,421
  • 5
  • 20
  • 43

1 Answers1

0

Message is an interface. Gson can't initialize it. If the actual implementation class is known/under your control - you may replace deserialization with something like

Message m = gson.fromJson(json, ActiveMQBlobMessage.class);

Otherwise, if you need interface - you may look at this post.

Community
  • 1
  • 1
Peter Alexeev
  • 218
  • 2
  • 6