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 角色是什么?


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01