3

一个 Enum 类,ColorName 是使用 ColorName json 文件中的 jsonschema2pojo 生成的。因此,默认情况下,Enum 类具有使用 @JsonCreator 注释的 fromValue 方法。

@JsonCreator
public static ColorName fromValue(String value) {
    ColorName constant = constants.get(value);
    if (constant == null) {
        throw new IllegalArgumentException(value);
    } else {
        return constant;
    }
}

如果此类用于将 json 字符串反序列化为 Java 对象,并且在 json 中传递了新的/错误的 Enum 值,则反序列化失败,IllegalArgumentException() 当尝试使用以下方法避免此错误时,它不起作用

ObjectMapper om = new ObjectMapper(); om.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);

这个问题似乎是因为@JsonCreator 总是很荣幸。有没有办法指示反序列化器跳过这个或忽略未知的枚举值?

4

1 回答 1

0

从https://github.com/FasterXML/jackson-databind/pull/1642/files开始,这已通过对 Jackson 库本身的更改来解决

根据https://github.com/FasterXML/jackson-databind/pull/1642它已被反向移植到 Jackson 2.8.9,因此请尝试升级到该版本。

于 2019-08-13T07:59:06.913 回答