[英]Java Web Services framework
我有以下情形:我必須實現Web服務客戶端,該客戶端將使用各種wsdl-s為不同的Web服務提供者提供服務。 即使是同一提供程序,也可能具有我必須支持的不同版本的wsdl-s。 我們在Spring中使用Java,因此我在考慮spring-ws框架。 但是似乎,作為實現的一部分,我必須將wsdl導入到我的項目中,並根據基於import pojo-s的created編寫代碼。 因此,對於不同的提供程序,使用不同的代碼不會有問題,因為邏輯是不同的。 但是我更喜歡為同一提供者的不同版本使用相同的代碼,並避免創建不同版本的pojo-s。 因此,我正在尋找類似於python的suds庫的東西。 在那里,您只需提供特定的wsdl並進行呼叫。 您無需處理wsdl導入和同一提供程序的不同版本的wsdl-只需在需要時調整ws調用的參數列表。 最好是,如果spring-ws可以做類似的事情-也許我只是沒有意識到,因為現在我們正在嘗試擺脫Axis和Axis-2。
您是否看過webServiceTemplate ? 您有很多可能性,並且不需要每次都導入wsdl。 例如,您可以使用如下字符串:
StreamSource source = new StreamSource(new StringReader(....));
ByteArrayOutputStream bytArrayOutputStream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bytArrayOutputStream);
wsTemplate.sendSourceAndReceiveToResult(source, result);
final String reply = new String(_bytArrayOutputStream.toByteArray());
但是,從長遠來看,這種解決方案是不合適的。 我會建議使用像這樣的pojos
1)在彈簧配置中,
<bean id="wsTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri" value="...."/>
<property name="marshaller" ref="marshaller"/>
<property name="unmarshaller" ref="marshaller"/>
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.mycompany.app.ws"/>
</bean>
2)創建包com.mycompany.app.ws並添加包信息
@javax.xml.bind.annotation.XmlSchema(
namespace = "....")
package com.mycompany.app.ws;
3)在包com.mycompany.app.ws中,您將需要定義請求和響應。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {})
@XmlRootElement(name = "WsRequest")
public final class WsRequest{
@XmlElement(required = true)
private String requstData;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {})
@XmlRootElement(name = "WsResponse")
public final class WsResponse {
@XmlElement(required = true)
private String responseData;
}
4)在您的代碼中執行以下操作:
WsResponse wsResponse = (WsResponse )
wsTemplate.marshalSendAndReceive(wsRequest_);
如果您需要添加新的wsdl或新的提供程序,會發生什么?
我還認為您可以創建一個工廠來使用適當的編組器初始化wsTemplate。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.