Do you always need an ObjectFactory class when usi

2019-03-11 01:13发布

Do you always need an ObjectFactory class when using JAXB?

Without it I get this exception:

javax.xml.bind.JAXBException: "com.a.b.c" doesnt contain ObjectFactory.class or jaxb.index

I gather the ObjectFactory can be overkill. But given this exception I'm guessing you need it.. but not sure why?

标签: java jaxb
2条回答
仙女界的扛把子
2楼-- · 2019-03-11 01:40

Instead of the ObjectFactory you can include a jaxb.index file which is a text file that contains a new line seperated list of Java classes.

For an example of using a jaxb.index file see:

查看更多
对你真心纯属浪费
3楼-- · 2019-03-11 01:53

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.

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.

查看更多
登录 后发表回答