gpt4 book ai didi

java - jackson 无法将空字符串值转换为枚举

转载 作者:搜寻专家 更新时间:2023-10-31 08:10:41 25 4
gpt4 key购买 nike

我正在寻找 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)

最佳答案

您可以为映射器启用 DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL,默认情况下禁用。唯一需要注意的是使用它,它将把所有未知的包括空字符串都视为枚举的 null。

关于java - jackson 无法将空字符串值转换为枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51406816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com