为了账号安全,请及时绑定邮箱和手机立即绑定

在Spring Data REST中发布@OneToMany子资源关联

在Spring Data REST中发布@OneToMany子资源关联

守候你守候我 2019-09-19 16:29:57
目前我有一个使用Spring Data REST的Spring Boot应用程序。我有一个与另一个域实体Post有@OneToMany关系的域实体Comment。这些类的结构如下:Post.java:@Entitypublic class Post {    @Id    @GeneratedValue    private long id;    private String author;    private String content;    private String title;    @OneToMany    private List<Comment> comments;    // Standard getters and setters...}Comment.java:@Entitypublic class Comment {    @Id    @GeneratedValue    private long id;    private String author;    private String content;    @ManyToOne    private Post post;    // Standard getters and setters...}他们的Spring Data REST JPA存储库是以下基本实现CrudRepository:PostRepository.java:public interface PostRepository extends CrudRepository<Post, Long> { }CommentRepository.java:public interface CommentRepository extends CrudRepository<Comment, Long> { }应用程序入口点是标准的简单Spring Boot应用程序。一切都是配置库存。Application.java@Configuration@EnableJpaRepositories@Import(RepositoryRestMvcConfiguration.class)@EnableAutoConfigurationpublic class Application {    public static void main(final String[] args) {        SpringApplication.run(Application.class, args);    }}一切似乎都正常。当我运行应用程序时,一切似乎都正常工作。我可以POST一个新的Post对象http://localhost:8080/posts:身体:  {"author":"testAuthor", "title":"test", "content":"hello world"}结果http://localhost:8080/posts/1:{    "author": "testAuthor",    "content": "hello world",    "title": "test",    "_links": {        "self": {            "href": "http://localhost:8080/posts/1"        },        "comments": {            "href": "http://localhost:8080/posts/1/comments"        }    }}但是,当我执行GET时,http://localhost:8080/posts/1/comments我得到一个空对象{}返回,如果我尝试将注释POST到同一个URI,我得到一个HTTP 405方法不允许。创建Comment资源并将其与此关联的正确方法是什么Post?http://localhost:8080/comments如果可能的话,我想避免直接POST 。
查看完整描述

3 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

您必须先发布评论,在发布评论时,您可以创建一个关联发布实体。


它应该如下所示:


http://{server:port}/comment METHOD:POST


{"author":"abc","content":"PQROHSFHFSHOFSHOSF", "post":"http://{server:port}/post/1"}

它会完美地运作。


查看完整回答
反对 回复 2019-09-19
?
富国沪深

TA贡献1790条经验 获得超9个赞

假设您已经发现了post URI并因此发现了关联资源的URI(被认为是$association_uri在下面),它通常采取以下步骤:


发现管理评论的馆藏资源:


curl -X GET http://localhost:8080


200 OK

{ _links : {

    comments : { href : "…" },

    posts :  { href : "…" }

  }

}

按照comments链接和POST您的数据到资源:


curl -X POST -H "Content-Type: application/json" $url 

{ … // your payload // … }


201 Created

Location: $comment_url

通过向PUT关联URI 发出a 来将评论分配给帖子。


curl -X PUT -H "Content-Type: text/uri-list" $association_url

$comment_url


204 No Content

请注意,在最后一步中,根据规范text/uri-list,您可以提交多个URI,用于标识由换行符分隔的注释,以便一次分配多个注释。


关于一般设计决策的一些注释。阿交/评论例如通常是聚集体,这意味着我会避免从背面参考一个很好的例子Comment的Post,并且还避免了CommentRepository完全。如果注释没有自己的生命周期(它们通常不是在组合风格的关系中),你宁可直接内联呈现注释,而是添加和删除注释的整个过程可以通过使用来处理JSON补丁。Spring Data REST 在即将发布的2.2版本的最新候选版本中增加了对该功能的支持。


查看完整回答
反对 回复 2019-09-19
?
白板的微信

TA贡献1883条经验 获得超3个赞

映射关联和组合有两种类型。在关联的情况下,我们使用连接表概念


员工 - 1到n->部门


因此,如果是Association Employee,Department,Employee_Department,将创建3个表


您只需要在代码中创建EmployeeRepository。除此之外,映射应该是这样的:


class EmployeeEntity{


@OnetoMany(CascadeType.ALL)

   private List<Department> depts {


   }


}

Depatment Entity不会包含forign key的任何mappping ...所以现在当你尝试在单个json请求中添加Employee with Department的POST请求时,它将被添加....


查看完整回答
反对 回复 2019-09-19
  • 3 回答
  • 0 关注
  • 997 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信