场景
JSON转为对象时,报错value not one of declared enum instancenames
报错原因
转换对象中有一个字段属性为枚举类型emun
,json中的该属性不在我定义的枚举范围的任何一个,所以报错。
解决方法
采用ObjectMapper
来JSON-to-Object
。
ObjectMapper mapper = new ObjectMapper()
//忽略未知enum字段,置为null
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
//反序列化忽略未知字段
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
Protocol protocol = mapper.readValue(mg, Protocol.class);