Java 如果getter抛出异常,如何使Jackson忽略属性

Java 如果getter抛出异常,如何使Jackson忽略属性,java,jackson,Java,Jackson,我有许多来自供应商的类,它们喜欢在属性访问上随机抛出RuntimeException public Object getSomeProperty() { if (!someObscureStateCheck()) { throw new IllegalStateExcepion(); } return calculateTheValueOfProperty(someRandomState); } 我不能更改类,不能添加注释,而且为每个类定义混合是不现实的

我有许多来自供应商的类,它们喜欢在属性访问上随机抛出RuntimeException

public Object getSomeProperty() {
    if (!someObscureStateCheck()) {
        throw new IllegalStateExcepion();
    }
    return calculateTheValueOfProperty(someRandomState);
}
我不能更改类,不能添加注释,而且为每个类定义混合是不现实的,因为堆栈的这部分经常更改

如果属性的getter抛出异常,如何使Jackson忽略该属性?

要在Jackson中执行,可以使用指定所需修改的。在您的情况下,是负责序列化单个字段的方法,因此您应该创建自己的
BeanPropertyWriter
,忽略字段序列化的异常,并使用
BeanSerializerModifier
注册一个模块,该模块用于用您自己的实现替换所有默认的
BeanPropertyWriter
实例。以下示例演示:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class JacksonIgnorePropertySerializationExceptions {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {
            @Override
            public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
                return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {
                    @Override
                    public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
                        try {
                            super.serializeAsField(bean, gen, prov);
                        } catch (Exception e) {
                            System.out.println(String.format("ignoring %s for field '%s' of %s instance", e.getClass().getName(), this.getName(), bean.getClass().getName()));
                        }
                    }
                }).collect(Collectors.toList());
            }
        }));

        mapper.writeValue(System.out, new VendorClass());
    }

    public static class VendorClass {
        public String getNormalProperty() {
            return "this is a normal getter";
        }

        public Object getProblematicProperty() {
            throw new IllegalStateException("this getter throws an exception");
        }

        public String getAnotherNormalProperty() {
            return "this is a another normal getter";
        }
    }
}

显示将从序列化值中忽略引发非法状态异常的
getProblematicProperty

是否可以使用try-catch?我将研究使用自定义
JsonGenerator
设置对象映射器,可能通过扩展和修改以不同方式忽略/处理异常。
ignoring java.lang.reflect.InvocationTargetException for field 'problematicProperty' of JacksonIgnorePropertySerializationExceptions$VendorClass instance
{"normalProperty":"this is a normal getter","anotherNormalProperty":"this is a another normal getter"}