我有以下簡單的類: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'。但我不斷收到
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "thirdField" (class Message), not marked as ignorable (2 known properties: , "type", "producer"])
好抓。我改變它來導入com.fasterxml.jackson.annotation.JsonIgnoreProperties; 但仍然得到相同的異常 – kk1957
@ kk1957我再現了你的情況,唯一的區別是我從com.fasterxml.jackson.annotation中導入了JsonIgnoreProperties並且它工作正常 - 沒有任何例外,消息對象被正確地反序列化。嘗試結帳https://github.com/LukaszWiktor/json-ignore-properties-test並運行Test.main() –
是的,看起來像我的設置有其他的東西不對。謝謝您的幫助。 – kk1957