java 空字符转枚举_java – 杰克逊无法将空字符串值转换为枚举

我正在寻找一个方便的解决方案与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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值