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

查看:1409
本文介绍了使用JAXB时是否总是需要ObjectFactory类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JAXB时是否总是需要ObjectFactory类?

Do you always need an ObjectFactory class when using JAXB?

没有它我得到这个例外:

Without it I get this exception:


javax.xml.bind。 JAXBException:
com.abc
不包含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天全站免登陆