我有一个非常基本的系统使用Spring和HATEOAS,我发现了一个问题。我有两个非常基本的实体汽车和一个人。避免吸气者和闯入者使问题更具可读性。
@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);
}
我可以创建和查询它们,但有些引用链接已被破坏。例如,一些POST成功创建了两个相关实体:
http://localhost:8080/people
{ "firstName" : "Frodo", "lastName" : "Baggins"}
http://localhost:8080/cars
{ "color":"black","brand":"volvo", "owner":"http://localhost:8080/people/1"}
这是对他们的GET回复:
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
实际上不起作用。
对此有何帮助?
答案 0 :(得分:1)
它就在那里,因为这就是HATEOAS的意义所在,用链接表示实体/资源关系。
我不确定为什么它不起作用。我猜它可能不起作用,因为在检索汽车资源时并不急切地获取所有者。
中的步骤自定义生成的链接