遵循本教程https://www.baeldung.com/spring-data-rest-relationships我试图用一个rest-put调用在两个实体之间重现一次链接创建。
然而,当我尝试链接地址/1&library/1/libraryAddresscurl-i-X PUT-d"http://localhost:8080/addresses/1"-H"内容类型:text/uri列表"http://localhost:8080/libraries/1/libraryAddress
"必须只发送一个链接来更新不是列表或地图的属性引用">
详细信息:
// 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> {}
使用rest 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.0org.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.");
}
这是字符串引导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.1.10.BUILD-SNAPSHOT,但不符合2.2.0.RELEASE
在这个版本中,情况可能发生了变化。我会看看的。不过,谢谢你指出这一点。
有人已经成功地用2.2.0运行了这种休息调用?