打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

这时候分为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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
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 方法 ";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

配置成功后启动tomcat,若tomcat启动成功

在浏览器输入

http://localhost:8080/dragsunTomDubboTest/services/ITestWebService?wsdl
  • 1
  • 1

若出现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";    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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]);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

接着两个服务器都跑起来,然后调用即可

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java ssh maven pom文件
Kudu Java API 条件查询
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory错误
3000门徒内部训练绝密视频(泄密版)第10课:彻底实战详解使用Java开发Spark程序
(七) 构建dubbo分布式平台-maven构建ant-framework框架的pom.xml文件配置(2)--代码生成工具
Jclouds 使用(1)安装
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服