使用wsdl在SPRING-WS中使用Web服务服务 [英] Consume webservice service in SPRING-WS using wsdl

查看:137
本文介绍了使用wsdl在SPRING-WS中使用Web服务服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have WSDL with me .eg: /sample/hello?wsdl . I want to invoke the service the webservice by configuring in Spring-ws. I passed this wsdl as parameter to tags in springconfig.xml. Can anyone please tell me how to consume this webservice in Spring-ws.

推荐答案

1.设置项目依赖项

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

1. Set up project dependencies

add the following dependencies to the pom file:

<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看起来像

3. Set up model classes which would map to your SOAP request/response objects

For example, if your SOAP request XML looked like

<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如下:

and your SOAP response XML looked like:

<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下)分别为:

the corresponding classes (under the package you specified in the marshaller bean: com.yourcompany.model) would be respectively:

@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();
    }

}

这篇关于使用wsdl在SPRING-WS中使用Web服务服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆