简体   繁体   English

Spring Data Rest Post子资源

[英]spring data rest post sub-resource

I am using spring data rest and want to post a new resource and its sub-resource.Both of them are new resource so there is no link pointing to either of them.In fact,the following json is a reasonable json I think to post the new resource and its sub-resource . 我正在使用spring data rest并希望发布一个新资源及其子资源。它们都是新资源,因此没有指向其中任何一个的链接。实际上,以下json是我认为要发布的合理json新资源及其子资源。

here is a json represents one new student. 这是代表一个新学生的json。

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

details of this student is sub-resource and not exist in databases. 该学生的details是子资源,数据库中不存在。

how I post John and his details at the same time(in one json or in one request),or there is a more proper way to do this? 我如何同时(在一个json或一个请求中)同时发布John和他的details ,或者有一种更合适的方法来做到这一点?

PS: Student class 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 class 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 is used to customize student's information. StudentDetails用于自定义学生信息。

both of id is auto increment,and I want to post one new student and his details info in the same request(I think it is friendly to the user) id都是自动递增的,我想在同一请求中发布一名新学生及其详细信息(我认为这对用户很友好)

Spring doesn't have first-class support for sub-resource (JAX-RS does). Spring没有对子资源的一流支持(JAX-RS有)。 You will either have to do 2 calls from the client, or handle it on the server. 您将不得不从客户端进行2次呼叫,或者在服务器上进行处理。 It's not clear from your question how details can be a resource; 从您的问题尚不清楚, details如何成为资源。 it has no id. 它没有ID。 Weight and height don't uniquely identify a person. 体重和身高不能唯一地识别一个人。 It seems your design is wrong, or you need to provide more information. 看来您的设计有误,或者您需要提供更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM