1

So as the title implies, Im working in a project in which after I call an API, this API will setup an Web-Service call, after the API calls the WS then it will send back (to my own app) the response. I need to log the response as XML. So after some searching I endup with code:

 Marshaller marshaller = JAXBContext.newInstance(
            vhu.ReqDeclareObject.class
    ).createMarshaller();

My App <-> API <-> WS

POM (dependency containing the VHU package):

 <dependency>
            <groupId>com.some.api</groupId>
            <artifactId>api</artifactId>
            <version>1.1.1-SNAPSHOT</version>
 </dependency>

The API has the JAXB generated classes (in VHU package) which I need to be able to marshal in my own project.

The problem is the the JAXB generated classes are in a dependency (com.some.dependency:vhu). And when I run the code Im getting this error:

vhu.ReqDeclaredObject$JaxbAccessorF_control cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor

I did a simple test trying to understand the error. I copy all the JAXB generated classes into my project and run the same code as above but this time the packaged is in my own project.

  Marshaller marshaller = JAXBContext.newInstance(
            ReqDeclareObject.class
    ).createMarshaller();

Here the application runs good.

Coping all the JAXB generated classes into the project is no viable long term solution. I need to be able to create a marshaller without bring all the classes into the project.

Thank you for your help.

Bruno Miguel
  • 1,003
  • 3
  • 13
  • 26

2 Answers2

0

I have not worked much on JAXB in particular but have a fair understanding of MOXY which is built on JAXB implementations.

  1. With classes are in dependency you mean Class A extends Class B and you are trying to marshal Class A using the JAXB?

  2. Have you tried passing the package itself to the marshaler instance? something like this?

Marshaller marshaller = JAXBContext.newInstance("io.model.jaxb").createMarshaller();

With this, you need the ObjectFactory. If interested please refer here: https://stackoverflow.com/a/5780252/16055533

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1 - I have improved the question so you and others can understand the problem better, 2- That's what Im trying to do in the example – Bruno Miguel May 31 '21 at 08:58
0

Finally I found a solution, its not mine but Im sharing here for others with same problem.

The thread doesnt seems to much related to the problem but there is an answer which solved my problem.

What is the ObjectFactory role during JAXB-Unmarshalling?

In my case I just added to the VM Environment options the property stated in the link.

Bruno Miguel
  • 1,003
  • 3
  • 13
  • 26