Raw XML request using spring webservice template

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

wsTemplate.marshalSendAndReceive (requestPayload) will give me an unmarshalled pojo object as output, but I need raw xml in response to the webservice call.

Another request, if I accept the answer as raw xml, I still need to define the 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 the error as being unable to marshal the type "com.abc.xyz.GetAbc" as an element because the @XmlRootElement annotation is missing] )

Any help.

+4
source share
1 answer

It may be too late for you, but you can use to translate the POJO back into XML

 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 an org.springframework.xml.transform package.

To generate POJOs from WSDL, I use the 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 is the binding.xml file

 <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> 
+4
source

Source: https://habr.com/ru/post/1487080/


All Articles