java – JacksonMapper反序列化null值

我将Json null值反序列化为
Java Object空字符串

我可以创建自定义反序列化器,但是当Json值为null时,它不会进入反序列化器.

我该如何反序列化呢?

提前致谢!

public class CustomStringDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException,
        JsonProcessingException {

    String str = jsonparser.getText();

    try {
        return (str == null) ? "" : str;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

}

   public CustomObjectMapper() {
        SimpleModule _module = new SimpleModule("Module", new Version(1, 9, 10, "FINAL"));
        _module.addDeserializer(String.class, new CustomStringDeserializer());
}

谢谢@nutlike
我是这样做的

    @Override
public String getNullValue() {
    return "";
}

最佳答案 也许覆盖方法
getNullValue()就足够了?

public class CustomStringDeserializer extends JsonDeserializer<String> {

@Override
public String deserialize(JsonParser jsonparser,
        DeserializationContext deserializationcontext) throws IOException,
        JsonProcessingException {

    return jsonparser.getText();

}

@Override
public String getNullValue() {
    return "";
}

}
点赞