Per eseguire custom serialization in Jackson, è possibile register a module con un BeanSerializerModifier
che specifica le modifiche necessarie. Nel tuo caso, il BeanPropertyWriter.serializeAsField
è il metodo responsabile della serializzazione di singoli campi, quindi devi creare il tuo BeanPropertyWriter
che ignori le eccezioni sulla serializzazione del campo e registrare un modulo conper sostituire tutte le istanze predefinite BeanPropertyWriter
con la tua implementazione. L'esempio seguente mostra:
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";
}
}
}
il codice precedente restituisce il seguente, utilizzando Jackson 2.7.1 e Java 1.8:
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"}
dimostrare che getProblematicProperty
, che genera IllegalStateException
, verrà omessa dalla serializzato valore.
È possibile utilizzare la funzione try catch? –
Vorrei studiare la configurazione del tuo object mapper con un 'JsonGenerator' personalizzato, possibilmente estendendo e modificando [l'implementazione predefinita] (http://fasterxml.github.io/jackson-core/javadoc/2.4/com/fasterxml/ jackson/core/json/JsonGeneratorImpl.html) per ignorare/gestire le eccezioni in modo diverso. – Vulcan