Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in marshalling - missing xmlrootelement annotation error

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);
like image 636
chappalprasad Avatar asked Oct 15 '16 13:10

chappalprasad


People also ask

What is XmlRootElement?

@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.

What is marshalling in XML?

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.

What is marshalling in JAXB?

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.

What is JAXB marshalling and Unmarshalling?

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.


1 Answers

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");
                    }
                }));        
like image 103
chappalprasad Avatar answered Sep 29 '22 07:09

chappalprasad