Do you always need an ObjectFactory class when using JAXB?

前端 未结 3 1626
后悔当初
后悔当初 2020-12-24 09:05

Do you always need an ObjectFactory class when using JAXB?

Without it I get this exception:

javax.xml.bind.JAXBException: \"com.a.b.c\"

相关标签:
3条回答
  • 2020-12-24 09:24

    I was using Spring and I just had to change

    Jaxb2Marshaller mlr = new Jaxb2Marshaller();
    mlr.setContextPaths("","");
    

    to

    Jaxb2Marshaller mlr = new Jaxb2Marshaller();
    mlr.setPackagesToScan("","");
    
    0 讨论(0)
  • 2020-12-24 09:28

    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.

    0 讨论(0)
  • 2020-12-24 09:28

    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:

    • http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html
    0 讨论(0)
提交回复
热议问题