How to parse JSON in Spring Boot Application automatically using Jackson

问题: I have json file with json object as value of property inside: { "name": "name", "json": {...} } I need to get it automatically in RestController and use it as entit...

问题:

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

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

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

My entity is:

UPDATE -> more specified entity

@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..
}

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

How can I solve It? The problem is in setJsonSchema() perhaps

update:

public String getJsonSchema() {
        return jsonSchema;
    }

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

Such getters/setters don't solve the problem


回答1:

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.

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.

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();
    }
  }
}

回答2:

You can try this one How to store schema-less EAV (Entity-Attribute-Value) data using JSON and Hibernate , You don’t have to create all these types manually, you can simply get them via Maven Central using the following dependency:

<dependency>
    <groupId>com.vladmihalcea</groupId>
    <artifactId>hibernate-types-52</artifactId>
    <version>${hibernate-types.version}</version> 
</dependency> 

For more info, check out the hibernate-types open-source project.

Then you can simply declare the new type you class.

@TypeDef(
    name = "jsonb-node", 
    typeClass = JsonNodeBinaryType.class
)

And the entity mapping will look like this:

@Type(type = "json-node")
@Column(columnDefinition = "json")
private JsonNode json;
  • 发表于 2019-03-05 13:41
  • 阅读 ( 70 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除