假设我有以下实体:
我正试图创建以下JSON表示形式,这样当我使用
@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>