gpt4 book ai didi

java - 当使用 Spring Boot Rest Controller 中的其他实体引用映射到 Hibernate 实体的 JSON 时,Jackson InvalidTypeIdException

转载 作者:行者123 更新时间:2023-12-02 10:19:29 26 4
gpt4 key购买 nike

我正在开发 REST Web 服务,并且我有以下内容:

项目.java

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Item implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "item_generator")
@SequenceGenerator(name="item_generator", sequenceName = "base.item_seq")
@Column(name = "id")
private long id;

@Column(name = "item_name")
private String itemName;

@JsonIgnore
@JsonManagedReference
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(
name="join_category_item",
joinColumns = @JoinColumn(name="item_id"),
inverseJoinColumns = @JoinColumn(name="category_id")
)
private Category category;
}

类别.java

@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_generator")
@SequenceGenerator(name="category_generator", sequenceName = "base.category_seq")
@Column(name = "id", nullable = false)
private long id;

@Column(name = "category_name")
private String categoryName;

@JsonBackReference
@OneToMany(mappedBy = "category")
@Fetch(FetchMode.JOIN)
private Collection<Item> items;
{

Controller 类:

@PostMapping(value = "/create",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Item> createItem(@RequestBody Item item) {
// some logic here...
}

所以我的问题是,当我尝试使用此 json 从 @Controller 调用 @PostMapping 时:

{
"Item": {
"itemName": "Item name",
"category": {
"categoryName": "Category Name"
}
}
}

我收到以下错误:

"[http-nio-8080-exec-6] WARN org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Could not resolve type id 'categoryName' as a subtype of [simple type, class com.test.Category]: known type ids = [Category] (for POJO property 'category'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'categoryName' as a subtype of [simple type, class com.test.Category]: known type ids = [Category] (for POJO property 'category') at [Source: (PushbackInputStream); line: 5, column: 4] (through reference chain: com.test.Item["category"])]"

另外,使用

{..., "Category" : {"categoryName": "Category Name"}}

在我的json(大写C)中,不会引发异常,而只是以空类别进入后映射。

提前谢谢您。

最佳答案

删除 @JsonTypeInfo 注释,我认为您尝试使用的 json 结构不需要它们。它们暗示 Item 和 Category 之间存在继承层次结构,但实际上并不存在,Item 仅具有对 Category 的引用。

关于java - 当使用 Spring Boot Rest Controller 中的其他实体引用映射到 Hibernate 实体的 JSON 时,Jackson InvalidTypeIdException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54446328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com