java - Object value as null in Jackson Parsing -


i using jackson library , trying achieve mentioned here

baseoperationrequest.java

@jsontypeinfo(    use = jsontypeinfo.id.name,    include = jsontypeinfo.as.property,    property = "command" ) @jsonsubtypes({    @jsonsubtypes.type(name = "zadd", value = zaddbaseoperationrequest.class) }) public class baseoperationrequest {    public short operationid;    public command command;    public string gameid;    public string key; } 

zaddbaseoperationrequest.java

public class zaddbaseoperationrequest extends baseoperationrequest{    public map<string, double> members; } 

command.java

public enum command{   zadd,   hset } 

the problem here when try pass object rest call this:

@restcontroller public class mycontroller{    //keeping get, change post , take in requesbody later on    @requestmapping(value = "/process/{object}", method = requestmethod.get, produces = "application/json")     public @responsebody responseentity process(@pathvariable string object){         system.out.println(object);//i getting correct--->(a)         baseoperationrequest[] baseoperationrequestarray = new objectmapper().readvalue(object, baseoperationrequest[].class);//getting exception --->(b)         system.out.println(baseoperationrequestarray);     } } 

now, calling follows:

1st scenario calling without members map:

<server>:<port>/.../process/[{"operationid":1,"command":"zadd","gameid":"t5","key":"abc"}] 

the process method getting called , since jackson told create object of zaddbaseoperationrequest when getting zadd in command, doing value of command assigned null in resultant object.

please explain why? did value of command went?

2nd scenario calling members map: :/.../process/[{"members":{"a":1.0},"operationid":1,"command":"zadd","gameid":"t5","key":"abc"}]

then in case, equation (a) showing [{"members":{"a":1.0,b that's it, did other part of went.

this making me mad. :). in advance..

please help.

it not practice send json path parameter.

to fix problem add visible=true in jsontypeinfo annotation. declaration become:

@jsontypeinfo(   use = jsontypeinfo.id.name,   include = jsontypeinfo.as.property,   property = "command",   visible = true ) 

as per jackson documentation visible:

property defines whether type identifier value passed part of json stream deserializer (true), or handled , removed typedeserializer (false). default value false, meaning jackson handles , removes type identifier json content passed jsondeserializer.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -