当(JAX-WS)webservice重定位到另一个地方时,如何避免重建客户端Stub?

时间:2009-09-08 05:48:57

标签: jax-ws

对不起,我问了两次同样的问题。

我部署了 JAX-WS Web服务,并从我的客户端代码中使用它。 我的要求是, 在将 JAX-WS Web服务从一个地方重新定位到另一个地方时,我应该如何避免构建我的客户端代码(存根)?

谢谢。

3 个答案:

答案 0 :(得分:3)

如果您使用的是Spring,您可以创建一个帮助bean来配置您的客户端(这是我在最近的项目中所做的):

<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位置(即使在运行时,如果您使用带有ProxyFactoryBean的自定义TargetSource)为了它的价值,这里是我的实现简单的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(服务端点接口)。

答案 1 :(得分:0)

我得到了我的问题的解决方案:

实际上我正在使用web-Service的默认构造函数来创建服务实例。

我们可以使用已经创建的存根与构造函数将新重定位的WSDLURL作为参数,因此我们不需要再次创建客户端存根。

更多关于此here:

谢谢你。

答案 2 :(得分:0)

创建存根的代码可以写成,

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位置 甚至无需编译客户端代码。

相关问题