Spring Data Rest - PATCH Postgres jsonb字段

Ant*_*gon 8 postgresql spring spring-data-rest json-patch jsonb

简短版本是:如何jsonb使用Spring Data Rest PATCH方法修补Postgres 字段中包含的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

例如:

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

PATCH /examples/1
{
    "jsonobject": {
        "foo": {"bar": "Welcome"}
    }
}
Run Code Online (Sandbox Code Playgroud)

预期产量:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Welcome"},
         "baz": 2
    }
}
Run Code Online (Sandbox Code Playgroud)

当前输出:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Welcome"}
    }
}
Run Code Online (Sandbox Code Playgroud)

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"}
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

GET /examples/1
{
    "id": 1,
    "jsonobject": {
         "foo": {"bar": "Welcome"}
    }
}
Run Code Online (Sandbox Code Playgroud)

application/json-patch+json:

PATCH /examples/1
[
    { "op": "replace", "path": "/jsonobject/foo/bar", "value": "Welcome" }
]
Run Code Online (Sandbox Code Playgroud)

输出:

{
    "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?"
}
Run Code Online (Sandbox Code Playgroud)

这归结为相同的想法:只查找实体属性,并完全覆盖或找不到.

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

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

谢谢你的阅读.

Ily*_*hin 2

好吧,您的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

希望对你有帮助。