我有一个非常基本的系统,使用Spring和HATEOAS,我发现了一个问题。我有两个非常基本的实体--汽车,和一个人。Getter和setter避免了使问题更加可读性。
@Entity
public class Car implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long carId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="personId")
private Person owner;
private String color;
private String brand;
}
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long personId;
private String firstName;
private String lastName;
@OneToMany(mappedBy="owner")
private List<Car> cars;
}这是我的宝库:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
@RepositoryRestResource(collectionResourceRel = "cars", path = "cars")
public interface CarRepository extends PagingAndSortingRepository<Car, Long> {
List<Person> findByBrand(@Param("brand") String name);
}我可以创建和查询他们,但一些参考链接被打破。例如,有几个帖子成功地创建了两个相关实体:
http://localhost:8080/people
{ "firstName" : "Frodo", "lastName" : "Baggins"}
http://localhost:8080/cars
{ "color":"black","brand":"volvo", "owner":"http://localhost:8080/people/1"}以下是他们的回复:
http://localhost:8080/cars/2
{
color: "black2",
brand: "volvo2",
_links: {
self: {
href: "http://localhost:8080/cars/2"
},
owner: {
href: "http://localhost:8080/cars/2/owner"
}
}
}
http://localhost:8080/people/1
{
firstName: "Frodo",
lastName: "Baggins",
_links: {
self: {
href: "http://localhost:8080/people/1"
},
cars: {
href: "http://localhost:8080/people/1/cars"
}
}
}但我不知道为什么车主在车里有这个网址:
http://localhost:8080/cars/2/owner实际上不起作用。
对此有什么帮助吗?
发布于 2014-08-05 21:26:19
它之所以存在,是因为这就是HATEOAS的意义所在,它用链接来表示实体/资源关系。
我不知道为什么不管用。我猜它可能不起作用,因为车主在检索汽车资源时没有被急切地抓取。
您可以按照https://stackoverflow.com/a/24660635/442773上的步骤定制生成哪些链接
https://stackoverflow.com/questions/25143193
复制相似问题