通过 REST 调用使用外键将项目添加到集合中
add item to the collection with foreign key via REST call
我有 2 个具有双向关联的 jpa 实体。
持有项目集合的实体Container
(oneToMany)
省略 getter/setters
@javax.persistence.Entity
@Table(name = "CONTAINER")
public class Container implements Serializable {
private static final long serialVersionUID = -3288335692695653843L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "container", cascade = CascadeType.ALL)
private List<Item> items;
}
实体Item
包含对容器 (ManyToOne) 的引用,具有属性值和日期。
省略 setter/getters
@javax.persistence.Entity
@Table(name = "ITEM")
public class Item implements Serializable {
private static final long serialVersionUID = -758343957629274274L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Basic
private Long value;
@Basic
private Date date;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CONTAINER_ID")
private Container container;
}
我也在使用 spring-data 存储库来公开数据。
我的界面存储库只是扩展了 CrudRepository<Container, Long>
和 CrudRepository<Item, Long>
@RepositoryRestResource
public interface ItemRepository extends CrudRepository<Item, Long> {
}
@RepositoryRestResource
public interface ContainerRepository extends CrudRepository<Container, Long> {
}
我正在尝试通过 REST 调用创建项目。
首先我在项目存储库上尝试了这个 rest/items
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000", "container": {"id":"1"}}
但它只是在容器上创建具有空引用的项。
当我尝试通过容器存储库添加时 rest/containers/1/items
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000", "container": {"id":"1"}}
我得到 HTTP/1.1 204 No Content
和 <Response body is empty>
。没有创建实例。
我的问题是如何通过引用容器的 REST 调用添加项目。
编辑:为了说明我的问题,我想为现有容器添加新项目。我不确定在通过 rest(json)
创建 Item 实例时如何处理外部 ID 密钥
http 响应状态 204 为无内容。也就是说,您调用的 REST 方法是无效的,即使实例已创建,它也不会 return 任何内容。
您确定没有创建实例吗?如果没有,请post提供创建它们的代码,以便我们更好地了解发生了什么。
我通过对 json 内的容器使用 link 解决了这个问题。
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000","container":"http://localhost:8080/container/1"}
我不确定没有 spring-data-rest
是否有效
编辑:我应该指出,linked 资源必须是 @RepositoryRestResource
并且应该是 aggregate root.
我有 2 个具有双向关联的 jpa 实体。
持有项目集合的实体Container
(oneToMany)
省略 getter/setters
@javax.persistence.Entity
@Table(name = "CONTAINER")
public class Container implements Serializable {
private static final long serialVersionUID = -3288335692695653843L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "container", cascade = CascadeType.ALL)
private List<Item> items;
}
实体Item
包含对容器 (ManyToOne) 的引用,具有属性值和日期。
省略 setter/getters
@javax.persistence.Entity
@Table(name = "ITEM")
public class Item implements Serializable {
private static final long serialVersionUID = -758343957629274274L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Basic
private Long value;
@Basic
private Date date;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CONTAINER_ID")
private Container container;
}
我也在使用 spring-data 存储库来公开数据。
我的界面存储库只是扩展了 CrudRepository<Container, Long>
和 CrudRepository<Item, Long>
@RepositoryRestResource
public interface ItemRepository extends CrudRepository<Item, Long> {
}
@RepositoryRestResource
public interface ContainerRepository extends CrudRepository<Container, Long> {
}
我正在尝试通过 REST 调用创建项目。
首先我在项目存储库上尝试了这个 rest/items
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000", "container": {"id":"1"}}
但它只是在容器上创建具有空引用的项。
当我尝试通过容器存储库添加时 rest/containers/1/items
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000", "container": {"id":"1"}}
我得到 HTTP/1.1 204 No Content
和 <Response body is empty>
。没有创建实例。
我的问题是如何通过引用容器的 REST 调用添加项目。
编辑:为了说明我的问题,我想为现有容器添加新项目。我不确定在通过 rest(json)
创建 Item 实例时如何处理外部 ID 密钥http 响应状态 204 为无内容。也就是说,您调用的 REST 方法是无效的,即使实例已创建,它也不会 return 任何内容。
您确定没有创建实例吗?如果没有,请post提供创建它们的代码,以便我们更好地了解发生了什么。
我通过对 json 内的容器使用 link 解决了这个问题。
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000","container":"http://localhost:8080/container/1"}
我不确定没有 spring-data-rest
编辑:我应该指出,linked 资源必须是 @RepositoryRestResource
并且应该是 aggregate root.