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

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

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

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

    private JsonNode schema;

    @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


public String getJsonSchema() {
        return jsonSchema;

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

Such getters/setters don't solve the problem


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.

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

  private Long id;

  private String name;

  private JsonNode json;

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

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

  public void setName(String name) { = name;

  public void setId(Long id) { = id;

  // Getter and setter for name

  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) {


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:


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

Then you can simply declare the new type you class.

    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. 小编 文章