【发布时间】:2017-03-18 18:05:00
【问题描述】:
我在使用 spring-data rest 和 spring-data jpa 获得正确的 url 以显示 self href 时遇到问题。
所以我有一个学生类:
@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> {
}
还有一个 StudentInformation 类
@Entity
@Table(name="studentinformation", schema="main")
public class StudentInformation {
@Id
private Long id;
...
...
}
(省略其他属性/getter/setter)
有相应的仓库
@RepositoryRestResource(collectionResourceRel = "studentinformation", path = "students/studentinformation")
public interface StudentInformationRepository extends CrudRepository<StudentInformation, Long> {
}
当我通过 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"
}
}
}
除了当我点击从学生到 studentInformation 的链接时,studentInformation 的自我链接不正确。
{
"id": 1,
...
...
"_links": {
"self": {
"href": "http://localhost:8080/students/studentinformation/1"
},
"studentinformation": {
"href": "http://localhost:8080/students/studentinformation/1"
}
}
}
如何让该链接阅读 "href": "http://localhost:8080/students/1/studentinformation 代替 "href": "http://localhost:8080/students/studentinformation/1"
谢谢
【问题讨论】:
标签: spring spring-data spring-data-jpa spring-data-rest spring-hateoas