本文介绍了如何使用Jackson解析Spring Boot Application中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 json文件 带有json对象作为内部属性的值:

I have json file with json object as value of property inside:

{
 "name": "name",
 "json": {...}
}

我需要在 RestController 中自动获取,并将其用作 JPA + Hibernate 中的实体.

I need to get it automatically in RestController and use it as entity in JPA+Hibernate.

我的实体是:

更新->更多指定的实体

@Entity
@Table(name = "collections")
public class Collection {
    @Id
    private String name;

    @Column(name = "cache_limit")
    private int limit;

    @Column(name = "cache_algorithm")
    private String algorithm;

    @Transient
    private JsonNode schema;

    @JsonIgnore
    @Column(name ="json_schema")
    private String jsonSchema;

    public Collection() {
    }

    public String getJsonSchema() {
        return schema.toString();
    }

    public void setJsonSchema(String jsonSchema) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            schema = mapper.readTree(jsonSchema);
        } catch (IOException e) {
            throw new RuntimeException("Parsing error -> String to JsonNode");
        }
    }

   ..setters and getters for name limit algorithm schema..
}

当我使用entityManager.persist(Collection)时,我将json_schema列设为 NULL

When I use entityManager.persist(Collection) I have json_schema column as NULL

我该如何解决?问题可能在 setJsonSchema()

更新:

public String getJsonSchema() {
        return jsonSchema;
    }

    public void setJsonSchema(JsonNode schema) {
        this.jsonSchema = schema.toString();
    }

这种吸气剂/阻气剂不能解决问题

Such getters/setters don't solve the problem

推荐答案

您可以将JsonNode json属性定义为@Transient,因此JPA不会尝试将其存储在数据库中.但是,杰克逊应该能够将其翻译回来并转发给杰森.

You could define JsonNode json property as @Transient so JPA does not try to store it on database. However, jackson should be able to translate it back and forward to Json.

然后,您可以为JPA编写getter/setter的代码,以便从JsonNode转换为String来回转发.您定义一个将JsonNode json转换为String的吸气剂getJsonString.可以将其映射到表列(例如'json_string'),然后定义一个setter,从JPA接收String并将其解析为JsonNode可用的JsonNode.

Then you can code getter/setter for JPA, so you translate from JsonNode to String back and forward. You define a getter getJsonString that translate JsonNode json to String. That one can be mapped to a table column, like 'json_string', then you define a setter where you receive the String from JPA and parse it to JsonNode that will be avaialable for jackson.

不要忘记将@JsonIgnore添加到getJsonString,因此Jackson不会尝试将jsonString转换为json.

Do not forget to add @JsonIgnore to getJsonString so Jackson does not try to translate to json as jsonString.

@Entity
@Table(name = "cats")
public class Cat {

  private Long id;

  private String name;

  @Transient
  private JsonNode json;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  public Long getId() {
    return id;
  }

  @Column(name ="name")
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setId(Long id) {
    this.id = id;
  }

  // Getter and setter for name

  @Transient
  public JsonNode getJson() {
    return json;
  }

  public void setJson(JsonNode json) {
    this.json = json;
  }


  @Column(name ="jsonString")
  public String getJsonString() {
    return this.json.toString();
  }

  public void setJsonString(String jsonString) {
    // parse from String to JsonNode object
    ObjectMapper mapper = new ObjectMapper();
    try {
      this.json = mapper.readTree(jsonString);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

这篇关于如何使用Jackson解析Spring Boot Application中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:38