我正在寻找一个方便的解决方案与Jackson(2.8)在反序列化之前/期间筛选出指向空字符串值的字段:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Guis {
public enum Context {
SDS,NAVIGATION
}
public enum Layer {
Library,Status,Drivingdata
}
// client code
String s = "{\"context\":\"\",\"layer\":\"Drivingdata\"}";
ObjectMapper mapper = new ObjectMapper();
Guis o = mapper.readValue(s,Guis.class);
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type cq.speech.rsi.api.Guis$Context from String "": value not one of declared Enum instance names: [SDS NAVIGATION] at [Source: {"context":"","layer":"Drivingdata"}; line: 1,column: 12] (through reference chain: cq.speech.rsi.api.Guis["context"])
还有什么我试过的……
啊这一个
mapper.getDeserializationConfig().with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
错误仍然存在,显然甚至谷歌搜索都没有多大帮助……
编辑
set DeserializationFeature不能像上面举例说明的那样工作.对我来说,解决方案最终是添加这个片段:
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL,true)