java - How to consume Spring HATEOAS REST resources containing links to another resources? -


i have /studentcourses endpoint on server (built spring data rest) returns following content:

{   "_embedded" : {     "studentcourses" : [       {         "uid" : "5f23abe9-b24e-4e76-86b0-d539950a0a41",         "registrationdate" : "7/23/2016",         "_links" : {           "self" : {             "href" : "http://localhost:8080/studentcourses/5f23abe9-b24e-4e76-86b0-d539950a0a41"           },           "studentcourse" : {             "href" : "http://localhost:8080/studentcourses/5f23abe9-b24e-4e76-86b0-d539950a0a41"           },           "course" : {             "href" : "http://localhost:8080/studentcourses/5f23abe9-b24e-4e76-86b0-d539950a0a41/course"           },           "student" : {             "href" : "http://localhost:8080/studentcourses/5f23abe9-b24e-4e76-86b0-d539950a0a41/student"           }         }       },       {         ...       },       ...     ]   },   "_links" : {     "self" : {       "href" : "http://localhost:8080/studentcourses"     },     "profile" : {       "href" : "http://localhost:8080/profile/studentcourses"     }   },   "page" : {     ...   } } 

and following client code:

class studentcoursedto {      string uuid;      string registrationdate;      studentdto student; // contains uuid, firstname, lastname, etc.      coursedto course; // contains uuid, name, etc.      // getters, setters  }  resttemplate resttemplate() {     objectmapper objectmapper = new objectmapper();     objectmapper.registermodule(new jackson2halmodule());     objectmapper.configure(deserializationfeature.fail_on_unknown_properties, false);     mappingjackson2httpmessageconverter messageconverter =             new mappingjackson2httpmessageconverter();     messageconverter.setobjectmapper(objectmapper);     messageconverter.setsupportedmediatypes(arrays.aslist(mediatypes.hal_json));     return new resttemplate(arrays.aslist(messageconverter)); }  ...  collection<studentcoursedto> studentcourses = resttemplate().exchange(         "http://localhost:8080/studentcourses",         httpmethod.get, null,         new parameterizedtypereference<pagedresources<studentcoursedto>>() {})         .getbody().getcontent(); 

the problem studentcoursedto.student , studentcoursedto.course null, studentcoursedto.uuid , studentcoursedto.registrationdate retrieved correctly server.
has idea have missed?
think there must someway tell resttemplate automatically follow links in returned content student , course in example above, haven't found way this.

just because there links not mean automatically followed.

i change studentcoursedto to:

class studentcoursedto {      string uuid;      string registrationdate;  } 

and deserialize response

pagedresources<resource<studentcoursedto>> studentcourses = resttemplate().exchange(         "http://localhost:8080/studentcourses",         httpmethod.get, null,         new parameterizedtypereference<pagedresources<resource<studentcoursedto>>>() {})         .getbody().getcontent(); 

for each resource<studentcoursedto> can follow links studentand course, e.g. using resttemplate retrieve resources.

of course gives 2 additional calls per response item - way avoid change service embed information in list resource.


Comments