使用 JAXB 时是否总是需要 ObjectFactory 类? [英] Do you always need an ObjectFactory class when using JAXB?

查看:64
本文介绍了使用 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.in​​dex

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.

这篇关于使用 JAXB 时是否总是需要 ObjectFactory 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆