我面临**JSON解析错误:无法反序列化Spring Boot项目中的`java.util.HashSet`的实例,因为它超出了Start_Object内标识** [英] I am facing **JSON parse error: Cannot deserialize instance of `java.util.HashSet` out of START_OBJECT token** in Spring Boot project

查看:0
本文介绍了我面临**JSON解析错误:无法反序列化Spring Boot项目中的`java.util.HashSet`的实例,因为它超出了Start_Object内标识**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到JSON分析错误:当我尝试保存与一对多关系映射到我的另一个POJO的POJO类对象时,无法使用我的Spring Boot项目反序列化java.util.HashSetjava.util.HashSet的实例。我不确定我在邮递员中发送的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> {

}

我从邮递员那里发送的JSON格式:

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

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

无法读取HTTp消息:org.springframework.http.converter.HttpMessageNotReadableException:JSON分析错误:无法反序列化java.util.HashSet的实例超出START_OBJECT标记;嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化java.util.HashSet的实例超出START_OBJECT标记 在[来源:(Pushback InputStream);行:4,列:15](通过参考链:com.example.demo.model.Vendor["children"])

我需要改进什么?

推荐答案

的确,JB Nizet在评论中指出了这一点。Jackson告诉您,它正试图将JSON反序列化为一个Set(java.util.HashSet),这是一个集合,但该文件部分的JSON是一个对象START_OBJECT。它不知道如何把一个物体变成一套,所以它放弃了。错误位于Vendor["children"]

您的请求包含以下针对儿童的内容:

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

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

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

这将是JSON中的对象数组,它将与CustomerSet很好地对应。

这篇关于我面临**JSON解析错误:无法反序列化Spring Boot项目中的`java.util.HashSet`的实例,因为它超出了Start_Object内标识**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆