Spring Data Rest - PUT 不适用于关联的引用类型? [英] Spring Data Rest - PUT is not working for associated reference types?

查看:50
本文介绍了Spring Data Rest - PUT 不适用于关联的引用类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Spring Data Rest 项目实现了以下域类.

I have the following domain class implemented for a Spring Data Rest project.

@Entity
@Data
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long addressID;

    private String houseName;

    private String apartmentNumber;

    @ManyToOne
    private City city;

    @ManyToOne
    private Country country; 

}

现在我通过发送带有以下 JSON 的 POST 来创建地址资源.

Now I am creating an Address resource by sending a POST with following JSON.

{   
    "houseName":"Some House",
    "apartmentNumber":"13 B",
    "city": "http://localhost:8080/city/1"
    "country":"http://localhost:8080/countries/1"
}

当我使用以下 JSON 向端点 http://localhost:8080/addresses/1 发送 PUT 请求时,houseName 的值会更新.然而,即使我为城市发送了不同的 URI,城市也保持不变.

When I send a PUT request to the endpoint http://localhost:8080/addresses/1 with the following JSON, the values for houseName is updated. However the city remains unchanged even though I am sending a different URI for the city.

{   
    "houseName":"Another House",
    "apartmentNumber":"13 B",
    "city": "http://localhost:8080/city/2"
    "country":"http://localhost:8080/countries/1"
}

如果我发送 PATCH 而不是 PUT,城市值也会更新.那么我该如何解决这个问题?

If I send a PATCH instead of PUT the city value is also updated. So how do I fix this?

更新 1

乡村课

@Data
@Entity
public class Country {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long countryID;

    private String countryName;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country", orphanRemoval = true)
    private List<City> cities;

}

城市类

@Data
@Entity
public class City {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long cityID;

    private String cityName;

    @ManyToOne
    @JoinColumn(name = "country_id")
    private Country country;
}

推荐答案

我遇到了同样的问题,并设法找到了一些相关信息.

I had the same problem and manage to find some information on it.

它是 Spring Data Rest2.5.7 版本的更改,并且是出于目的".

It is a change in version 2.5.7 of Spring Data Rest and is "by purpose".

奥利弗·德罗博姆的回答是:

The answer of Oliver Drotbohm is:

我对此进行了调查,我认为您希望事情能够在他们不工作的方式.PUT 请求不考虑与可链接的资源,即指向的相关资源链接.原因有两个:

I looked into this and I'd argue you're expecting things to work in a way they don't work. PUT requests don't consider associations to linkable resources, i.e. related resources that are pointed to by links. The reason for that is two-fold:

  1. 如果我们考虑使用负载中关联字段的 URI 来更新这些关联,那么问题就出现了如果未指定 URI,则会发生这种情况.根据当前的行为,链接关联根本不是有效载荷的一部分,因为它们只驻留在在 _links 块中.在这种情况下,我们有两个选择:擦除未处理的关联,这打破了PUT what you GET"方法.只擦除使用 null 提供的那些会排序模糊你把资源的整个状态".
  2. 出于 1. 中提到的所有原因,公开了可以直接操作的专用关联资源.

所以看起来如果你想改变资源的两个状态加公会的同时,估计暴露了一个专门的资源来做到这一点是要走的路.

So it looks like that if you want to change both state of the resource plus associations at the same time, I guess exposing a dedicated resource to do that is the way to go.

您可以在 Jira Spring 网站上找到完整答案:无法使用项目资源上的 PUT 请求更新关联资源

Full answer you can find on Jira Spring site: Unable to update associated resource using PUT request on the item resource

(我写的关于堆栈溢出的问题在这里:Spring Data Rest - 存储库上的 PUT 在子引用上静默失败)

(the question I wrote on stack overflow is here: Spring Data Rest - PUT on repository silently fails on child references)

这篇关于Spring Data Rest - PUT 不适用于关联的引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆