如何编组或解组JAXBElement <string>请求或响应

时间:2019-05-07 15:48:01

标签: marshalling spring-ws jaxbelement

我正在设置一个spring-ws Web服务客户端,请求或响应的类型是JAXBElemet。我该如何配置编组器?

我使用.xsd文件生成动态wsdl。 .xsd文件如下:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:tns="http://com.bytedusk.dev/spring-seed/ws/echo"
        targetNamespace="http://com.bytedusk.dev/spring-seed/ws/echo"
        elementFormDefault="qualified">
<xs:element name="EchoRequest" type="xs:string"></xs:element>
<xs:element name="EchoResponse" type="xs:string"></xs:element>

我的XML代码:

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />
<!-- <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
</bean> -->
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <constructor-arg ref="messageFactory" />
    <property name="marshaller" ref="marshaller"></property>
    <property name="unmarshaller" ref="marshaller"></property>
    <property name="messageSender">
        <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender"></bean>
    </property>
    <property name="defaultUri" value="http://${webservice.ip}:${webservice.port}/${maven.app-name}/ws/echo" />
</bean>

我的客户代码:

@Controller
public class MainController {

   @Autowired
   private WebServiceTemplate webServiceTemplate;

   @Autowired
   private JAXBElement<String> requestWSBean;

   @RequestMapping(value="/RequestWS")
   @ResponseBody
   public String RequestWS() throws JAXBException{
      requestWSBean.setValue("Request From SpringWS Template Client");
      JAXBElement<String> response = (JAXBElement<String>) 
            webServiceTemplate.marshalSendAndReceive(requestWSBean);
      return response.getValue();
   }
}

0 个答案:

没有答案