Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raw xml response using spring webservice template

Tags:

spring

I am writing a client to call SOAP webservice using webserviceTemplate (spring-ws). I am using JAXB to convert wsdl to POJO. I am sending request as POJO to webservice however want the response in xml format (raw xml instead of unmarshalled pojo format).

wsTemplate.marshalSendAndReceive(requestPayload) would give me unmarshalled pojo object as output however I need raw xml in response from webservice call.

One more query, if I am taking response as raw xml, Do i still need to define marshaller bean as :

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.abc.xyz" />
</bean>

(when i define this marshaller I get error as unable to marshal type "com.abc.xyz.GetAbc" as an element because it is missing an @XmlRootElement annotation])

Any help.

like image 871
Manglesh Avatar asked Jun 19 '13 15:06

Manglesh


1 Answers

Maybe too late for you, but to marshal the POJO back to XML one can use

final Result marshallerResult = new StringResult();
marshaller.marshal(input, marshallerResult);

where marshaller is the one you used in wsTemplate.

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="contextPath" value="com.dhl.dctservice" />
</bean>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">     
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="marshaller" />
</bean>

StringResult is from org.springframework.xml.transform package.

To generate POJOs from WSDL I'm using maven plugin

<!-- WSDL -> Java (start) -->
<plugin>
    <!-- !!! READ !!! -->
    <!-- mvn cxf-codegen-plugin:wsdl2java NOT working, comment phase and
    run "mvn clean install -DskipTests") -->
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${cxf-codegen-plugin.version}</version>
    <executions>
        <execution>
            <id>wsdl2java</id>
            <!-- comment this to generate java classes from wsdl during the "mvn
            clean install -DskipTests" -->
            <phase>manual-generate-sources</phase>
            <configuration>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>${basedir}/src/main/resources/wsdl/service.wsdl</wsdl>
                        <extraargs>
                            <extraarg>-verbose</extraarg>
                            <extraarg>-b</extraarg>
                            <extraarg>${basedir}/src/main/resources/wsdl/bindings.xml</extraarg>
                            <extraarg>-client</extraarg>
                            <extraarg>-xjc-Xts</extraarg>
                        </extraargs>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>stax2-api</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-xjc-ts</artifactId>
            <version>2.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-jaxb-xjc</artifactId>
            <version>2.1.13</version>
        </dependency>
    </dependencies>
</plugin>
<!-- WSDL -> Java (end) -->

where binding.xml file is

<jaxb:bindings
    version="2.1" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <jaxb:globalBindings generateElementProperty="false">
        <xjc:simple />
    </jaxb:globalBindings>

</jaxb:bindings>
like image 139
Betlista Avatar answered Nov 15 '22 09:11

Betlista