Spring+基于AXIS2 的 werservice

Spring+基于AXIS2 的 werservice

Web Service基本概念

Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术。是:通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。

XML:(Extensible Markup Language)扩展型可标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础。

Soap:(Simple Object Access Protocol)简单对象存取协议。是XML Web Service 的通信协议。当用户通过UDDI找到你的WSDL描述文档后,他通过可以SOAP调用你建立的Web服务中的一个或多个操作。SOAP是XML文档形式的调用方法的规范,它可以支持不同的底层接口,像HTTP(S)或者SMTP。

WSDL:(Web Services Description Language) WSDL 文件是一个 XML 文档,用于说明一组 SOAP 消息以及如何交换这些消息。大多数情况下由软件自动生成和使用。

UDDI (Universal Description, Discovery, and Integration) 是一个主要针对Web服务供应商和使用者的新项目。在用户能够调用Web服务之前,必须确定这个服务内包含哪些商务方法,找到被调用的接口定义,还要在服务端来编制软件,UDDI是一种根据描述文档来引导系统查找相应服务的机制。UDDI利用SOAP消息机制(标准的XML/HTTP)来发布,编辑,浏览以及查找注册信息。它采用XML格式来封装各种不同类型的数据,并且发送到注册中心或者由注册中心来返回需要的数据。

接下来是将webservice 基于axis2 +spring 配置实战

spring 的配置就不细讲了

axis2 用到的 maven 依赖如下:
    <properties>
        <axis2.version>1.6.2</axis2.version>
    </properties>

      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-adb -->
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-adb</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-jaxws -->
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-jaxws</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-kernel</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-metadata</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-saaj -->
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-saaj</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-spring</artifactId>
          <version>${axis2.version}</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-transport-http</artifactId>
          <version>${axis2.version}</version>
      </dependency>

      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-transport-local</artifactId>
          <version>${axis2.version}</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
      <dependency>
          <groupId>commons-httpclient</groupId>
          <artifactId>commons-httpclient</artifactId>
          <version>3.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.neethi/neethi -->
      <dependency>
          <groupId>org.apache.neethi</groupId>
          <artifactId>neethi</artifactId>
          <version>3.0.2</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.woden/woden-api -->
      <dependency>
          <groupId>org.apache.woden</groupId>
          <artifactId>woden-api</artifactId>
          <version>1.0M9</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.woden/woden-impl-commons -->
      <dependency>
          <groupId>org.apache.woden</groupId>
          <artifactId>woden-impl-commons</artifactId>
          <version>1.0M9</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.woden/woden-impl-commons -->
      <dependency>
          <groupId>org.apache.woden</groupId>
          <artifactId>woden-impl-dom</artifactId>
          <version>1.0M9</version>
      </dependency>


      <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
      <dependency>
          <groupId>wsdl4j</groupId>
          <artifactId>wsdl4j</artifactId>
          <version>1.6.2</version>
      </dependency>


      <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.schema/XmlSchema -->
      <dependency>
          <groupId>org.apache.ws.commons.schema</groupId>
          <artifactId>XmlSchema</artifactId>
          <version>1.4.7</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom-api -->
      <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          <artifactId>axiom-api</artifactId>
          <version>1.2.13</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom-dom -->
      <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          <artifactId>axiom-dom</artifactId>
          <version>1.2.13</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom-impl -->
      <dependency>
          <groupId>org.apache.ws.commons.axiom</groupId>
          <artifactId>axiom-impl</artifactId>
          <version>1.2.13</version>
      </dependency>

这时候分为producer(生产者即服务提供者),consumer(消费者即服务调用者)

producer配置

定义webservice接口需要在WEB-INF目录下 创建services文件夹并且在services文件夹下的子文件夹(命名自由)目录下创建META-INF目录,
创建 services.xml (服务接口文件)
<?xml version="1.0" encoding="UTF-8"?>

<service name="ITestWebService">
    <description>TestWebService</description>
    <parameter name="ServiceClass">com.java.webservice.consumer.ITestWebService</parameter>
    <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
    <!--
 org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
      -->
    <parameter name="SpringBeanName">testWebService</parameter>
    <!--SpringBeanName名字是固定的不能改
    testWebService 是spring中注册的实现类的id(这个大家肯定很清楚了)
    -->
    <!--
    <operation name="doTestWebService">
        <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"
                          class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
    -->
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
</service>
spring配置文件中 配置producer的接口类的实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--
    <bean id="testWebService" class="com.java.webservice.consumer.impl.TestWebServiceImpl"></bean>
    -->
    <bean id="testWebServiceTarget" class="com.java.webservice.consumer.impl.TestWebServiceImpl"></bean>


    <bean id="testWebService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>com.java.webservice.consumer.ITestWebService</value>
        </property>
        <property name="proxyTargetClass">
            <value>false</value>
        </property>
        <property name="target">
            <ref bean="testWebServiceTarget"/>
        </property>
    </bean>
    <!--
    -->

</beans>
org.springframework.aop.framework.ProxyFactoryBean这个类基于动态代理代理接口并生成实现类
ITestWebService类 与 实现类 TestWebServiceImpl
package com.java.webservice.consumer;

import java.io.Serializable;

/**
 * Created by zhuangjiesen on 2017/1/15.
 */
public interface ITestWebService extends Serializable {

    public String doTestWebService(String testParam);
}



package com.java.webservice.consumer.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.java.webservice.consumer.ITestWebService;

/**
 * Created by zhuangjiesen on 2017/1/15.
 */
public class TestWebServiceImpl implements ITestWebService {


    public String doTestWebService(String testParam) {

        System.out.println(" TestWebServiceImpl : doTestWebService ! testParam : "+testParam);

        return "我是庄杰森的 TestWebServiceImpl doTestWebService 方法 ";
    }
}
配置成功后启动tomcat,若tomcat启动成功
在浏览器输入
http://localhost:8080/dragsunTomDubboTest/services/ITestWebService?wsdl
若出现wsdl接口描述则配置成功

接下来就是consumer的配置

maven配置跟 producer 一致
consumer是基于spring mvc 请求调用webservice接口的
    /**
     Description: 跳转  webservice 测试   <p>
     @author : zhuangjiesen@ssit-xm.com.cn 庄杰森 2016年4月19日
     */
    @RequestMapping("/common/indexToWebServiceTest.do")
    public String indexToWebServiceTest(HttpServletRequest request,HttpServletResponse response,ModelMap model){

        WebServiceProducer webServiceProducer=(WebServiceProducer)BeanHelper.getApplicationContext().getBean("webServiceProducer");
        try {
            webServiceProducer.invokeTestWebService_doTestWebService();
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute("title","webservice + axis2 测试!!@!!!");
        return "/common/index.html";
    }
webServiceProducer类
package com.java.webservice.producer;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;

/**
 * Created by zhuangjiesen on 2017/1/15.
 */
public class WebServiceProducer {

    public void invokeTestWebService_doTestWebService() throws  Exception {
        //call web service by RPC method
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();

        //specify URL for invoking
        //wsdl接口
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/dragsunTomDubboTest/services/ITestWebService?wsdl");

        options.setTo(targetEPR);

      //参数数组
        Object[] opAddEntryArgs = new Object[]{"超人--庄杰森!!  "};

        //返回值
        Class[] classes = new Class[]{String.class};

        //wsdl接口中 targetNamespace 与方法名
        QName opAddEntry = new QName("http://consumer.webservice.java.com","doTestWebService");
        //opAddEntry.equals(objectToTest);

        Object[] objs=serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes);
        System.out.println("  WebServiceProducer !!! doTestWebService :  "+objs[0]);

    }


}
接着两个服务器都跑起来,然后调用即可
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值