Jackson with Generics JSON Can not construct instance of java.lang.Class

iCode

I have a Java Web application with multiple web modules. One will be acting as server and other module is acting as client, both deployed in different servers. Client app will call server app through rest service to get and save data. From server I am getting a JSON string and I'm trying to convert it to object with Generic types.

Here is my object

 public class MyObject<T> {

    private String name;
    private List<T> list;

    private final Class<T> referenceType;

    @JsonCreator
    public MyObject(@JsonProperty("referenceType") Class<T> referenceType) {
        this.referenceType = referenceType;
        list = new ArrayList<T>();
    }

    public Class<T> getReferenceType() {
        return this.referenceType;
    }

//getter and setter
}

In the server I am setting the object in following way

public String getAll(Long key) {
    List<SomeObject> list = someObjectDao.getAll(key);
    MyObject<SomeObject> myObject = new MyObject<SomeObject>(
                    SomeObject.class);
    appObject.setList(list);
    JSONObject jsonget = new JSONObject(myObject);
    return jsonget.toString();
}

In the client app, I am getting the JSON string like this

{"name":"someName","referenceType":"class com.pkg.model.SomeObject","list":[{list - index - 0},{list - index-1}]}

And I am trying to convert the string to MyObject type in following way

private MyObject readJson(String output) throws Exception {
        return new ObjectMapper().readValue(output,
                    new TypeReference<MyObject>() {
                    });
    }

But I'm getting following exception,

Can not construct instance of java.lang.Class, problem: class com.pkg.model.SomeObject
 at [Source: java.io.StringReader@4f906bf5; line: 1, column: 151]

How can I convert the JSON string to object?

Thanks.

Master Slave

Your client side is OK, the JSON you're reading in is not valid

the following would deserialize properly

"referenceType":"com.pkg.model.SomeObject"

than you'll be stuck with the list part.

Setting the server side to use jackson, e.g.

return new ObjectMapper().writeValueAsString(myObject);

would fix your issue

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JSON parse error: Can not construct instance of class

Json and abstract class 'Can not construct instance'

Jackson JSON Can not construct instance of "About" : deserialize issue

Jackson JSON + Java Generics

jackson throw JsonMappingException can not construct instance of

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of : poja class no suitable constructor found

Jackson: InvalidFormatException: Can not construct instance of java.util.Date from String value

Cannot construct instance of - Jackson

Construct class instance from class object in java

Jackson JSON + Java Generics get LinkedHashMap

java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B

Can we create different class instance using Java generics by passing class type as argument

Java Generics, Create an instance of Class<T>

java.lang.Class generics and wildcards

Getting a java.lang.Class with generics

Can't deserialize extended abstract class json using Jackson in Java

Jackson to deserialize JSON with generics

JSON Serializing date in a custom format (Can not construct instance of java.util.Date from String value)

Can I use willSet to construct a class instance dictionary entry?

JsonMappingException: Can not construct instance of

Creating an instance of a parameterized class using an instance of java.lang.Class

Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

How can I detect a class instance with generics in typescript

In TypeScript, how can you extract a class instance type using generics?

JSON: InvalidFormatException: Can not construct instance of int from String value

Jackson cannot construct instance with one parameter constructor

Java Generics - Why returning super class instance gives compile error

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:Cannot construct instance of`java.time.Instant`(no Creators,like default construct):

How can i make a class Immutable if it has a instance variable of java.lang.Object?