gpt4 book ai didi

java - 如何包装 jackson 序列化异常

转载 作者:行者123 更新时间:2023-11-30 05:28:11 24 4
gpt4 key购买 nike

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

示例:

public class Example {
public String getSomeField() {
//some logic which will throw in example NPE
throw new NullPointerException();
}
}

理想情况下我想获得JSON:

{"someField":"null"}

{"someField":"NPE"}

最佳答案

最通用的方法可能是实现自定义 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 {

public static void main(String[] args) throws Exception {
SimpleModule module = new SimpleModule();
module.setSerializerModifier(new BeanSerializerModifier() {
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
if (beanDesc.getBeanClass() == Response.class) {
return beanProperties.stream()
.map(SilentExceptionBeanPropertyWriter::new)
.collect(Collectors.toList());

}

return super.changeProperties(config, beanDesc, beanProperties);
}
});

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);


System.out.println(mapper.writeValueAsString(new Response(1, "ONE")));
System.out.println(mapper.writeValueAsString(new Response(-1, "MINUS")));
System.out.println(mapper.writeValueAsString(new Response(-1, null)));
}
}

class SilentExceptionBeanPropertyWriter extends BeanPropertyWriter {

public SilentExceptionBeanPropertyWriter(BeanPropertyWriter base) {
super(base);
}

@Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
try {
super.serializeAsField(bean, gen, prov);
} catch (Exception e) {
Throwable cause = e.getCause();
gen.writeFieldName(_name);
gen.writeString(cause.getClass().getName() + ":" + cause.getMessage());
}
}
}


class Response {

private int count;
private String message;

public Response(int count, String message) {
this.count = count;
this.message = message;
}

public int getCount() {
if (count < 0) {
throw new IllegalStateException("Count is less than ZERO!");
}
return count;
}

public void setCount(int count) {
this.count = count;
}

public String getMessage() {
if (message == null) {
throw new NullPointerException("message can not be null!");
}
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

以上示例打印:

{"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!"}

关于java - 如何包装 jackson 序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58146134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com