JsonIgnoreProperties不起作用

JsonIgnoreProperties不起作用

本文介绍了JsonIgnoreProperties不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单类:

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'。但我一直在

All I am trying to do is to convert the above json string into Message class and ignore the 'thirdField'. But I keep getting

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


推荐答案

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

You've mixed different versions of Jackson.Notice that you import JsonIgnoreProperties from org.codehaus.jackson.annotate (version 1.x)while you're using ObjectMapper from com.fasterxml.jackson.databind (version 2.x).

这篇关于JsonIgnoreProperties不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:57