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

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

问题描述

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

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:

@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...
}

评论.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.

应用程序.java

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

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

似乎一切正常.当我运行该应用程序时,一切似乎都正常工作.我可以像这样向 http://localhost:8080/posts POST 一个新的 Post 对象:

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 执行 GET 时,我得到一个空对象 {} 返回,如果我尝试向同一个 URI 发布评论,我得到一个 HTTP 405 Method Not Allowed.

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.

创建Comment 资源并将其与此Post 关联的正确方法是什么?如果可能,我想避免直接 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天全站免登陆