【问题标题】:Adding a resource to a collection using spring-data-rest使用 spring-data-rest 将资源添加到集合
【发布时间】:2016-08-27 22:18:27
【问题描述】:

我的情况: 我有一个可以有很多工人的组织。我试图在 SDR 项目的主要撰稿人 this example 之后向一个组织添加一个新工作人员,但我遇到了各种错误(包括 204,但没有任何反应)。

这是我的实体和休息电话:

@Entity
public class Organization {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


    @OneToMany
    @JoinTable(name = "OrganizationWorker", joinColumns = {@JoinColumn(name = "OrganizationID")},
            inverseJoinColumns = {@JoinColumn(name = "WorkerID")})
    private Set<Worker> workers = new LinkedHashSet<>();
}

public class Worker {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String givenName;

    @NotNull
    private String familyName;

    @NotNull
    private LocalDate dob;

    @NotNull
    private String nationalId;

    private byte[] photo;
}

获取http://localhost:8080/hal

{
    "_links": {

        "workers": {
            "href": "http://localhost:8080/hal/workers{?page,size,sort}",
            "templated": true
        }
    }
}

发布http://localhost:8080/hal/workers

{
 "givenName": "James",
    "familyName": "Bond",
    "dob": "1970-01-01",
    "nationalId": "XXX-60-XXXX",
    "photo": null,
}

回复:

Location: http://localhost:8080/hal/workers/8
Date: Mon, 02 May 2016 16:53:02 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Application-Context: application
Content-Type: application/hal+json;charset=UTF-8


{
    "givenName": "James",
    "familyName": "Bond",
    "dob": "1970-01-01",
    "nationalId": "XXX-60-XXXX",
    "photo": null,
    "_links": {
        "self": {
            "href": "http://localhost:8080/hal/workers/8"
        },
        "worker": {
            "href": "http://localhost:8080/hal/workers/8"
        }
    }
}

最后一步,根据描述中的链接:

curl -X PUT -H "ContentType: text/uri-list" http://localhost:8080/hal/organizations/2 -d 'http://localhost:8080/hal/workers/8'

{
    "cause": null,
    "message": "Target bean is not of type of the persistent entity!"
}

做一些调试很明显具体的抱怨是什么。堆栈跟踪指向这里:

@Override
public PersistentPropertyAccessor getPropertyAccessor(Object bean) {

    Assert.notNull(bean, "Target bean must not be null!");
    Assert.isTrue(getType().isInstance(bean), "Target bean is not of type of the persistent entity!");

    return new BeanWrapper<Object>(bean);
}

getType() -> 组织

isInstance(bean) -> org.springframework.hateoas.Resource 的 bean 实例

对此有何意见?我按照信中的指示进行了操作。

【问题讨论】:

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


    【解决方案1】:

    这就是答案(我需要出去散步才能理清头绪并击中这个)。

    您必须发布到关联资源

    http://localhost:8080/hal/organizations/1/工人

    我突然想到了那个金块,然后我去重新阅读了这篇文章。

    对于像我这样的愚蠢错误,400 错误会更有用。

    【讨论】:

    • 这行得通。我也有类似的情况。但是,这会不断更新(仅)一个资源。你知道如何添加新资源吗?例如,为组织增加一名以上的员工。
    • 你不能 - 这不是它的工作原理。要添加额外的资源,您需要 GET /hal/organizations/1/workers 生成一个您添加到的数组,然后将 PUTPOST 添加到同一资源。我认为语义上 PATCH 是正确的步骤,但我知道 PATCH 的支持方式与其他动词相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 2015-08-30
    相关资源
    最近更新 更多