【发布时间】:2019-08-29 11:23:13
【问题描述】:
假设我有两个资源Person 和Article
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long person_id;
private String firstName;
private String lastName;
@OneToMany(mappedBy="person", cascade=CascadeType.ALL)
private List<Article> articles = new ArrayList<>();
}
@Entity
@Table(name="article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String details;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="person_id")
private Person person;
}
我现在想将 HATEOAS 支持添加到我正在使用的控制器的响应中 org.springframework.hateoas.ResourceAssembler
public class PersonResourceAssembler implements ResourceAssembler<Person, Resource<Person>> {
private EntityLinks entityLinks;
public UserJobResourceAssembler(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<Person> toResource(Person entity) {
Resource<UserJob> resource = new Resource<>(entity);
resource.add(
entityLinks.linkFor(Person.class).withSelfRel()),
entityLinks.linkFor(...logic...).withRel("articles")) //here I am hardcoding the relation link name i.e "article"
);
return resource;
}
}
所以,在上面的代码中,“article”是硬编码的链接名称,但我不想这样做。我希望它以Spring-Data-REST 处理它的方式进行,即对于每个关系,它会自动检测Entity 类中使用的变量的名称,例如articles 将从Person 中选择,person 将从中选择Article.
我不知道Spring-Data-REST 是如何处理它的,但是有没有现成/定制的解决方案来满足这个要求?
【问题讨论】:
标签: spring-boot spring-data spring-data-rest spring-hateoas