Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson - How to get object from JsonNode? Jawampa [Web Application Messaging Protocol - to Java]

I'm trying to get an object from a JsonNode, have been researching and trying various things but nothing works.

I tried this 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);
    }
});

If anyone can help I am grateful!

Result Error:

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$1$1.call(Main.java:120)
    at org.gradle.Main$1$1.call(Main.java:1)
    at rx.Observable$31.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.access$400(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$2.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, [], {})
like image 604
Anderson K Avatar asked Jun 19 '26 18:06

Anderson K


1 Answers

First of all, I have no gradle or Jawampa experience.

The mapping part of your code is correct and it should function properly. A quick and isolated example(from whatever framework you're using) shows that the mapping works:

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();
    }
}

}

In the above example, if you comment the Pojo constructor and run the code you will get this ERROR:

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)

This result raises the question: Where exactly did you "nest" this Pojo.class? Cause your stacktrace says that it cannot find a constructor NOT for your Pojo.class but for org.gradle.Main$Pojo Put that POJO somewhere else(preferably not nested) then give it a try.

like image 87
sebster Avatar answered Jun 21 '26 08:06

sebster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!