Spring 数据剩余 - PATCH Postgres jsonb 字段

Spring Data Rest - PATCH Postgres jsonb field

简短的版本是:如何使用 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 是 serialize/deserialize 的方法 Spring 提到的数据其余部分 here. We also tried to give the field its own Type, as mentioned in these answers.

我们的目标是修补此字段包含的 JSON 对象,使用 Spring 数据休息。

例如:

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 修补示例资源并覆盖每个请求的属性的值,而不是试图深入研究 JSON 对象的属性以仅修补请求的嵌套属性。

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

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 但是这种方法将需要您每次都从数据库中读取对象,并进行另一次 serialize/deserialize 往返。

但是如果你在 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.

中使用任何类型的结构

希望这对您有所帮助。

假设使用 Hibernate 5 作为 JPA 实现

使您的 jsonobject 字段成为特定的 class 类型(包含您想要的字段)而不是 String

然后您可以为 jsonb 类型添加自定义 Hibernate 用户类型。

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

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

    @Basic
    @Type(type = "com.package.JsonObjectType")
    private JsonObject jsonobject;
}

自定义类型的实现非常冗长,但本质上它使用 Jackson 的 ObjectMapper 将对象作为 String 传递到 JDBC 语句中(从 ResultSet 检索时反之亦然) .

public class JsonObjectType implements UserType {

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public int[] sqlTypes() {
        return new int[]{Types.JAVA_OBJECT};
    }

    @Override
    public Class<JsonObject> returnedClass() {
        return JsonObject.class;
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
        final String cellContent = rs.getString(names[0]);
        if (cellContent == null) {
            return null;
        }
        try {
            return mapper.readValue(cellContent.getBytes("UTF-8"), returnedClass());
        } catch (final Exception ex) {
            throw new HibernateException("Failed to convert String to Invoice: " + ex.getMessage(), ex);
        }
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.OTHER);
            return;
        }
        try {
            final StringWriter w = new StringWriter();
            mapper.writeValue(w, value);
            w.flush();
            st.setObject(index, w.toString(), Types.OTHER);
        } catch (final Exception ex) {
            throw new HibernateException("Failed to convert Invoice to String: " + ex.getMessage(), ex);
        }
    }

    @Override
    public Object deepCopy(final Object value) throws HibernateException {
        try {
            // use serialization to create a deep copy
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(value);
            oos.flush();
            oos.close();
            bos.close();

            ByteArrayInputStream bais = new ByteArrayInputStream(bos.toByteArray());
            return new ObjectInputStream(bais).readObject();
        } catch (ClassNotFoundException | IOException ex) {
            throw new HibernateException(ex);
        }
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public Serializable disassemble(final Object value) throws HibernateException {
        return (Serializable) this.deepCopy(value);
    }

    @Override
    public Object assemble(final Serializable cached, final Object owner) throws HibernateException {
        return this.deepCopy(cached);
    }

    @Override
    public Object replace(final Object original, final Object target, final Object owner) throws HibernateException {
        return this.deepCopy(original);
    }

    @Override
    public boolean equals(final Object obj1, final Object obj2) throws HibernateException {
        if (obj1 == null) {
            return obj2 == null;
        }
        return obj1.equals(obj2);
    }

    @Override
    public int hashCode(final Object obj) throws HibernateException {
        return obj.hashCode();
    }
}

最后,您需要告诉 hibernate 将 Java 对象存储为 jsonb Postgre 类型。这意味着创建您的自定义方言 class(并在其中进行配置)。

public class MyPostgreSQL94Dialect extends PostgreSQL94Dialect {

    public MyPostgreSQL94Dialect() {
        this.registerColumnType(Types.JAVA_OBJECT, "jsonb");
    }
}

有了所有这些,您应该没问题,Spring Data Rest 修补机制应该可以工作。

PS

答案很大程度上受到 this github 回购的启发,它的作用基本相同,但使用的是 Hibernate 4。看看那个。