Spring boot rest service, how to get it to marshal the links as properties? -


first question here gentle :)

i've got jpa project want expose rest. i've done far:

my entity:

@entity public class signupsheet {     @id     @generatedvalue     private long id;      @column     private string name;      @column     private string description;      @column     @temporal(temporaltype.timestamp)     private date datetime;      @manytoone     private user parent;      @onetomany     private list<volunteer> volunteers;      //getter , setters } 

all , good, call added spring-boot-starter-data-rest pom , service. here's json back.

http://localhost:8080/api-0.1.0/signupsheets/1  {   "name": "auction",   "description": "my first sign sheet",   "datetime": "2015-04-22t03:47:12.000+0000",   "volunteers": [    {     "role": "bringing stuff",     "comments": "i have comments!"    }    ],    "endpoint": "/signupsheets",   "_links": {     "self": {       "href": "http://localhost:8080/api-0.1.0/signupsheets/1"     },     "parent": {       "href": "http://localhost:8080/api-0.1.0/signupsheets/1/parent"     },     "user": {       "href": "http://localhost:8080/api-0.1.0/signupsheets/1/user"     }    } } 

super! pretty expected. call service using spring's resttemplate , here's i'm stuck. when marshals signupsheet object pulls in of object, id field null (which makes sense, because there no id field in json, self reference) , onetomany , manytoone object null (i assume same reason).

my question is: how tell either spring hateoas add id json or tell jackson how marshal id id field? furthermore how links? should not marshaling jpa entity , instead create pojo signupsheet (something i'd avoid duplication purposes talked if it's necessary/desirable reason i'm missing). have jackson2halmodule added objectmapper seems make no difference whether it's there or not.

@bean @primary public objectmapper objectmapper() {     objectmapper o = new objectmapper();     o.registermodule(new jackson2halmodule());     return o; } 

thanks in advance help!

=======================================================

solution:

first step, read manual :)

so found out need extend resourcesupport on newly created dtos. done , done. getting no links back! seems needed add jackson2halmodule object mapper on resttemplate this:

    objectmapper o = new objectmapper();     o.registermodule(new jackson2halmodule());     mappingjackson2httpmessageconverter c = new mappingjackson2httpmessageconverter();     c.setobjectmapper(o);     resttemplate.getmessageconverters().add(0, c); 

so figure i'll extend off of resttemplate , @component , should hateoas resource.

i don't think should trying deserialise json jpa entity. jpa entity closely tied application's database , should considered implementation detail of server. instead, i'd recommend mapping type that's modelled on rest api, not on structure of database , usage of jpa.

you're using spring data rest embraces hypermedia. means clients should use uris identify resources , links navigate between them. example, on client side, sign sheet has id; it's href of self link in response. therefore, there's no need expose id jpa entity. indeed, doing expose implementation detail of application clients need not know about.

rather trying populate of properties in response, spring data rest provides links instead. example, access sign sheet's parent, should extract href of parent link response , perform get request on uri.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -