【问题标题】:Create relation / link one to one between 2 entities using spring data rest使用弹簧数据休息在2个实体之间创建关系/一对一链接
【发布时间】:2020-02-24 08:48:43
【问题描述】:

按照本教程https://www.baeldung.com/spring-data-rest-relationships,我正在尝试通过rest put调用在2个实体之间复制链接创建。

但是,当我尝试链接地址/1 和库/1/libraryAddress 时,我收到以下错误消息 curl -i -X PUT -d "http://localhost:8080/addresses/1" -H "Content-Type:text/uri-list" http://localhost:8080/libraries/1/libraryAddress

“必须仅发送 1 个链接来更新不是列表或地图的属性引用”

详情:

// Data model
// Master library class which have one address
@Entity
public class Library {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String name;

    @OneToOne
    @JoinColumn(name = "address_id")
    @RestResource(path = "libraryAddress", rel="address")
    private Address address;

    // standard constructor, getters, setters
}

// Address linked with a onetoone relation with library
@Entity
public class Address {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String location;

    @OneToOne(mappedBy = "address")
    private Library library;

    // standard constructor, getters, setters
}

// Repositories
public interface LibraryRepository extends CrudRepository<Library, Long> {}
public interface AddressRepository extends CrudRepository<Address, Long> {}

使用其余 api 进行的查询:

创建一个库

curl -i -X POST -H "Content-Type:application/json"
  -d '{"name":"My Library"}' http://localhost:8080/libraries

创建地址

curl -i -X POST -H "Content-Type:application/json"
  -d '{"location":"Main Street nr 5"}' http://localhost:8080/addresses

创建关联

curl -i -X PUT -d "http://localhost:8080/addresses/1"
  -H "Content-Type:text/uri-list" http://localhost:8080/libraries/1/libraryAddress

--> 错误 {"cause":null,"message":"必须仅发送 1 个链接来更新不是 List 或 Map 的属性引用。"}

你有解决这个问题的线索吗?

问候, 模糊。

【问题讨论】:

  • 我使用的是 spring-boot 2.2.0.RELEASE
  • 您是否尝试过从 GitHub 下载项目源代码并运行它?我刚刚做了,对你来说失败的请求按照文章中的描述工作。我注意到下载的示例项目使用 Spring Boot 2.1.7,物有所值。
  • 我要试一试!还有一个特点是我为其他测试嵌入了 spring-boot-starter-hateoas 库。也许有冲突。
  • Spring Boot 2.1.7 工作正常,我只是测试一下。

标签: rest spring-boot jpa spring-data-rest hateoas


【解决方案1】:

这实际上是 Spring Boot 2.2.0 org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController 321 行中的一个错误:

if (source.getLinks().hasSingleLink()) {
        throw new IllegalArgumentException(
            "Must send only 1 link to update a property reference that isn't a List or a Map.");
}

应该是:

if (!source.getLinks().hasSingleLink()) {
        throw new IllegalArgumentException(
            "Must send only 1 link to update a property reference that isn't a List or a Map.");
}

这是 String Boot 2.1.7 的代码

if (source.getLinks().size() != 1) {
        throw new IllegalArgumentException(
            "Must send only 1 link to update a property reference that isn't a List or a Map.");
}

【讨论】:

    【解决方案2】:

    此类订单符合 2.1.10.BUILD-SNAPSHOT 但不符合 spring-boot 2.2.0.RELEASE

    此版本可能有所改变。我会看的。不过感谢您指出这一点。

    有人已经设法使用 2.2.0 运行这种休息呼叫?

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多