我正在编写一个客户端,使用webserviceTemplate(spring-ws)调用SOAP webservice。我正在使用JAXB将wsdl转换为POJO。我将请求作为POJO发送到webservice但是想要xml格式的响应(原始xml而不是unmarshalled pojo格式)。
wsTemplate.marshalSendAndReceive(requestPayload)会给我作为输出的unmarshalled pojo对象,但是我需要原始xml来响应webservice调用。
还有一个查询,如果我将响应作为原始xml,我仍然需要将marshaller bean定义为:
<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>
(当我定义这个marshaller时,我得到错误,因为 无法编组类型“com.abc.xyz.GetAbc”作为元素,因为它缺少@XmlRootElement注释] )
任何帮助。
答案 0 :(得分:4)
对你来说可能为时已晚,但要将POJO编组回XML,可以使用
final Result marshallerResult = new StringResult();
marshaller.marshal(input, marshallerResult);
其中marshaller
是您在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
org.springframework.xml.transform
来自<!-- 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) -->
包。
从WSDL生成POJO我正在使用maven插件
binding.xml
其中<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>
文件是
{{1}}