Jackson - 如何从 JsonNode 获取对象? Jawampa [Web 应用程序消息传递协议 - 到 Java]
Jackson - How to get object from JsonNode? Jawampa [Web Application Messaging Protocol - to Java]
我正在尝试从 JsonNode
获取一个对象,一直在研究和尝试各种方法,但没有任何效果。
我试过了Jackson - Convert JsonNode into POJO
public class Pojo {
public String message;
public Pojo() {
}
@Override
public String toString() {
return "Pojo [message=" + message + "]";
}
public Pojo(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
//Register a procedure
addProcSubscription =
client1.registerProcedure("event.com.me").subscribe(new Action1<Request>() {
public void call(Request request) {
JsonNode node = request.arguments().get(0);
System.out.println("Request Pojo" + node.toString());
ObjectMapper mapper = new ObjectMapper();
try {
Pojo mPojo = mapper.treeToValue(node, Pojo.class);
System.out.println("Message Say" + mPojo.getMessage());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
request.reply("success!");
}
});
//Call a procedure
Pojo pojo = new Pojo();
pojo.setMessage("Hey friend");
Observable<Pojo> result1 = client2.call("event.com.me", Pojo.class, pojo);
result1.subscribe(new Action1<Pojo>() {
public void call(Pojo t1) {
System.out.println("Completed add with result " + t1);
}
}, new Action1<Throwable>() {
public void call(Throwable t1) {
System.out.println("Completed add with error " + t1);
}
});
如果有人能帮助我感激不尽!
结果错误:
Session1 status changed to Disconnected
Session1 status changed to Connecting
Session1 status changed to Connected
Request Pojo{"message":"Hey friend"}
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.gradle.Main$Pojo]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1063)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3023)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1637)
at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:1981)
at org.gradle.Main.call(Main.java:120)
at org.gradle.Main.call(Main.java:1)
at rx.Observable.onNext(Observable.java:7074)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130)
at ws.wamp.jawampa.WampClient.onMessageReceived(WampClient.java:698)
at ws.wamp.jawampa.WampClient.access0(WampClient.java:91)
at ws.wamp.jawampa.WampClient$SessionHandler.channelRead0(WampClient.java:359)
at ws.wamp.jawampa.WampClient$SessionHandler.channelRead0(WampClient.java:308)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at ws.wamp.jawampa.transport.WampClientWebsocketHandler.channelRead(WampClientWebsocketHandler.java:63)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:161)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:116)
at java.lang.Thread.run(Thread.java:695)
Completed add with error rx.exceptions.OnErrorThrowable: ApplicationError(jawampa.error.invalid_value_type, [], {})
首先,我没有 gradle 或 Jawampa 经验。
您的代码映射部分正确,应该可以正常运行。一个快速且独立的示例(来自您使用的任何框架)表明映射有效:
public class Pojo {
public String message;
public Pojo() {
}
@Override
public String toString() {
return "Pojo [message=" + message + "]";
}
public Pojo(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public static void main(String[] args) {
final ObjectMapper mapper = new ObjectMapper();
//in your code the passed JsonNode is populated from the request
ObjectNode node = mapper.createObjectNode();
node.put("message", "Hey friend");
try {
Pojo mPojo = mapper.treeToValue(node, Pojo.class);
System.out.println("Message -> " + mPojo.getMessage());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
在上面的示例中,如果您注释 Pojo 构造函数和 运行 代码,您将得到此错误:
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class utils.Pojo]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1063)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3023)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1637)
at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:1981)
at utils.Pojo.main(Pojo.java:47)
这个结果提出了一个问题:你"nest"这个Pojo.class到底在哪里?因为你的堆栈跟踪说它找不到构造函数 NOT 为你的 Pojo.class
但为 org.gradle.Main$Pojo
将该 POJO 放在其他地方(最好不要嵌套)然后试一试。
我正在尝试从 JsonNode
获取一个对象,一直在研究和尝试各种方法,但没有任何效果。
我试过了Jackson - Convert JsonNode into POJO
public class Pojo {
public String message;
public Pojo() {
}
@Override
public String toString() {
return "Pojo [message=" + message + "]";
}
public Pojo(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
//Register a procedure
addProcSubscription =
client1.registerProcedure("event.com.me").subscribe(new Action1<Request>() {
public void call(Request request) {
JsonNode node = request.arguments().get(0);
System.out.println("Request Pojo" + node.toString());
ObjectMapper mapper = new ObjectMapper();
try {
Pojo mPojo = mapper.treeToValue(node, Pojo.class);
System.out.println("Message Say" + mPojo.getMessage());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
request.reply("success!");
}
});
//Call a procedure
Pojo pojo = new Pojo();
pojo.setMessage("Hey friend");
Observable<Pojo> result1 = client2.call("event.com.me", Pojo.class, pojo);
result1.subscribe(new Action1<Pojo>() {
public void call(Pojo t1) {
System.out.println("Completed add with result " + t1);
}
}, new Action1<Throwable>() {
public void call(Throwable t1) {
System.out.println("Completed add with error " + t1);
}
});
如果有人能帮助我感激不尽!
结果错误:
Session1 status changed to Disconnected
Session1 status changed to Connecting
Session1 status changed to Connected
Request Pojo{"message":"Hey friend"}
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.gradle.Main$Pojo]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1063)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3023)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1637)
at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:1981)
at org.gradle.Main.call(Main.java:120)
at org.gradle.Main.call(Main.java:1)
at rx.Observable.onNext(Observable.java:7074)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130)
at ws.wamp.jawampa.WampClient.onMessageReceived(WampClient.java:698)
at ws.wamp.jawampa.WampClient.access0(WampClient.java:91)
at ws.wamp.jawampa.WampClient$SessionHandler.channelRead0(WampClient.java:359)
at ws.wamp.jawampa.WampClient$SessionHandler.channelRead0(WampClient.java:308)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at ws.wamp.jawampa.transport.WampClientWebsocketHandler.channelRead(WampClientWebsocketHandler.java:63)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:161)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:787)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:116)
at java.lang.Thread.run(Thread.java:695)
Completed add with error rx.exceptions.OnErrorThrowable: ApplicationError(jawampa.error.invalid_value_type, [], {})
首先,我没有 gradle 或 Jawampa 经验。
您的代码映射部分正确,应该可以正常运行。一个快速且独立的示例(来自您使用的任何框架)表明映射有效:
public class Pojo {
public String message;
public Pojo() {
}
@Override
public String toString() {
return "Pojo [message=" + message + "]";
}
public Pojo(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public static void main(String[] args) {
final ObjectMapper mapper = new ObjectMapper();
//in your code the passed JsonNode is populated from the request
ObjectNode node = mapper.createObjectNode();
node.put("message", "Hey friend");
try {
Pojo mPojo = mapper.treeToValue(node, Pojo.class);
System.out.println("Message -> " + mPojo.getMessage());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
在上面的示例中,如果您注释 Pojo 构造函数和 运行 代码,您将得到此错误:
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class utils.Pojo]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1063)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3023)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1637)
at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:1981)
at utils.Pojo.main(Pojo.java:47)
这个结果提出了一个问题:你"nest"这个Pojo.class到底在哪里?因为你的堆栈跟踪说它找不到构造函数 NOT 为你的 Pojo.class
但为 org.gradle.Main$Pojo
将该 POJO 放在其他地方(最好不要嵌套)然后试一试。