JaxbRepresentation gives error “doesnt contain ObjectFactory.class or jaxb.index”

走远了吗. 提交于 2019-12-02 20:14:39

To get rid of additional jaxb.index files you may use Java class to instantiate the context:

http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html#newInstance(java.lang.Class...)

Usually you need to pass in only single java class because other classes are "statically reachable from these class(es)" so JAXB is able to identify them.

In my case I was able to resolve this by adding a file called "jaxb.index" in the same package folder as the JAXB annotated class. In that file list the simple, non-qualified names of the annotated classes.

For example, my file /MyProject/src/main/java/com/example/services/types/jaxb.index is simply one line (since I have only one JAXB typed class):

ServerDiagContent

which refers to the class com.example.services.types.ServerDiagContent

I got this error because of a ClassLoader issue, and I was able to solve it by explicitly passing the ClassLoader that JAXB should use, so this:

JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName());

gave an error, but worked properly when using:

JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName(),
                        com.myexample.test.ObjectFactory.class.getClassLoader());

which is probably similar to user3243752's answer, I bet that JAXB is automatically choosing the ClassLoader from the passed in class when using the #newInstance(Class... classesToBeBound) method signature.

In my case I was able to solve the problem by changing the instantiation of the JAXBContext. It can either be instantiated with the package name or the ObjectFactory class as a parameter.

When instantiating with the package name:

com.myexample.test.ObjectFactory objectFactory = new com.myexample.test.ObjectFactory();
JAXBContext jaxbContext = JAXBContext.newInstance(objectFactoryMessageBody.getClass().getPackage().getName());

It gave the error:

"com.myexample.test" doesnt contain ObjectFactory.class or jaxb.index

No errors when instantiating with the class name:

com.myexample.test.ObjectFactory objectFactory = new com.myexample.test.ObjectFactory();
JAXBContext jaxbContext = JAXBContext.newInstance(objectFactoryMessageBody.getClass());

I was using Spring and I just had to change

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

to

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

Ref1 & Ref2

If you have an instantiation of your object factory like

private ObjectFactory of;

..then the safest, most reliable way to get a Context to Marshall with is:

  JAXBElement<GreetingListType> gl = of.createGreetings( grList );
  JAXBContext jc = JAXBContext.newInstance(of.getClass());
  Marshaller m = jc.createMarshaller();

Use the complete package name of the where the ObjectFactory class is present to declare and instantiate the Object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!