简体   繁体   中英

JAXB Marshal exception: .Impl class nor any of its super class is not known to any of this context

I am encountering a problem while marshalling my Java objects using JAXB. When i run the web service as an independent application, it works fine.

When i Integrate it with my project, and use the generated Java classes, while marshalling, I get an exception

RequestImpl class nor any of its super class is not known to any of this context.

While generating my JAXB instance I use the following statement:

JAXBContext jaxb = JAXBContext.newInstance("com.eos.hotels.API.Request");

I have 2 separate xsds, one for Request and another for Response .

The generated java classes are stored in a folder structure as follows:

API->Request
API->Response

In both the above folders, an ObjectFactory.java is created and jaxb.properties is also there.

Also, inside each of the above mentioned folders, impl folder is created which has all Impl files, and inside this impl folder, runtime folder is created.

All the xs:element from XSD are created as interfaces, only the Impls are classes. Also, there are no XML annotations in the generated java files.

Can anyone tell me what is going wrong here?

Is this problem happening because of folder structure or because the XML annotations are not present?

I read on the internet that annotations if not present also are not a problem.

I have even tried to copy the generated impl classes in the same folder as ObjectFactory.java , but that also did not work.

Try to call toString method on your JAXBContext object. Because you are using JAXB1 classes it should display somethig like: com.eos.hotels.API.impl.runtime.DefaultJAXBContextImpl. If it's not - you probably don't have jaxb.properties file in com.eos.hotels.API package or you've deleted com.eos.hotels.API.impl.runtime package.

在从多个目录生成的类上创建JAXBContext时,需要包括所有用冒号':'分隔的软件包名称。

 JAXBContext jaxb = JAXBContext.newInstance("com.example.package1:org.example.package2");

So you're using jaxb1, and generating your jaxb classes from 2 distinct XSD's, hopefully declaring two separate namespace URIs (otherwise that will mess up JAXB).

First, I would consider combining the two XSD's, and having a single namespace URI. You can still use the same package structure, or end up with a single one, but more importantly will end up with a single runtime folder. Also, you will end up with a single ObjectFactory and that will prevent having to use FQCN (fully-qualified classnames) when using both ObjectFactory classes from the the same class (that would make your import statements usless, which is a trap that could be at play here also).

When you build you JAXBContext, make sure to list all the root packages (not the impl ones), and you should be good to go.

Try this and let us know what you get. Good luck.

While creating the JAxbContext I used the following:

JAXBContext jaxb = new DefaultJAXBContextImpl(packagename, ObjectFactory.class                 .getClassLoader());

Though I am not sure why, it seems to be working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM