【发布时间】:2015-11-10 05:59:37
【问题描述】:
为了使用单个 http 请求在我的数据库中创建多条记录,我需要实现什么?
我有一个使用 SpringDataRest 和 JPA/Hibernate 的小型 Web 应用程序,我可以在其中创建具有如下请求的资源:
curl -XPUT -H"Content-Type: application/json; charset utf-8"\
-d'{"id":"1","type":"test"}'\
http://localhost:8080/test/items/1
相反,我想做类似的事情:
curl -XPUT -H"Content-Type: application/json; charset utf-8"\
-d'[{"id":"1","type":"test1"},{"id":"2","type":"test2"}]'\
http://localhost:8080/test/items/
相应的存储库如下所示:
@RestResource(path = "items", rel = "items")
public interface ItemRepository extends PagingAndSortingRepository<Item, String> {
}
使用的 Bean:
@Entity
@XmlRootElement(name = "page")
@Table(name="page")
public class Item {
@Id
@Column(name="id")
private String id;
@Column(name="type")
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
}
【问题讨论】:
-
你卡在哪里了?
-
PUT 数据时出现 Http 错误。使用 POST,只创建第一条记录。
标签: java hibernate jpa spring-data-rest