我有一个用几个已知字段定义的JSON。我将表单定义如下,
@JsonIgnoreProperties(ignoreUnknown = true)
public class TaxInitialForm {
@JsonProperty("taxId")
private String taxId;
@JsonProperty("Name")
private String Name;
@JsonProperty("dob")
private String dob;
@JsonProperty("city")
private String city;
@JsonProperty("state")
private String state;
@JsonProperty("zipCode")
private String zipCode;
//...//
}
我正在使用JSON获得“国家”字段。因此添加了@JsonIgnoreProperties(ignoreUnknown = true),假设未在表单中定义的未知字段将被忽略。但是我越来越傻了。例外
"com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "country" (class com.tax.vo.TaxInitialForm), not marked as ignorable
这是我反序列化的方式,
TaxInitialForm taxInitialForm = objectMapper.readValue(notifyMsg, TaxInitialForm .class);
我应该添加些什么以将其标记为可忽略?
答案 0 :(得分:0)
您必须像下面那样初始化对象映射器:
ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
希望这可以解决您的问题!