讓我們假設我有以下實體:春數據休息投影嵌入式實體
@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();
}
我試圖創建下面的JSON表示,所以,當我使用http://localhost:8080/api/registrations?projection=minimal
我不需要所有的用戶數據來沿着:
{
"_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}",
}
}
}
}
但是我已經制作的投影我得到一個異常(它的工作原理沒有getUserName()語句很顯然,我已經定義的方式是錯誤的界面...但它是做正確的方法像這樣?
編輯:異常是以下
Invalid property 'userName' of bean class [com.test.Registration]:
Could not find field for property during fallback access! (through reference chain: org.springframework.hateoas.PagedResources["_embedded"]
->java.util.UnmodifiableMap["registrations"]->java.util.ArrayList[0]->org.springframework.data.rest.webmvc.json.["content"]
->$Proxy119["userName"])</div></body></html>
如果你分享你得到的異常,可能會有幫助嗎? –