hibernate 我遇到**JSON解析错误:无法反序列化Sping Boot 项目中START_OBJECT标记**外的“java.util.HashSet”示例

bqujaahr  于 2023-02-04  发布在  Java
关注(0)|答案(2)|浏览(226)

我收到JSON解析错误:当我尝试保存使用一对多关系与另一个PojoMap的Pojo类对象时,无法使用我的Spring Boot项目反序列化START_OBJECT标记外的java.util.HashSet示例。我不确定是否在Postman中发送正确的JSON格式。我尝试保存已定义Collection元素Set的持久类的值。
父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"
                 }
}

我在控制台上收到此错误消息:-
无法读取HTTP消息:数据库服务器。http。转换器。HttpMessageNotReadableException:JSON解析错误:无法反序列化START_OBJECT标记外的java.util.HashSet示例;嵌套异常为com. fasterxml. jackson. databind. exc。输入不匹配异常:无法反序列化java.util.HashSet的示例(位于[源:(推回输入流);行:4,列:15](通过参考链:com.示例.演示.模型.供应商["子项"])
我需要改进什么?

iih3973s

iih3973s1#

JBNizet确实在一条评论中指出了这一点。Jackson告诉您,它试图将JSON反序列化为Setjava.util.HashSet),这是一个集合,但文件的这一部分的JSON是一个对象START_OBJECT。它不知道如何将对象转换为集合,所以放弃了。错误在Vendor["children"]
您的申请包含以下内容(针对儿童):

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

由于children是一个集合,因此如果您想要单个Child,它应该如下所示:

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

这将是JSON中的一个对象数组,它将很好地对应于CustomerSet

o0lyfsai

o0lyfsai2#

Jackson告知您正在发送普通Java对象,而不是集合对象,请检查您Map的请求,实体内的集合不是对象,请按以下格式发送您的请求
当前您发送的-
{“用户名”:“XAdmin”、“用户名”:“家庭”、“用户姓氏”:“客户端”、“用户密码”:“管理@123”,

**"role": {
    "roleName": "admin",
    "roleDescription": "admin profile"
}**

}
代替上述要求,请发送-
{“用户名”:“XAdmin”、“用户名”:“家庭”、“用户姓氏”:“客户端”、“用户密码”:“管理@123”,

**"role": [{
    "roleName": "admin",
    "roleDescription": "admin profile"
}]**

}

相关问题