我有以下简单的类:
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties({ "thirdField" })
public class Message {
private TypeA type;
private String producer;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
在我的测试课上
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的就是将上面的json字符串转换为Message类并忽略'thirdField'.但我一直在努力
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "thirdField" (class Message), not marked as ignorable (2 known properties: , "type", "producer"])
Run Code Online (Sandbox Code Playgroud)
Luk*_*tor 41
你混合了不同版本的杰克逊.您导入通知JsonIgnoreProperties
从org.codehaus.jackson.annotate
(1.x版本),而你正在使用ObjectMapper
从com.fasterxml.jackson.databind
(2.x版).
小智 5
尝试使用上一个Jackson版本(2.4):
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties({"id"})
Run Code Online (Sandbox Code Playgroud)
在这里,您可以找到使用2.4版实现它的示例:http: //www.ibm.com/developerworks/java/library/j-hangman-app/index.html
归档时间: |
|
查看次数: |
39502 次 |
最近记录: |