jsonhibernatespring-boothibernate-mappinghibernate-onetomany

I am facing **JSON parse error: Cannot deserialize instance of `java.util.HashSet` out of START_OBJECT token** in Spring Boot project


I am getting JSON parse error: Cannot deserialize instance of java.util.HashSet out of START_OBJECT token, with my Spring Boot project, when I am trying to save Pojo class object which is mapped with One-To-Many relationship with my another Pojo. I am not sure whether I am sending the right format of JSON in Postman. I am trying to save values of a persistent class that has a Collection element Set defined.

The Parent Pojo class:

package com.example.demo.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "vendor")
public class Vendor {

    @Id
    int vendorId;

    @Column
    String vendorName;

    @OneToMany(fetch = FetchType.LAZY, targetEntity = Customer.class, cascade = CascadeType.ALL)
    @JoinColumn(name = "vendorId")

    Set children;

    public int getVendorId() {
        return vendorId;
    }

    public void setVendorId(int vendorId) {
        this.vendorId = vendorId;
    }

    public String getVendorName() {
        return vendorName;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public Set getChildren() {
        return children;
    }

    public void setChildren(Set children) {
        this.children = children;
    }
} 

Child Pojo Class:

package com.example.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GeneratorType;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@Entity
@Table(name = "customer")
public class Customer {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    int customerId;

    @Column
    String customerName;

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

}

Controller:

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Vendor;
import com.example.demo.service.VendorDataSaveService;

@RestController
public class VendorSaveController {

    @Autowired
    private VendorDataSaveService dataSaveService;

    @PostMapping("/save")
    public void saveVendor(@RequestBody Vendor vendor) {
        dataSaveService.saveVendorRecord(vendor);
    }
}

Service class:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.model.Vendor;
import com.example.demo.repository.VendorDataSaveRepository;

@Service
public class VendorDataSaveService {

    @Autowired
    private VendorDataSaveRepository repository;

    public void saveVendorRecord(Vendor vendor) {
        repository.save(vendor);
    }
}

Repository class:

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.model.Vendor;

public interface VendorDataSaveRepository extends JpaRepository<Vendor, Integer> {

}

The JSON Format I have Sent from Postman:

    "vendorId" : 101,
    "vendorName" : "JAIN BOOKS",
    "children" : {
                    "customerId" : 1,
                    "customerName" : "AMIT"
                 }
}

I am getting this error message on console:-

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.HashSet out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.HashSet out of START_OBJECT token at [Source: (PushbackInputStream); line: 4, column: 15] (through reference chain: com.example.demo.model.Vendor["children"])

What do I need to improve?


Solution

  • Indeed, JB Nizet nailed it in a comment. Jackson is telling you that it's trying to deserialize JSON into a Set (java.util.HashSet), which is a collection, but the JSON for that part of the file is a object START_OBJECT instead. It doesn't know how to turn an object into a set, so it's giving up. The error is at Vendor["children"]

    Your request contains this for children:

    "children" : {
        "customerId" : 1,
        "customerName" : "AMIT"
    }
    

    Since children is a collection, if you want a single Child, it should look like this:

    "children" : [
        {
            "customerId" : 1,
            "customerName" : "AMIT"
        }
    ]
    

    That would be an array of objects in JSON, which would correspond nicely to your Set of Customers.