【问题标题】:Consume webservice service in SPRING-WS using wsdl使用 wsdl 在 SPRING-WS 中使用 webservice 服务
【发布时间】:2013-09-05 16:55:24
【问题描述】:

我有 WSDL。例如:/sample/hello?wsdl。我想通过在 Spring-ws 中配置来调用服务 web 服务。我将此 wsdl 作为参数传递给 springconfig.xml 中的标签。 谁能告诉我如何在 Spring-ws 中使用这个 web 服务。

【问题讨论】:

    标签: spring web-services wsdl spring-ws


    【解决方案1】:

    1。设置项目依赖

    在 pom 文件中添加以下依赖项:

    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.5</version>
    </dependency>
    

    2。设置 Web 服务应用程序上下文

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
        <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />
    
        <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="contextPath" value="com.yourcomany.model" />
        </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" />
            </property>
            <property name="defaultUri"
                value="http://<hostname>:<portnumber>/sample/hello" />
        </bean>
    
    </beans>
    

    3。设置将映射到您的 SOAP 请求/响应对象的模型类

    例如,如果您的 SOAP 请求 XML 看起来像

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xxx="http://yourcomapny.com">
       <soapenv:Header/>
       <soapenv:Body>
          <xxx:InputParameters>
             <xxx:paramONE>1</xxx:paramONE>
          </xxx:InputParameters>
       </soapenv:Body>
    </soapenv:Envelope>
    

    您的 SOAP 响应 XML 看起来像:

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
       <env:Header>
          ...
       </env:Header>
       <env:Body>
          <xxx:OutputParameters xmlns:xxx="http://yourcompany.com">
             <xxx:paramONE>0</xxx:paramONE>
          </xxx:OutputParameters>
       </env:Body>
    </env:Envelope>
    

    相应的类(在您在 marshaller bean 中指定的包下:com.yourcompany.model)将分别为:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "paramONE" })
    @XmlRootElement(name = "InputParameters", namespace = "http://yourcompany.com")
    public class InputParameters {
    
        @XmlElement(required = true, namespace = "http://yourcompany.com")
        private String paramONE;
    
        public String getParamONE() {
            return paramONE;
        }
    
        public void setParamONE(String paramONE) {
            this.paramONE = paramONE;
        }
    
    }
    

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "paramONE" })
    @XmlRootElement(name = "OutputParameters", namespace = "http://yourcompany.com")
    public class OutputParameters {
    
        @XmlElement(required = true, namespace = "http://yourcompany.com")
        private BigDecimal paramONE;
    
        public BigDecimal getParamONE() {
            return this.paramONE;
        }
    
        public void setParamONE(BigDecimal paramONE) {
            this.paramONE= paramONE;
        }
    
    }
    

    4。添加对象工厂(在 com.yourcompany.model 包下)以创建请求/响应对象

    @XmlRegistry
    public class ObjectFactory {
    
        public ObjectFactory() {
        }
    
        public InputParameters createYourRequest() {
            return new InputParameters();
        }
    
        public OutputParameters createYourResponse() {
            return new OutputParameters();
        }
    
    }
    

    5。创建一个客户端来使用服务

    界面:

    public interface YourService {
    
        BigDecimal getValue(String paramOne);
    
    }
    

    实施

    @Component("yourServiceClient")
    public class YourServiceClient implements YourService {
    
        private static final ObjectFactory WS_CLIENT_FACTORY = new ObjectFactory();
    
        private WebServiceTemplate webServiceTemplate;
    
        @Autowired
        public YourServiceClient(WebServiceTemplate webServiceTemplate) {
            this.webServiceTemplate = webServiceTemplate;
        }
    
        @Override
        public BigDecimal getValue(String paramOne) {
            InputParameters request = WS_CLIENT_FACTORY
                    .createYourRequest();
            request.setParamONE(paramOne);
    
            OutputParameters response = (OutputParameters) webServiceTemplate
                    .marshalSendAndReceive(request);
    
            return response.getParamONE();
        }
    
    }
    

    【讨论】:

    • 我认为请使用“packagesToScan”而不是“contextPath”
    • 如何向不同的网络服务发送请求?我的意思是,如果您不想在应用程序上下文中设置 WSDL。
    • 1.添加另一个 webServiceTemplate ;重复使用相同的 marshaller/unmarshaller/messageSender;将 defaultUri 设置为第二个 Web 服务的 URI; 2. com.yourcompany.model 包下新增请求和响应类; 3、在ObjectFactory中添加方法,用于创建第二个Web服务的请求和响应对象; 4. 将方法添加到 YourService 接口及其实现(或创建单独的接口)以使用新服务。请记住,因为您现在有两个 WebServiceTemplate 实例,所以您需要使用类似 @Resource 的东西来区分它们
    • 对我上面的评论稍作修改:您仍然可以使用单个 WebServiceTemplate bean 并通过 messageSenders 字段在其中指定多个 Web 服务 URI
    • @TaoufikMohdit,非常有趣的解决方案,感谢您的完整回答。我有一个问题:httpclient 依赖项是做什么用的?我的意思是在您提出的解决方案中。提前感谢您的宝贵时间
    【解决方案2】:

    @Taoufik Mohdit 回答完毕!!

    要构建输入和输出对象,您可以使用Webservice-Client: Common approach with Spring WS, JAXB and just one WSDL file? 了解如何自动构建这些对象

    【讨论】:

      【解决方案3】:

      鉴于这个问题仍然存在,我想我会发布一个更新,以反映最新版本的 Spring Web 服务框架和 Spring 总体上引入的一些变化:

      1. Spring Boot 的引入允许利用“starter”POM 来简化 Maven 配置。 Spring-WS 有一个特定的 spring-boot-starter-web-services 启动器
      2. 引入了Spring JavaConfig,而不是使用 XML 指定 Spring 配置文件,它为配置 Spring 提供了一个类型安全的纯 Java 选项。
      3. 可以使用 Maven 插件自动生成基于给定 WSDL 文件的请求/响应对象。 Spring-WS 示例使用的插件是maven-jaxb2-plugin

      WebServiceTemplate 仍然是客户端 Web 服务访问的核心类。有关更多信息,您可以查看我写的detailed example on how to consume a web service using Spring-WS starting from a WSDL file

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多