第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在Spring Data REST中發(fā)布@OneToMany子資源關(guān)聯(lián)

在Spring Data REST中發(fā)布@OneToMany子資源關(guān)聯(lián)

守候你守候我 2019-09-19 16:29:57
目前我有一個(gè)使用Spring Data REST的Spring Boot應(yīng)用程序。我有一個(gè)與另一個(gè)域?qū)嶓wPost有@OneToMany關(guān)系的域?qū)嶓wComment。這些類的結(jié)構(gòu)如下: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存儲(chǔ)庫是以下基本實(shí)現(xiàn)CrudRepository:PostRepository.java:public interface PostRepository extends CrudRepository<Post, Long> { }CommentRepository.java:public interface CommentRepository extends CrudRepository<Comment, Long> { }應(yīng)用程序入口點(diǎn)是標(biāo)準(zhǔn)的簡單Spring Boot應(yīng)用程序。一切都是配置庫存。Application.java@Configuration@EnableJpaRepositories@Import(RepositoryRestMvcConfiguration.class)@EnableAutoConfigurationpublic class Application {    public static void main(final String[] args) {        SpringApplication.run(Application.class, args);    }}一切似乎都正常。當(dāng)我運(yùn)行應(yīng)用程序時(shí),一切似乎都正常工作。我可以POST一個(gè)新的Post對象http://localhost:8080/posts:身體:  {"author":"testAuthor", "title":"test", "content":"hello world"}結(jié)果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"        }    }}但是,當(dāng)我執(zhí)行GET時(shí),http://localhost:8080/posts/1/comments我得到一個(gè)空對象{}返回,如果我嘗試將注釋POST到同一個(gè)URI,我得到一個(gè)HTTP 405方法不允許。創(chuàng)建Comment資源并將其與此關(guān)聯(lián)的正確方法是什么Post?http://localhost:8080/comments如果可能的話,我想避免直接POST 。
查看完整描述

3 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

您必須先發(fā)布評論,在發(fā)布評論時(shí),您可以創(chuàng)建一個(gè)關(guān)聯(lián)發(fā)布實(shí)體。


它應(yīng)該如下所示:


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


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

它會(huì)完美地運(yùn)作。


查看完整回答
反對 回復(fù) 2019-09-19
?
富國滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

假設(shè)您已經(jīng)發(fā)現(xiàn)了post URI并因此發(fā)現(xiàn)了關(guān)聯(lián)資源的URI(被認(rèn)為是$association_uri在下面),它通常采取以下步驟:


發(fā)現(xiàn)管理評論的館藏資源:


curl -X GET http://localhost:8080


200 OK

{ _links : {

    comments : { href : "…" },

    posts :  { href : "…" }

  }

}

按照comments鏈接和POST您的數(shù)據(jù)到資源:


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

{ … // your payload // … }


201 Created

Location: $comment_url

通過向PUT關(guān)聯(lián)URI 發(fā)出a 來將評論分配給帖子。


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

$comment_url


204 No Content

請注意,在最后一步中,根據(jù)規(guī)范text/uri-list,您可以提交多個(gè)URI,用于標(biāo)識(shí)由換行符分隔的注釋,以便一次分配多個(gè)注釋。


關(guān)于一般設(shè)計(jì)決策的一些注釋。阿交/評論例如通常是聚集體,這意味著我會(huì)避免從背面參考一個(gè)很好的例子Comment的Post,并且還避免了CommentRepository完全。如果注釋沒有自己的生命周期(它們通常不是在組合風(fēng)格的關(guān)系中),你寧可直接內(nèi)聯(lián)呈現(xiàn)注釋,而是添加和刪除注釋的整個(gè)過程可以通過使用來處理JSON補(bǔ)丁。Spring Data REST 在即將發(fā)布的2.2版本的最新候選版本中增加了對該功能的支持。


查看完整回答
反對 回復(fù) 2019-09-19
?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

映射關(guān)聯(lián)和組合有兩種類型。在關(guān)聯(lián)的情況下,我們使用連接表概念


員工 - 1到n->部門


因此,如果是Association Employee,Department,Employee_Department,將創(chuàng)建3個(gè)表


您只需要在代碼中創(chuàng)建EmployeeRepository。除此之外,映射應(yīng)該是這樣的:


class EmployeeEntity{


@OnetoMany(CascadeType.ALL)

   private List<Department> depts {


   }


}

Depatment Entity不會(huì)包含forign key的任何mappping ...所以現(xiàn)在當(dāng)你嘗試在單個(gè)json請求中添加Employee with Department的POST請求時(shí),它將被添加....


查看完整回答
反對 回復(fù) 2019-09-19
  • 3 回答
  • 0 關(guān)注
  • 1099 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)