使用 JAXB 时是否总是需要 ObjectFactory 类?
在使用 JAXB 时是否总是需要 ObjectFactory 类?
Do you always need an ObjectFactory class when using JAXB?
没有它我得到这个例外:
Without it I get this exception:
javax.xml.bind.JAXB 异常:com.a.b.c"不包含 ObjectFactory.class 或jaxb.index
javax.xml.bind.JAXBException: "com.a.b.c" doesnt contain ObjectFactory.class or jaxb.index
我收集 ObjectFactory 可能有点矫枉过正.但考虑到这个例外,我猜你需要它..但不知道为什么?
I gather the ObjectFactory can be overkill. But given this exception I'm guessing you need it.. but not sure why?
当您使用 JAXBContext.newInstance(String)
工厂方法时,您会遇到该异常,您将包名称作为争论.这确实需要 ObjectFactory
存在,否则 JAXB 不知道要处理哪些类.
You get that exception when you use the JAXBContext.newInstance(String)
factory method, where you pass in the package name as the argument. This does require the ObjectFactory
to be there, otherwise, JAXB doesn't know which classes to process.
如果您没有 ObjectFactory
,则需要 JAXBContext.newInstance(Class...)
代替,传入带注释的类的显式列表以添加到上下文.
If you don't have an ObjectFactory
, you need to JAXBContext.newInstance(Class...)
instead, passing in the explicit list of annotated classes to add to the context.