What is the ObjectFactory role during JAXB-Unmarshalling?(JAXB-Unmarshalling 期间的 ObjectFactory 角色是什么?)
问题描述
我正在使用 JAXB 2.2.2 来解析一个简单的 XML-REST 流.这是一段代码:
I'm using JAXB 2.2.2 to parse a simple XML-REST stream. This is the piece of code:
JAXBContext jc = JAXBContext.newInstance( "com.example.entities" );
Unmarshaller u = jc.createUnmarshaller();
r = (Response )u.unmarshal( inputStream );
ObjectFactory 类:
ObjectFactory class:
@XmlRegistry
public class ObjectFactory {
public Response createRsp() {
return new Response();
}
}
响应类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="rsp")
@XmlType
public class Response { ... }
com.example.entities"必须包含 ObjectFactory 类或 jaxb.index.我想使用 ObjectFactory 类来决定一些 pojo 初始化,但这些类从未使用过:Response 类总是由 class.newInstance() 直接实例化.这有什么问题吗?
The "com.example.entities" must contain the ObjectFactory class or jaxb.index. I would like to use the ObjectFactory class in order to decide some pojo initialization, but these class is never used: the Response class is always instantiated by class.newInstance() directly. Is there something wrong in this?
推荐答案
您可以利用 @XmlType
注解来控制对象的创建方式:
You can leverage the @XmlType
annotation to control how the objects are created:
@XmlType(factoryClass=ObjectFactory.class, factoryMethod="createRsp")
public class Response {
}
更多信息
- http://bdoughan.blogspot.com/2011/06/jaxb-and-factory-methods.html
这篇关于JAXB-Unmarshalling 期间的 ObjectFactory 角色是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JAXB-Unmarshalling 期间的 ObjectFactory 角色是什么?


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