Spring Data REST:无法使用json-patch更新关联

时间:2019-03-13 22:09:59

标签: spring-data-rest json-patch

我有两个与@ManyToOne相关联的实体:

@Entity
public class Header {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
}

@Entity
public class Detail {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @ManyToOne
    @JoinColumn(name = "headerId")
    private Header header;
}

我正在使用具有Content-Type:application / json-patch + json的PATCH请求,以在创建后将Detail关联到Header,但是它失败如下(为清晰起见,格式化了错误响应):

curl -X PATCH http://localhost:8080/details/3 -H 'Content-Type: application/json-patch+json' -d '[{"op":"replace","path":"/header","value":"http://localhost:8080/headers/2"}]'

{
    "cause": {
        "cause": {
            "cause": {
                "cause": {
                    "cause" : null,
                    "message" : "No converter found capable of converting from type [java.lang.String] to type [@javax.persistence.ManyToOne @javax.persistence.JoinColumn com.test.sdr.jsonpatchtest.Header]"
                },
                "message" : "EL1001E: Type conversion problem, cannot convert from java.lang.String to @javax.persistence.ManyToOne @javax.persistence.JoinColumn com.test.sdr.jsonpatchtest.Header"
            },
            "message" : "Type conversion failure"
        },
        "message" : "EL1034E: A problem occurred whilst attempting to set the property 'header': Type conversion failure"
    },
    "message" : "Could not read an object of type class com.test.sdr.jsonpatchtest.Detail from the request!; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1034E: A problem occurred whilst attempting to set the property 'header': Type conversion failure"
}

通过非json-patch的PATCH请求更新关联可以正常工作:

curl -X PATCH http://localhost:8080/details/3 -H 'Content-Type: application/json' -d '{"header":"http://localhost:8080/headers/2"}'

通过PUT请求更新关联也可以:

curl -X PUT http://localhost:8080/details/3/header -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/headers/1'

调试代码(比较非json补丁与json补丁PATCH请求)显示以下内容:

  • 非json修补程序使用UriToEntityConverter,该UriToEntityConverter从URI中提取ID,然后按ID查找Header实体。
  • json补丁使用GenericConversionService $ NoOpConverter,因为它无法找到从String到Header的转换器

我的json补丁请求是否错误?

我是否必须显式注册一个转换器以通过json-patch PATCH请求使更新关联起作用?

这是不受支持的功能吗?还是错误?

我已将一个示例应用程序推送到GitHub来演示此问题。

感谢您的回复。

0 个答案:

没有答案