2017-08-13 111 views
0

我使用spring data rest並要發佈一個新的資源和他們及其子resource.Both是新的資源,所以沒有鏈接指向任them.In事實上,以下JSON是一個合理的json,我認爲要發佈新資源及其子資源。春天數據休息後子資源

這裏是一個JSON代表一個新來的學生。

{ 
    "name": "John", 
    "sex": "man", 
    "details": [ 
    { 
     "detailKey": "weight", 
     "detailValue": 130 
    }, 
    { 
     "detailKey": "height", 
     "detailValue": 175 
    }, 
    ] 
} 

這個學生的details是子資源,並在數據庫中不存在。

我如何張貼約翰和他的details在同一時間(在一個JSON或在一個請求),或者有一個更正確的方法來做到這一點?

PS: Student

@Entity 
@Table(name = "student", catalog = "test") 
public class Student implements java.io.Serializable { 

    // Fields 

    private Integer id; 
    private String name; 
    private String sex; 

    // Constructors 

    /** default constructor */ 
    public Student() { 
    } 

    /** full constructor */ 
    public Student(String name, String sex) { 
     this.name = name; 
     this.sex = sex; 
    } 

    // Property accessors 
    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    public Integer getId() { 
     return this.id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    @Column(name = "name") 
    public String getName() { 
     return this.name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Column(name = "sex") 
    public String getSex() { 
     return this.sex; 
    } 

    public void setSex(String sex) { 
     this.sex = sex; 
    } 

} 

StudentDetails

@Entity 
@Table(name = "student_details", catalog = "test") 
public class StudentDetails implements java.io.Serializable { 

    // Fields 

    private Integer id; 
    private Integer studentId; 
    private String detailCode; 
    private String detailValue; 

    // Constructors 

    /** default constructor */ 
    public StudentDetails() { 
    } 

    /** full constructor */ 
    public StudentDetails(Integer studentId, String detailCode, 
      String detailValue) { 
     this.studentId = studentId; 
     this.detailCode = detailCode; 
     this.detailValue = detailValue; 
    } 

    // Property accessors 
    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    public Integer getId() { 
     return this.id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    @Column(name = "student_id") 
    public Integer getStudentId() { 
     return this.studentId; 
    } 

    public void setStudentId(Integer studentId) { 
     this.studentId = studentId; 
    } 

    @Column(name = "detail_code") 
    public String getDetailCode() { 
     return this.detailCode; 
    } 

    public void setDetailCode(String detailCode) { 
     this.detailCode = detailCode; 
    } 

    @Column(name = "detail_value") 
    public String getDetailValue() { 
     return this.detailValue; 
    } 

    public void setDetailValue(String detailValue) { 
     this.detailValue = detailValue; 
    } 

} 

StudentDetails用於定製學生的信息。

兩個ID是自動遞增的,我要發佈一個新的學生和他的細節信息在同一個請求(我認爲這是友好的用戶)

+0

你可以分享你的POJO和你的休息回購? ! –

回答

0

春天沒有一流的支持對於子資源(JAX-RS)。您將不得不從客戶端執行2個呼叫,或者在服務器上處理它。從你的問題不清楚如何details可以是一個資源;它沒有ID。體重和身高並不能唯一識別一個人。看來你的設計是錯誤的,或者你需要提供更多的信息。

+0

感謝您的回答兩個電話將是一個妥善的解決辦法,但我很好奇,如何處理它的server.According到[春季數據REST - 參考文獻](https://docs.spring.io/spring-data/REST /文檔/電流/參考/ HTML),我所知道的唯一方法是關閉保存方法寫一個'StudentController'它處理POST和補丁request.Do你有類似的代碼發佈資源,在一個新的子資源要求 – john

+0

@john提取一些字段作爲學生的詳細信息有什麼意義?我沒有看到任何重用。 –

+0

其實,這是一個測試演示,但我認爲這是meaningful.Sometimes有一些信息可以用鍵值。因此定製,用戶可以添加更多的信息,如重量:130和高度:175 – john