我收到 JSON 解析错误:无法从 START_OBJECT token 中反序列化 java.util.HashSet
的实例,在我的 Spring Boot 项目中,当我尝试保存时与我的另一个 Pojo 映射为一对多关系的 Pojo 类对象。我不确定我是否在 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;
}
}
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);
}
}
服务类:
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"
}
}
我在控制台上收到此错误消息:-
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 ofjava.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 是一个对象 START_OBJECT
代替。它不知道如何将一个对象变成一个集合,所以它放弃了。错误在 Vendor["children"]
您的请求包含针对 child 的:
"children" : {
"customerId" : 1,
"customerName" : "AMIT"
}
因为 children
是一个集合,如果你想要一个单独的 Child,它应该是这样的:
"children" : [
{
"customerId" : 1,
"customerName" : "AMIT"
}
]
这将是一个 JSON 格式的对象数组,可以很好地对应于 Customer
的 Set
。
关于json - 我正面临 **JSON 解析错误 : Cannot deserialize instance of `java.util.HashSet` out of START_OBJECT token** in Spring Boot project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58824476/