Java 如何在SpringDataREST应用程序中创建实体之间的引用

Java 如何在SpringDataREST应用程序中创建实体之间的引用,java,spring,spring-boot,spring-data-jpa,spring-data-rest,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data Rest,我正在尝试用Spring Boot+DataREST+JPA构建简单的应用程序。 具有一对多关系的类别和账簿实体: <!-- language-all: java --> @Entity public class Category { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @OneToMany(

我正在尝试用Spring Boot+DataREST+JPA构建简单的应用程序。
具有一对多关系的类别和账簿实体:

<!-- language-all: java -->    
@Entity
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
    private Set<Book> books;

    ...getters & setters next...
}
每个实体的简单存储库

@RepositoryRestResource
public interface BookRepository extends JpaRepository<Book, Long> {}

@RepositoryRestResource
public interface CategoryRepository extends JpaRepository<Category, Long> {}
应用程序成功启动,我可以创建书籍和类别
Q.:如何创建和删除它们之间的引用

我尝试了下面描述的解决方案: -对我不起作用:在带有“ContentType:text/uri列表”标题的PUT请求中,我有响应代码204,数据库中没有任何更改。深入查看,我在日志中发现以下调试消息:

s.w.s.m.m.a.RequestMappingHandlerMapping : 
Did not find handler method for [/categories/1/books]
此url仅适用于GET请求

Q.:你知道我的配置有什么问题吗


谢谢。

要创建图书(id:1)和类别(id:1)之间的关系,请执行以下操作:

  • 提出要求,
  • 媒体类型:文本/uri列表
  • 数据:
  • 请求
卷曲示例:

curl -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/categories/1" http://localhost:8080/books/1/category
curl -X DELETE http://localhost:8080/books/1/category
要删除此关系,只需对同一地址执行删除请求

卷曲示例:

curl -X PUT -H "Content-Type: text/uri-list" -d "http://localhost:8080/categories/1" http://localhost:8080/books/1/category
curl -X DELETE http://localhost:8080/books/1/category

还要回答您的第二个问题:您的配置看起来不错,我已经在您的代码上测试了这个示例。

这个解决方案对我很有效,谢谢!有可能一次把几本书添加到一个类别中吗?我已经尝试过撤销这个put(或post)请求,但没有成功。我没有例外,我的行为与你发现的bug描述相符。