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 角色是什么?
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
