對不起,我問了兩次相同的問題。如何避免重建客戶端存根(JAX-WS)web服務重定位到另一個位置?
我部署了JAX-WS Web服務,並從我的客戶端代碼中使用它。 我的要求是, 如何避免構建我的客戶端代碼(存根),同時將我的JAX-WS Web服務從一個地方遷移到另一個地方?
謝謝。
對不起,我問了兩次相同的問題。如何避免重建客戶端存根(JAX-WS)web服務重定位到另一個位置?
我部署了JAX-WS Web服務,並從我的客戶端代碼中使用它。 我的要求是, 如何避免構建我的客戶端代碼(存根),同時將我的JAX-WS Web服務從一個地方遷移到另一個地方?
謝謝。
如果你正在使用Spring,你可以配置客戶端(這是我在最近的一個項目完成)創建一個幫助豆:
<bean name="exampleClient" class="com.lingoswap.ws.util.JaxWsClientFactoryBean">
<property name="wsdlDocumentLocation" value="${exampleClient.wsdlDocumentLocation}" />
<property name="namespaceURI" value="http://com.example/exampleClient" />
<property name="localPart" value="ExampleService" />
<property name="serviceEndpointInterface" value="com.example.ExampleServicePortType" />
</bean>
的$ {之間的部分... }是屬性佔位符,這意味着從PropertyPlaceholderConfigurer指定的屬性文件中查找該值。指定值的示例如下所示:
## Web Service WSDL locations
exampleClient.wsdlDocumentLocation=http://www.example.com/exampleService?wsdl
然後,您可以修改屬性文件(也許「myapplication.properties」)更改WSDL位置需要(即使在運行時,如果您使用自定義的TargetSource一個ProxyFactoryBean一樣)對於它的價值,這是我實現一個簡單的JaxWsClientFactoryBean(無自動屬性修改支持):
package com.lingoswap.ws.util;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
public class JaxWsClientFactoryBean implements FactoryBean, InitializingBean {
private URL wsdlDocumentLocation;
private Class<?> serviceEndpointInterface;
private String namespaceURI;
private String localPart;
// derived from namespaceURI and localPart
private QName serviceName;
public void afterPropertiesSet() {
serviceName = new QName(namespaceURI, localPart);
}
public Object getObject() {
Service service = Service.create(wsdlDocumentLocation, serviceName);
Object port = service.getPort(serviceEndpointInterface);
return port;
}
public Class<?> getObjectType() {
return serviceEndpointInterface;
}
public boolean isSingleton() {
return false;
}
public URL getWsdlDocumentLocation() {
return wsdlDocumentLocation;
}
public void setWsdlDocumentLocation(final URL wsdlDocumentLocation) {
this.wsdlDocumentLocation = wsdlDocumentLocation;
}
public Class<?> getServiceEndpointInterface() {
return serviceEndpointInterface;
}
public void setServiceEndpointInterface(
final Class<?> serviceEndpointInterface) {
this.serviceEndpointInterface = serviceEndpointInterface;
}
public String getNamespaceURI() {
return namespaceURI;
}
public void setNamespaceURI(final String namespaceURI) {
this.namespaceURI = namespaceURI;
}
public String getLocalPart() {
return localPart;
}
public void setLocalPart(final String localPart) {
this.localPart = localPart;
}
}
我決定提供一個答案,即使你已經回答了你自己的問題。也許你會發現這個建議很有用。使用類似FactoryBean的實現的好處在於,它可以重用於所有Web服務客戶端,並封裝來自其消費者的Web服務創建。您的Web層或業務層對象將僅取決於SEI(服務端點接口)。
我得到了我的問題解決方案:
其實我是使用Web服務的默認構造函數用於創建服務實例。
我們可以使用已創建的存根,並將構造函數的新重定位WSDLURL作爲參數,因此我們不需要再創建客戶端存根。
更該給here:
感謝你。
創建存根的代碼可以寫成,
URL wsdlLocation = new URL("http://example.org/my.wsdl");
QName serviceName = new QName("http://example.org/sample", "MyService");
Service s = Service.create(wsdlLocation, serviceName);
我們可以使用屬性文件在運行時更改WSDL位置的目的。
甚至不需要編譯客戶端代碼。