Spring Data Rest - PATCH Postgres jsonb 字段

标签 spring postgresql spring-data-rest jsonb json-patch

简短版本是:如何使用 Spring Data Rest PATCH 方法修补包含在 Postgres jsonb 字段中的 JSON 对象?

这里是长版本,请考虑以下实体:

@Entity
@Table(name = "examples")
public class Example {

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

    private String jsonobject;

    @JsonRawValue
    public String getJsonobject() {
        return jsonobject == null ? null : jsonobject;
    }

    public void setJsonobject(JsonNode jsonobject) {
        this.jsonobject = jsonobject == null ? null : jsonobject.toString();
    }
}

jsonobject 是 Postgres 类型 jsonb。这些 getter/setter 是为提到的 Spring Data Rest 序列化/反序列化它的方式 here .我们还尝试为该字段提供自己的类型,如 these answers 中所述.

我们的目标是使用 Spring Data Rest 修补该字段包含的 JSON 对象。

例如:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Hello"},
         "baz": 2
    }
}

PATCH /examples/1
{
    "jsonobject": {
        "foo": {"bar": "Welcome"}
    }
}

预期输出:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Welcome"},
         "baz": 2
    }
}

当前输出:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Welcome"}
    }
}

Spring Data Rest 修补 Example 资源并覆盖每个请求的属性的值,而不是试图深入 JSON 对象的属性以仅修补请求的嵌套属性。

此时我们认为 Spring Data Rest 对 application/merge-patch+jsonapplication/json-patch+json 媒体类型的支持会派上用场。以下是每种媒体类型的输出:

application/merge-patch+json:

PATCH /examples/1
{
    "jsonobject": {
        "foo": {"bar": "Welcome"}
    }
}

输出:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Welcome"}
    }
}

application/json-patch+json:

PATCH /examples/1
[
    { "op": "replace", "path": "/jsonobject/foo/bar", "value": "Welcome" }
]

输出:

{
    "cause": {
        "cause": null,
        "message": "EL1008E:(pos 8): Property or field 'foo' cannot be found on object of type 'java.lang.String' - maybe not public?"
    },
    "message": "Could not read an object of type class com.example.Example from the request!; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 8): Property or field 'foo' cannot be found on object of type 'java.lang.String' - maybe not public?"
}

归结为相同的想法:仅查找实体属性,然后完全覆盖或找不到。

问题如下:有没有办法让 Spring Data Rest 理解它正在处理 jsonb 字段,从而查找 JSON 嵌套属性而不是只查找实体属性?

注意:@Embeddable/@Embedded 注释最有可能被避免,因为它们暗示知道嵌套的属性名称,这会降低对 jsonb 字段的兴趣.

感谢阅读。

最佳答案

好吧,您的 EntityManager 不知道您的 jsonObject 字段中有一些结构,它是纯字符串。您应该实现自己的解决方法。您可以如何开始工作的一个例子在这里 https://github.com/bazar-nazar/pgjson但是这种方法将要求您每次都从数据库中读取对象,并进行另一次序列化/反序列化往返。

但是如果你在 postgresql 上,你可以使用它的所有功能(注意:这会使你的应用程序与 postgresql 紧密耦合,因此数据库将变得更难被替换)

我建议实现自定义 jdbc 查询,比如简单的例子:

public static class JsonPatchRequest {
    String path;
    String operation;
    String value;
}


@Inject
private JdbcTemplate jdbcTemplate;

@PatchMapping(value = "/example/{id}") 
public void doPatch(@PathVariable("id") Long id, @RequestBody JsonPatchRequest patchRequest) {
    // this line should transform your request path from  "/jsonobject/foo/bar"  to "{foo,bar}" string
    String postgresqlpath = "{" + patchRequest.path.replaceFirst("/jsonobject/", "").replaceAll("/", ",") + "}";

    switch(patchRequest.operation) {
        case "replace" :
            jdbcTemplate.execute("UPDATE example SET jsonobject = jsonb_set(jsonobject, ?, jsonb ?) WHERE id = ?", new PreparedStatementCallback<Void>() {
                @Override
                public Void doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    ps.setString(1, postgresqlpath);

                    // this one transforms pure value, to string-escaped value (manual workaround)   so  'value' should become '"value"'
                    ps.setString(2, "\"".concat(patchRequest.value).concat("\""));

                    ps.setLong(3, id);

                    ps.execute();
                    return null;
                }
            });
            break;
        case "delete" :
            jdbcTemplate.execute("UPDATE example SET jsonobject = jsonobject #- ? WHERE id = ? ", new PreparedStatementCallback<Void>() {
                @Override
                public Void doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                    ps.setString(1, postgresqlpath);
                    ps.setLong(2, id);
                    ps.execute();
                    return null;
                }
            });
            break;
    }
}

另请注意:第一种方法将强制您将 jsonobjet 字段设为预定义类型,因此可以将其替换为纯规范化实体,因此与它没有太大关系。第二种方法不会强制您在 json 中包含任何类型的结构。

希望对您有所帮助。

关于Spring Data Rest - PATCH Postgres jsonb 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40366781/

相关文章:

json - 使用 Spring Data JPA 将 JSON 作为字段存储在 PostgreSQL 中

java - Spring 将 Clock 作为构造函数参数注入(inject)时出现问题

java - 在 Eclipse (3.6 Helios) 中使用 Spring (3.0.5)

sql - 选择计数查询

postgresql - 为选择中的每一行调用一个函数 - Postgres

java - Spring Data REST - PUT 请求自 v.2.5.7 起无法正常工作

java - 使用 JWT 的 Spring Data Rest

java - 如何在自定义 Spring Data Rest Controller 中正确处理 POST?

java - IntelliJ IDEA Community Edition 和 Spring 使用 Spring DevTools 缓慢重新加载

java - Spring MVC : Generating links which match the controller url pattern