gpt4 book ai didi

jackson - JsonIgnoreProperties 不起作用

转载 作者:行者123 更新时间:2023-12-02 05:20:00 24 4
gpt4 key购买 nike

我有以下简单的类:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties({ "thirdField" })
public class Message {

private TypeA type;
private String producer;

//Getters and Setters

}

在我的测试课

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {
public void testMethd() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.USE_ANNOTATIONS, true);
Class<T> instanceType = Message.class;

String msgBody = "{\"producer\": \"clientApp\", \"type\": \"aType\", \"thirdField\": []}";
objectMapper.readValue(msgBody, instanceType);
}
}

我想做的就是将上面的 json 字符串转换为 Message 类并忽略“thirdField”。但我不断得到

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "thirdField" (class Message), not marked as ignorable (2 known properties: , "type", "producer"])

最佳答案

您混合了 jackson 的不同版本。请注意,您从 org.codehaus.jackson.annotate(版本 1.x)导入 JsonIgnoreProperties当您使用 com.fasterxml.jackson.databind(版本 2.x)中的 ObjectMapper 时。

关于jackson - JsonIgnoreProperties 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20986995/

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