简体   繁体   English

如何使用spring-data-rest来发布新的嵌套实体

[英]How to POST new nested entities using spring-data-rest

just wondering if it is possible to POST new entities within new entities. 只是想知道是否可以在新实体中发布新实体。

Person.java Person.java

@Entity
public class Person {
    @oneToOne(optional = false)
    private Address address;
}

Address.java Address.java

@Entity
public class Address {
    private String street;
}

What I would like to do is create a Person with an Address in one HTTP request. 我想要做的是在一个HTTP请求中创建一个具有地址的Person。 Is this possible with something like the request below? 这可能与下面的请求有关吗?

curl -i -X POST -H "Content-Type: application/json" /
-d '{"address": {"street":"street 1"}}' http://localhost:8080/people

So far my investigation and document searching says no. 到目前为止,我的调查和文件检索说没有。 But thought I would ask here before giving up. 但是我想在放弃之前会问这里。

Thanks. 谢谢。

It works, but in your example you haven't turned on cascading. 它可以工作,但在你的例子中你还没有开启级联。

@OneToOne(optional = false, cascade = CascadeType.ALL)
private Address address;

Furthermore you must not have an exported repository for Address . 此外,您不能拥有Address的导出存储库。 Otherwise Address and Person would be independent resources and had to be treated accordingly, which means separate POSTs. 否则AddressPerson将是独立的资源,必须相应地对待,这意味着单独的POST。

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

相关问题 在嵌套对象中使用具有相同属性名称的Jackson反序列化的Spring-data-rest POST - Spring-data-rest POST using Jackson Deserialization with same property name in nested objects Spring-Data-Rest验证器 - Spring-Data-Rest Validator Curl不适用于HTTP发布到Spring数据休息的RequestMapping - Curl not working for HTTP Post to Spring-data-rest RequestMapping 使用 Spring-Data-Rest,如何从一个 REST 调用更新 OneToOne 关系双方的 FK? - Using Spring-Data-Rest, how to update FK on both sides of OneToOne relationship from one REST call? 如何将 spring-data-rest 与 spring websocket 混合到一个实现中 - How to mix spring-data-rest with spring websocket into a single implementation 使用spring-data-rest返回所有结果 - returning all result using spring-data-rest 如何使用Spring-data-rest替换ResourceAssemblerSupport - How to use Spring-data-rest for replacing ResourceAssemblerSupport 如何使用IntelliJ,maven和spring-data-rest处理多个子项目? - How to work with multiple sub-projects using IntelliJ, maven and spring-data-rest? 如何使用spring-data-rest和MockMvc创建用于集成测试的JSON - How to create JSON for integration tests with spring-data-rest and MockMvc 如何在spring-data-rest中防止relTargetType的json输出? - How to prevent json output of relTargetType in spring-data-rest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM