When i call one of the WSDL operation from a spring project, i am getting following exception -
com.sun.istack.internal.SAXException2: unable to marshal type "com.pkg.wsdl.ABC" as an element because it is missing an @XmlRootElement annotation
I am using following in pom.xml to generate java objects from a WSDL(already used by many clients) as part of a spring project -
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.13.1</version>
Looking at similar issue resolution i changed the code to use JAXBElement but still getting same error -
ABC vabc = new ABC();
vabc.set(..) // populate vabc object
ObjectFactory of = new ObjectFactory();
JAXBElement<ABC> jabc = of.createABC(vabc);
ABC oabc = jabc .getValue();
Marshaller Code -
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.pkg.wsdl");
and Calling the backend Web Service -
ABCResp response = (ABCResp) getWebServiceTemplate()
.marshalSendAndReceive("http://host:port/svcname",oabc);
@XmlRootElement is an annotation that people are used to using with JAXB (JSR-222). It's purpose is to uniquely associate a root element with a class. Since JAXB classes map to complex types, it is possible for a class to correspond to multiple root elements.
Object/XML Mapping, or O/X mapping for short, is the act of converting an XML document to and from an object. This conversion process is also known as XML Marshalling, or XML Serialization.
The JAXB Marshaller interface is responsible for governing the process of serializing Java content trees i.e. Java objects to XML data. This marshalling to XML can be done to variety of output targets.
JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API.
Had following issues that i had to solve -
1- missing xmlRootElement annotation error
had to pass JAXBElement itself in marshalSendAndReceive as shown below.
You can pull the exact details from ObjectFactory for a QName.
2- missing soapAction in the request error
had to pass WebServiceMessageCallback function as shown below to set soapAction
3- classCastExcetion unmarshalling the response
had to add JAXBIntrospector to fix this error
ABCResp response = (ABCResp ) JAXBIntrospector.getValue(getWebServiceTemplate()
.marshalSendAndReceive(
"http://host:port/svcname",
new JAXBElement<ABC>(new QName(uri, localpart),ABC.class,request),
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
((SoapMessage)message).setSoapAction("/test");
}
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With