在Spring Data REST中发布@OneToMany子资源关联 [英] POSTing a @OneToMany sub-resource association in Spring Data REST

查看:292
本文介绍了在Spring Data REST中发布@OneToMany子资源关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个使用Spring Data REST的Spring Boot应用程序。我有一个域实体 Post ,它与另一个域实体的 @OneToMany 关系, Comment 。这些类的结构如下:

Currently I have a Spring Boot application using Spring Data REST. I have a domain entity Post which has the @OneToMany relationship to another domain entity, Comment. These classes are structured as follows:

Post.java:

Post.java:

@Entity
public 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:

Comment.java:

@Entity
public 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的基本实现

Their Spring Data REST JPA repositories are basic implementations of CrudRepository:

PostRepository.java:

PostRepository.java:

public interface PostRepository extends CrudRepository<Post, Long> { }

CommentRepository.java:

CommentRepository.java:

public interface CommentRepository extends CrudRepository<Comment, Long> { }

应用程序入口点是标准的简单Spring Boot应用程序。一切都是配置库存。

The application entry point is a standard, simple Spring Boot application. Everything is configured stock.

Application.java

Application.java

@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

一切似乎都能正常工作。当我运行应用程序时,一切似乎都正常工作。我可以将一个新的Post对象POST到 http:// localhost:8080 / posts ,如下所示:

Everything appears to work correctly. When I run the application, everything appears to work correctly. I can POST a new Post object to http://localhost:8080/posts like so:

正文:
{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"
        }
    }
}

然而,当我在 http:// localhost:8080 / posts / 1 / comments 我得到一个空对象 {} 返回,如果我尝试将注释POST到同一个URI,我得到一个不允许的HTTP 405方法。

However, when I perform a GET at http://localhost:8080/posts/1/comments I get an empty object {} returned, and if I try to POST a comment to the same URI, I get an HTTP 405 Method Not Allowed.

创建评论资源并将其与此 P关联的正确方法是什么? OST ?如果可能的话,我想避免直接POST到 http:// localhost:8080 / comments

What is the correct way to create a Comment resource and associate it with this Post? I'd like to avoid POSTing directly to http://localhost:8080/comments if possible.

推荐答案

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

You have to post the comment first and while posting the comment you can create an association posts entity.

它应该如下所示:

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

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

它会完全正常。

这篇关于在Spring Data REST中发布@OneToMany子资源关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆