cca*_*mpo 101 java spring spring-data-jpa spring-data-rest spring-boot
目前我有一个使用Spring Data REST的Spring Boot应用程序.我有一个与另一个域实体Post有@OneToMany关系的域实体Comment.这些类的结构如下:
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...
}
Run Code Online (Sandbox Code Playgroud)
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...
}
Run Code Online (Sandbox Code Playgroud)
他们的Spring Data REST JPA存储库是以下基本实现CrudRepository:
PostRepository.java:
public interface PostRepository extends CrudRepository<Post, Long> { }
Run Code Online (Sandbox Code Playgroud)
CommentRepository.java:
public interface CommentRepository extends CrudRepository<Comment, Long> { }
Run Code Online (Sandbox Code Playgroud)
应用程序入口点是标准的简单Spring Boot应用程序.一切都是配置库存.
Application.java
@Configuration
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
一切似乎都正常.当我运行应用程序时,一切似乎都正常工作.我可以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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我执行GET时,http://localhost:8080/posts/1/comments我得到一个空对象{}返回,如果我尝试将注释POST到同一个URI,我得到一个HTTP 405方法不允许.
创建Comment资源并将其与此关联的正确方法是什么Post?http://localhost:8080/comments如果可能的话,我想避免直接POST .
Oli*_*ohm 53
假设您已经发现了post URI并因此发现了关联资源的URI(被认为是$association_uri在下面),它通常采取以下步骤:
发现管理评论的馆藏资源:
curl -X GET http://localhost:8080
200 OK
{ _links : {
comments : { href : "…" },
posts : { href : "…" }
}
}
Run Code Online (Sandbox Code Playgroud)按照comments链接和POST您的数据到资源:
curl -X POST -H "Content-Type: application/json" $url
{ … // your payload // … }
201 Created
Location: $comment_url
Run Code Online (Sandbox Code Playgroud)通过向PUT关联URI 发出a 来将评论分配给帖子.
curl -X PUT -H "Content-Type: text/uri-list" $association_url
$comment_url
204 No Content
Run Code Online (Sandbox Code Playgroud)请注意,在最后一步中,根据规范text/uri-list,您可以提交多个URI,用于标识由换行符分隔的注释,以便一次分配多个注释.
关于一般设计决策的一些注释.阿交/评论例如通常是聚集体,这意味着我会避免从背面参考一个很好的例子Comment的Post,并且还避免了CommentRepository完全.如果注释没有自己的生命周期(它们通常不是在组合风格的关系中),你宁可直接内联呈现注释,而是添加和删除注释的整个过程可以通过使用来处理JSON补丁.Spring Data REST 在即将发布的2.2版本的最新候选版本中增加了对该功能的支持.
小智 48
您必须先发布评论,在发布评论时,您可以创建一个关联发布实体.
它应该如下所示:
http://{server:port}/comment METHOD:POST
{"author":"abc","content":"PQROHSFHFSHOFSHOSF", "post":"http://{server:port}/post/1"}
Run Code Online (Sandbox Code Playgroud)
它会完美地运作.
| 归档时间: |
|
| 查看次数: |
29000 次 |
| 最近记录: |