Marshaling JSON and Generics in Java with Spring MVC -


i'm trying marshall json object wrapper class contains generic object, additional information object's signature.

public class signable<t> {      private t object;     private string signature;      public class signable() {        generatesignature();     }      /* getters , setters  */ } 

the wrapper class works fine long construct object created, , able produce desired json

@requestmapping(value="/test/json/return",method=requestmethod.get) public @responsebody signable<cart> gettest() {     cart cart = new cart();     // populate cart orderitems ...     signable<cart> sign = new signable<cart>();     sign.setobject(cart);     return sign; } 

which able generate expected output

{    "object":{         "orderitems":[               {                   "id": "****",                   "desc": "asdlfj",                   "price": 25.53               }         ]    },    "signature":"s9d94f9f9gdfg67d8678g6s87d6f7g6"; } 

which format want. however, when trying marshal same json generated signable signable, receive following error:

java.lang.classcastexception: java.util.linkedhashmap cannot cast cart 

for reason it's not able determine "object" in json should mapped type of cart (it defaults linkedhashmap), though it's specified in method header.

@requestmapping(value="/test/json/post",method=requestmethod.post) public @responsebody signable<cart> posttest(@requestbody signable<cart> sign) 

is there way explicitly types of objects want generate json insert in place of generic?

any appreciated.

thanks.

the issue here of passing proper type information, fill in missing info (which occurs due java type erasure).

but easiest way sub-class type want, have like:

class cartsignable extends signable<cart>  { } 

and use that, instead of constructing generic instance; , if so, type information correctly included (since stored in class definition of cartsignable; whereas signable has type variable!).

jackson can take in type information during serialization well, issue here of how spring pass such information. easier use sub-classing style. note can use anonymous inner-classes sub-classing well.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -