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

后端 未结 8 1273
再見小時候
再見小時候 2021-02-02 08:11

I am trying to create a sample test application which converts an object to JaxbRepresentation. But when I try to run this, it gives me an error.

Main.java file

8条回答
  •  既然无缘
    2021-02-02 09:07

    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());
    

提交回复
热议问题