简体   繁体   English

将JSON字符串转换为JMS消息

[英]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.

相关问题 将String转换为javax.jms.Message - Convert String to javax.jms.Message 想把一个 XML String 转换成 JMS 消息 object - Want to convert an XML String into a JMS message object 在ActiveMq中将JSON消息转换为javax.jms.ObjectMessage - Convert JSON message to javax.jms.ObjectMessage in ActiveMq Spring JMS JSON消息转换器 - Spring JMS json message converter 用Java将字符串转换为JMS BytesMessage - Convert String to a JMS BytesMessage in Java 如何将对象的 JSONArray 转换为 json 字符串消息 - How to convert JSONArray of objects to a json string message 将javax.jms.Message转换为com.ibm.jms.JMSTextMessage - Convert javax.jms.Message to com.ibm.jms.JMSTextMessage 无法将类型的对象转换为JMS消息。 支持的消息有效负载为:字符串,字节数组,映射 <String,?> ,可序列化的对象 - Cannot convert object of type to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object org.springframework.messaging.converter.MessageConversionException:无法从 [org.apache.qpid.jms.message.JmsMessage] 转换为 [java.lang.String] - org.springframework.messaging.converter.MessageConversionException: Cannot convert from [org.apache.qpid.jms.message.JmsMessage] to [java.lang.String] 无法将消息从 Json 字符串转换为对象。 类转换异常 - Not able to convert Message from Json String to Object. ClassCastException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM