【问题标题】:How to create multiple resources with one Request in Spring Data Rest?如何在 Spring Data Rest 中使用一个请求创建多个资源?
【发布时间】: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


【解决方案1】:

您需要一个控制器。

本质上,您希望覆盖资源上的 POST 方法以接受项目列表而不是单个项目。

您必须在控制器中获取对您的存储库的引用并手动插入对象。

【讨论】:

    猜你喜欢
    • 2017-03-14
    • 2018-07-10
    • 2017-07-27
    • 2014-11-04
    • 2016-11-06
    • 2015-05-02
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多