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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.