Spring数据rest不正确的资源href
我在使用SpringDataRESTwithSpringDataJPA获取正确的url以显示self-href时遇到问题 所以我有一个学生班:Spring数据rest不正确的资源href,spring,spring-data,spring-data-jpa,spring-data-rest,spring-hateoas,Spring,Spring Data,Spring Data Jpa,Spring Data Rest,Spring Hateoas,我在使用SpringDataRESTwithSpringDataJPA获取正确的url以显示self-href时遇到问题 所以我有一个学生班: @Entity @Table(name="student", schema="main") public class Student { @Id private Long id; ... ... @OneToOne @JoinColumn(name = "id") private Studen
@Entity
@Table(name="student", schema="main")
public class Student {
@Id
private Long id;
...
...
@OneToOne
@JoinColumn(name = "id")
private StudentInformation studentInformation;
}
具有相应的存储库文件
@RepositoryRestResource(collectionResourceRel = "students", path = "students")
public interface StudentRepository extends PagingAndSortingRepository<Student, Long> {
}
(省略了其他属性/getter/setter)
具有相应的存储库
@RepositoryRestResource(collectionResourceRel = "studentinformation", path = "students/studentinformation")
public interface StudentInformationRepository extends CrudRepository<StudentInformation, Long> {
}
除非我遵循从学生到学生信息的链接,否则学生信息的自我链接不正确
{
"id": 1,
...
...
"_links": {
"self": {
"href": "http://localhost:8080/students/studentinformation/1"
},
"studentinformation": {
"href": "http://localhost:8080/students/studentinformation/1"
}
}
}
我该如何阅读该链接
“href”:”
而不是
“href”:”
谢谢首先,像
学生/studentinformation
这样的多段路径可能不起作用,因为它不受支持,请参见,因此不要专注于设计URL。如果您确实需要http://localhost:8080/students/1/studentinformation
representation-您需要为此定义自定义控制器,而不需要依靠弹簧数据支架
其次,在/students
端点上使用不同的学生数据不是更好吗?如果这只是学生
资源的不同表示形式,我会使用投影,例如学生/1?投影=最小
和学生/1?投影=完整
如果
studentinformation
包含与students
完全不同的数据,并且它不是Student
资源的表示,只需为/studentinformation
定义一个端点,感谢您的响应。那么您是说没有办法在self-href URL中堆叠资源?比如,http://localhost:8080/RESOURCE/{id}/SUB_RESOURCE
使用@RepositoryRestResource?我必须定义我自己的自定义REST控制器?这似乎很奇怪。是的,你可以在我提供的链接中检查,至少在存储库中没有。如果你能找到任何方法,很高兴知道。方法是定义@RepositoryRestController使用自定义实现并将其与资源关联。如果要使用存储库实现,可以执行SUB_resource/{ID}?resource_ID={resource_ID}
{
"id": 1,
...
...
"_links": {
"self": {
"href": "http://localhost:8080/students/1"
},
"student": {
"href": "http://localhost:8080/students/1"
},
"studentinformation": {
"href": "http://localhost:8080/students/1/studentinformation"
}
}
}
{
"id": 1,
...
...
"_links": {
"self": {
"href": "http://localhost:8080/students/studentinformation/1"
},
"studentinformation": {
"href": "http://localhost:8080/students/studentinformation/1"
}
}
}