Java Jaxb:如何生成 ObjectFactory 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6442312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jaxb: How do I generate ObjectFactory class?
提问by Dave
I'm using Java 6, JaxB 2 and SpringSource Tool Suite (same as Eclipse). I had a couple of Java classes I wrote, from which I used JaxB to generate an XML schema. However, I'm noticing in order to use JaxB's ability to generate an XML document from Java objects, I need an ObjectFactory.
我正在使用 Java 6、JaxB 2 和 SpringSource Tool Suite(与 Eclipse 相同)。我编写了几个 Java 类,从中我使用 JaxB 生成了一个 XML 模式。但是,我注意到为了使用 JaxB 从 Java 对象生成 XML 文档的能力,我需要一个 ObjectFactory。
final Marshaller marshaller = jaxbContext.createMarshaller();
// Here is where I don't have an ObjectFactory defined
final JAXBElement<WebLeads> webLeadsElement
= (new ObjectFactory()).createWebLeads(webLeadsJavaObj);
How can I generate an ObjectFactory without blowing away the classes I already have now?
如何在不破坏我现在已有的类的情况下生成 ObjectFactory?
采纳答案by bdoughan
UPDATE
更新
This question may be referring to the role of ObjectFactory
in creating a JAXBContext
. If you bootstrap a JAXBContext
on a context path then it will check for an ObjectFactory in that location in order to determine the classes in that package:
这个问题可能是指ObjectFactory
在创建JAXBContext
. 如果您JAXBContext
在上下文路径上引导 a ,那么它将检查该位置的 ObjectFactory 以确定该包中的类:
If you do not have an ObjectFactory
but still wish to create you JAXBContext
on a context path you can include a file called jaxb.index
in that package listing files to be included in the JAXBContext
(referenced classes will automatically pulled in):
如果您没有ObjectFactory
但仍希望JAXBContext
在上下文路径上创建您,您可以包含一个jaxb.index
在该包列表文件中调用的文件以包含在JAXBContext
(引用的类将自动引入):
Alternatively you can bootstrap you JAXBContext
on an array of classes instead of a context path:
或者,您可以JAXBContext
在一组类而不是上下文路径上引导您:
Is ObjectFactory Required
是否需要 ObjectFactory
An ObjectFactory
is not required, although even when starting from Java classes there are use cases where you can leverage a similar class annotated with @XmlRegistry
in order to use the @XmlElementDecl
annotation.
AnObjectFactory
不是必需的,尽管即使从 Java 类开始,也有一些用例可以利用注释的类似类@XmlRegistry
来使用@XmlElementDecl
注释。
Creating an Instance of JAXBElement
创建 JAXBElement 的实例
You can always create the JAXBElement
directly:
您始终可以JAXBElement
直接创建:
final JAXBElement<WebLeads> webLeadsElement = new JAXBElement<WebLeads>(
new QName("root-element-name"),
WebLeads.class,
webLeadsJavaObj);
Alternative to JAXBElement
JAXBElement 的替代品
Or since JAXBElement is simply used to provide root element information, you can annotate your WebLeads
class with @XmlRootElement
:
或者由于 JAXBElement 仅用于提供根元素信息,因此您可以使用以下内容注释您的WebLeads
类@XmlRootElement
:
@XmlRootElement(name="root-element-name")
public class WebLeads {
...
}
回答by Puce
I don't think you need an ObjectFactory.
我认为您不需要 ObjectFactory。
It's just a utility class XJC generates to make life easier in some cases.
它只是 XJC 生成的一个实用程序类,用于在某些情况下使生活更轻松。
Edit: Reading your question, I guess you created the POJOs with JAXB annotations by hand.
编辑:阅读您的问题,我猜您是手动创建了带有 JAXB 注释的 POJO。
Consider to add the XmlRootElement on the "root" class: http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlRootElement.html
考虑在“root”类上添加 XmlRootElement:http: //download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlRootElement.html
Here some more info: No @XmlRootElement generated by JAXB
这里有更多信息: JAXB 没有生成 @XmlRootElement
回答by TravMan
You don't 'need' a factory for the JaxB marshaller to function. If you pass it an object with a list or a map variable, it will in fact marshall it correctly. This is of course true only if you've correctly initilized the JaxB marshaller towards the object's class that you want to marshall.
您不需要为 JaxB 编组器运行的工厂。如果您将一个带有列表或映射变量的对象传递给它,它实际上会正确地编组它。当然,只有当您正确地将 JaxB 编组器初始化为要编组的对象类时,这才是正确的。
You can create a factory, and that factory can create some specialized return (say you don't want it to return your public temp variables)
您可以创建一个工厂,该工厂可以创建一些专门的返回值(假设您不希望它返回您的公共临时变量)