如何包装杰克逊序列化异常

Jackson如果其中一个 getter 抛出异常,如何序列化对象?


例子:


public class Example {

     public String getSomeField() {

          //some logic which will throw in example NPE

          throw new NullPointerException();

     }

}

理想情况下我想得到JSON:


{"someField":"null"}

或者


{"someField":"NPE"}


一只萌萌小番薯
浏览 90回答 1
1回答

倚天杖

最通用的方法可能是实现 custom BeanPropertyWriter。您可以通过创建类来注册它BeanSerializerModifier。下面的示例展示了如何做到这一点。import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.BeanDescription;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationConfig;import com.fasterxml.jackson.databind.SerializerProvider;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;import java.util.List;import java.util.stream.Collectors;public class JsonApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; SimpleModule module = new SimpleModule();&nbsp; &nbsp; &nbsp; &nbsp; module.setSerializerModifier(new BeanSerializerModifier() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (beanDesc.getBeanClass() == Response.class) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return beanProperties.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(SilentExceptionBeanPropertyWriter::new)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return super.changeProperties(config, beanDesc, beanProperties);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; mapper.registerModule(module);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(new Response(1, "ONE")));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(new Response(-1, "MINUS")));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(new Response(-1, null)));&nbsp; &nbsp; }}class SilentExceptionBeanPropertyWriter extends BeanPropertyWriter {&nbsp; &nbsp; public SilentExceptionBeanPropertyWriter(BeanPropertyWriter base) {&nbsp; &nbsp; &nbsp; &nbsp; super(base);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.serializeAsField(bean, gen, prov);&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Throwable cause = e.getCause();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gen.writeFieldName(_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gen.writeString(cause.getClass().getName() + ":" + cause.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}class Response {&nbsp; &nbsp; private int count;&nbsp; &nbsp; private String message;&nbsp; &nbsp; public Response(int count, String message) {&nbsp; &nbsp; &nbsp; &nbsp; this.count = count;&nbsp; &nbsp; &nbsp; &nbsp; this.message = message;&nbsp; &nbsp; }&nbsp; &nbsp; public int getCount() {&nbsp; &nbsp; &nbsp; &nbsp; if (count < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException("Count is less than ZERO!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCount(int count) {&nbsp; &nbsp; &nbsp; &nbsp; this.count = count;&nbsp; &nbsp; }&nbsp; &nbsp; public String getMessage() {&nbsp; &nbsp; &nbsp; &nbsp; if (message == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NullPointerException("message can not be null!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return message;&nbsp; &nbsp; }&nbsp; &nbsp; public void setMessage(String message) {&nbsp; &nbsp; &nbsp; &nbsp; this.message = message;&nbsp; &nbsp; }}上面的例子打印:{"count":1,"message":"ONE"}{"count":"java.lang.IllegalStateException:Count is less than ZERO!","message":"MINUS"}{"count":"java.lang.IllegalStateException:Count is less than ZERO!","message":"java.lang.NullPointerException:message can not be null!"}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java