java - RESTEasy POJO json "embedded" objects as links/id's -


i have problem struggling how represent embedded objects in pojo links, instead of embedding them directly.

i'm unsing resteasy jettison provider. in 3.0.11.final version.

the book.java pojo

public class book {     private  integer bookid;     private  string title;     private  author author; } 

the author.java pojo

public class author {     private  integer authorid;     private  string name; } 

when use resteasy generate xml or json representation of book, see this:

<book>     <bookid>1</bookid>     <title>my book</title>     <author>         <authorid>2</authorid>         <name>andre schild</name>     </author> </book> 

but wan't have this:

<book>     <bookid>1</bookid>     <title>my book</title>     <author author="2"><atom:link rel="list" href="http://.../rest/authors/2"/></author> </book> 

since use jpa db backend connection, pojo directly contain author pojo, , not id.

i did tests jersey, did got stuck on same problem

as nobody knows relation between author field , uri can't happen automatically. plugin reasteasy-links can define these relations 2 annotations @addlinks , @linkresource. can find more information in docs.

this plugin not change value of field add atom links entity. works jettison , jaxb.

here's quick + dirty example using jackson replaces value of author field.

we'll use annotation define relation between pojo , resource:

@retention(retentionpolicy.runtime) @target(elementtype.type) public @interface linked {      string path();      string rel() default "";  } 

this annotation needs applied author class:

@linked(path = "/rest/authors/{authorid}", rel = "list") public class author {} 

at book field need add serializer want use:

public class book {     @jsonserialize(using = linkedserializer.class)     private author author; } 

the serializer this:

public class linkedserializer extends jsonserializer<object> {      @override     public void serialize(object value, jsongenerator jgen, serializerprovider provider) throws ioexception, jsonprocessingexception {         linked linked = value.getclass().getannotation(linked.class);         matcher matcher = pattern.compile("\\{(.+?)\\}").matcher(linked.path());         if (!matcher.find()) {             return;         }         string param = matcher.group(1);         try {             field field = value.getclass().getdeclaredfield(param);             field.setaccessible(true);             object paramvalue = field.get(value);             string path = linked.path().replacefirst("\\{.*\\}", paramvalue.tostring());             jgen.writestartobject();             jgen.writefieldname("href");             jgen.writestring(path);             jgen.writefieldname("rel");             jgen.writestring(linked.rel());             jgen.writeendobject();         } catch (nosuchfieldexception | securityexception | illegalaccessexception ex) {             throw new illegalargumentexception(ex);         }     }  } 

note: we're using path , not full uri here don't know base uri , can't inject uriinfo or servletrequest in serializer. servletrequest via resteasyproviderfactory.getcontextdata(httpservletrequest.class).


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -