当前位置: 首页 > 知识库问答 >
问题:

我面临**JSON解析错误:无法反序列化Spring Boot项目中START_OBJECT标记外的`java.util.hashset`实例**

蓟清野
2023-03-14

当我试图保存与另一个Pojo以一对多关系映射的Pojo类对象时,我收到了JSON解析错误:无法反序列化START_OBJECT令牌中的java.util.hashset实例。我不确定在Postman中是否发送了正确的JSON格式。我正在尝试保存定义了集合元素集的持久性类的值。

父Pojo类:

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

子Pojo类:

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

存储库类:

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"
                 }
}

我在控制台收到这个错误消息:-

我需要改进什么?

共有1个答案

蒙麒
2023-03-14

事实上,JB Nizet在一篇评论中就指出了这一点。Jackson告诉您,它正在尝试将JSON反序列化为set(java.util.hashset),这是一个集合,但文件的这一部分的JSON是一个对象start_object。它不知道如何把一个对象变成一个集合,所以它在放弃。错误位于供应商[“children”]

您的请求包含以下内容:

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

因为children是一个集合,所以如果您想要一个单独的子级,它应该如下所示:

"children" : [
    {
        "customerId" : 1,
        "customerName" : "AMIT"
    }
]
 类似资料: