2

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

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

谢谢你。

4

3 回答 3

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(服务端点接口)。

于 2009-12-21T17:59:33.073 回答
0

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

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

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

更多关于这里给出的:

感谢您。

于 2009-09-19T11:02:56.627 回答
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 位置。
甚至不需要编译客户端代码。

于 2011-08-19T06:51:59.557 回答