java - Spring Data Rest projection for Embedded Entities -


lets assume have following entities:

@entity public class registration {      @manytoone     private student student;     //getters, setters }  @entity public class student {      private string id;     private string username;     private string name;     private string surname;     //getters, setters }  @projection(name="minimal", types = {registration.class, student.class}) public interface registrationprojection {      string getusername();     student getstudent();  } 

i'm trying create following json representation, when use http://localhost:8080/api/registrations?projection=minimal dont need user data come along:

{   "_links": {     "self": {       "href": "http://localhost:8080/api/registrations{?page,size,sort,projection}",     }   },   "_embedded": {     "registrations": [       {         "student": {           "username": "user1"         }       }     ],     "_links": {       "self": {         "href": "http://localhost:8080/api/registrations/1{?projection}",       }     }   } } 

however projection have created exception(it works without getusername() statement. have defined interface in wrong way...but correct way this?

edit: exception following

invalid property &#39;username&#39; of bean class [com.test.registration]:  not find field property during fallback access! (through reference chain: org.springframework.hateoas.pagedresources[&quot;_embedded&quot;] -&gt;java.util.unmodifiablemap[&quot;registrations&quot;]-&gt;java.util.arraylist[0]-&gt;org.springframework.data.rest.webmvc.json.[&quot;content&quot;] -&gt;$proxy119[&quot;username&quot;])</div></body></html> 

as exception said username not member of registration entity. why failed. think understand that.

first @ if want expose projection registration should first change following line:

@projection(name="minimal", types = {registration.class, student.class}) 

by setting types = {registration.class, student.class} asked spring data rest apply projection on registration.class , student.class. , can cause issue because depending of type should not have same member/methods. in practice types must share common ancestor.

otherwise main problem should try virtual projections following thing (i didn't try on sample should work, hope):

@projection(name="minimal", types = {registration.class}) public interface registrationprojection {      @value("#{target.getstudent().getusername()}")     string getusername(); } 

i don't know if familiar spel previous code create getusername this.getstudent().getusername() because target bound on instance object (see documentation).


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -