java - Custom object deserializes fine in Jax-RS but if it is used in another object it doesn't work -


deserializing works fine if pass custom object through

@post public response savecustomobject(customobject data) {     // prints correct value     system.out.println(data); } 

however, if property on object, gets default value of custom object

@post public response savecustomobjectwrapper(customobjectwrapper data) {     // prints incorrect value     system.out.println(data.getcustomobject()); } 

my provider registered , looks this:

public customobject readfrom(class<customobject> type, type type1, annotation[] antns, mediatype mt, multivaluedmap<string, string> mm, inputstream in) throws ioexception, webapplicationexception {     try {         return new customobject(ioutils.tostring(in));     } catch (exception ex) {         throw new processingexception("error deserializing customobject.", ex);     } } 

the problem reader other objects doesn't lookup/delegation while unmarshalling. mean that, can seen in this answer, 1 reader looks reader based on type. assuming format json, whether you're using moxy (the default glassfish) or jackson, result same. reader smart enough handle the json itself, doesn't need lookup other readers.

one solution create reader wrapper class, , lookup/delegation, seen in link above. if have lot of these situations, may can extend default reader, , override unmarshalling method, advise against this, unless know you're doing.

another solution, depending on serializer you're using, write jsondeserializer (for jackson) or xmladapter (for moxy or jackson). jackson example (you can see better example here)

public class customobjectdeserializer extends jsondeserializer<customobject> {       @override     public customobject deserialize(jsonparser jp, deserializationcontext dc)              throws ioexception, jsonprocessingexception {         jsonnode node = jp.getcodec().readtree(jp);         return new customobject("hello world");     } }  @jsondeserialize(using = customobjectdeserializer.class) public class customobject {     public string message;     public string getmessage() { return message; }     public void setmessage(string message) { this.message = message; }     public customobject(string message) { this.message = message; }     public customobject(){} } 

in case, there no need custom reader @ all. handle customobjects , objects have customobject member. 1 problem i'm not sure how or if can inputstream. need use jackson apis parse json.

if want use jackson instead of default moxy glassfish, can add jackson dependency

<dependency>     <groupid>org.glassfish.jersey.media</groupid>     <artifactid>jersey-media-json-jackson</artifactid>     <version>2.13</version> </dependency> 

then register jacksonfeature, or disable moxy, mentioned here. if want continue using moxy, don't know if there such thing class level adapter, still need reader create xmladapter class members. it's bit of hassle, that's why recommend jackson, many other reasons, besides particular use case. can see example of adapter here

now lot of answer based on assumption using json format, haven't specified media type using. if other format, think maybe solution create customer reader wrapper.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -