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?