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

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

我收到 JSON 解析错误:无法从 START_OBJECT 令牌 反序列化 java.util.HashSet 的实例,使用我的 Spring 引导项目,当我试图保存 Pojo class object 时,它与我的另一个 Pojo 映射为 One-To-Many 关系。我不确定我是否在 Postman 中发送了正确的 JSON 格式。我正在尝试保存已定义 Collection 元素集的持久 class 的值。

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 波乔 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;
    }

}

控制器:

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

服务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);
    }
}

存储库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> {

}

我从 Postman 发送的 JSON 格式:

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

我在控制台上收到此错误消息:-

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"])

我需要改进什么?

的确,JB Nizet 在评论中指出了这一点。 Jackson 告诉你它正在尝试将 JSON 反序列化为 Set (java.util.HashSet),这是一个集合,但是文件那部分的 JSON 是 object START_OBJECT 代替。它不知道如何将 object 变成一个集合,所以它放弃了。错误在 Vendor["children"]

您的请求包含 children:

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

因为children是一个集合,如果你想要一个Child,它应该是这样的:

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

那将是 JSON 中的 object 的数组,这将很好地对应于 SetCustomer