亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在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 關注
  • 1105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號