[英]Convert JSON string to JMS Message
I have a scenario where I have to save the JMS messages on disk. 我有一种情况,必须将JMS消息保存在磁盘上。 I write the messages in JSON format using the Gson library as follows: 我使用Gson库以JSON格式编写消息,如下所示:
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? 如何从Json字符串中读取JMS消息?
Message
is an interface. Message
是一个接口。 Gson can't initialize it. Gson无法初始化它。 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. 否则,如果您需要界面-您可以看一下这篇文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.