How do you serialize an Exception subclass in JAXB?(如何在 JAXB 中序列化 Exception 子类?)
问题描述
如何序列化 Exception 的子类?
How do you serialize a subclass of Exception?
这是我的例外:
@XmlType
public static class ValidationFault extends Exception {
public ValidationFault() {
}
}
我已经尝试过使用 @XmlTransient 和 @XmlAccessorType 的各种变体,但 JAXB 不断尝试序列化 getStackTrace/setStackTrace 对,但这是无法完成的.
I have tried all sorts of variations on using @XmlTransient and @XmlAccessorType, but JAXB continually tries to serialize the getStackTrace/setStackTrace pair, which can't be done.
如何告诉 JAXB 忽略父级上的所有字段?
How do I tell JAXB to ignore all the fields on the parent?
推荐答案
我通过以下信息解决了这个问题:http://forums.java.net/jive/thread.jspa?messageID=256122
I solved this with the information at: http://forums.java.net/jive/thread.jspa?messageID=256122
您需要使用以下配置初始化您的 JAXBContext(其中 jaxbObj 是要序列化的对象):
You need to initialize your JAXBContext with the following config (where jaxbObj is the object to serialize):
Map<String, Object> jaxbConfig = new HashMap<String, Object>();
// initialize our custom reader
TransientAnnotationReader reader = new TransientAnnotationReader();
try {
reader.addTransientField(Throwable.class.getDeclaredField("stackTrace"));
reader.addTransientMethod(Throwable.class.getDeclaredMethod("getStackTrace"));
} catch (SecurityException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
jaxbConfig.put(JAXBRIContext.ANNOTATION_READER, reader);
JAXBContext jc = JAXBContext.newInstance(new Class[] {jaxbObj.getClass()},jaxbConfig);
这篇关于如何在 JAXB 中序列化 Exception 子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JAXB 中序列化 Exception 子类?


- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01