分布式(基础)-WebService(三)

简介: 分布式(基础)-WebService(三)

WebService-Axis2的方式

1、引入相关的依赖:

  1. <properties>
  2.    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3.    <maven.compiler.source>1.8</maven.compiler.source>
  4.    <maven.compiler.target>1.8</maven.compiler.target>
  5.    <axis2.version>1.7.9</axis2.version>
  6.  </properties>

  7.  <dependencies>
  8.    <dependency>
  9.      <groupId>junit</groupId>
  10.      <artifactId>junit</artifactId>
  11.      <version>4.11</version>
  12.      <scope>test</scope>
  13.    </dependency>
  14.    <!--axis2 begin-->
  15.    <dependency>
  16.      <groupId>org.apache.axis2</groupId>
  17.      <artifactId>axis2</artifactId>
  18.      <version>${axis2.version}</version>
  19.      <type>pom</type>
  20.    </dependency>

  21.    <dependency>
  22.      <groupId>org.apache.axis2</groupId>
  23.      <artifactId>axis2-spring</artifactId>
  24.      <version>${axis2.version}</version>
  25.      <exclusions>
  26.        <exclusion>
  27.          <groupId>org.springframework</groupId>
  28.          <artifactId>spring-beans</artifactId>
  29.        </exclusion>
  30.        <exclusion>
  31.          <groupId>org.springframework</groupId>
  32.          <artifactId>spring-core</artifactId>
  33.        </exclusion>
  34.        <exclusion>
  35.          <groupId>org.springframework</groupId>
  36.          <artifactId>spring-web</artifactId>
  37.        </exclusion>
  38.        <exclusion>
  39.          <groupId>org.springframework</groupId>
  40.          <artifactId>spring-context</artifactId>
  41.        </exclusion>
  42.      </exclusions>
  43.    </dependency>

  44.    <dependency>
  45.      <groupId>org.springframework</groupId>
  46.      <artifactId>spring-context</artifactId>
  47.      <version>4.3.18.RELEASE</version>
  48.    </dependency>

  49.    <dependency>
  50.      <groupId>org.springframework</groupId>
  51.      <artifactId>spring-web</artifactId>
  52.      <version>4.3.18.RELEASE</version>
  53.    </dependency>

  54.    <dependency>
  55.      <groupId>org.apache.axis2</groupId>
  56.      <artifactId>axis2-transport-http</artifactId>
  57.      <version>${axis2.version}</version>
  58.    </dependency>

  59.    <dependency>
  60.      <groupId>org.apache.axis2</groupId>
  61.      <artifactId>axis2-transport-local</artifactId>
  62.      <version>${axis2.version}</version>
  63.    </dependency>

  64.    <dependency>
  65.      <groupId>org.apache.axis2</groupId>
  66.      <artifactId>axis2-xmlbeans</artifactId>
  67.      <version>${axis2.version}</version>
  68.    </dependency>
  69.  </dependencies>

  70.  <build>
  71.    <finalName>axis2-demo</finalName>
  72.    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  73.      <plugins>
  74.        <plugin>
  75.          <artifactId>maven-clean-plugin</artifactId>
  76.          <version>3.1.0</version>
  77.        </plugin>
  78.        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  79.        <plugin>
  80.          <artifactId>maven-resources-plugin</artifactId>
  81.          <version>3.0.2</version>
  82.        </plugin>
  83.        <plugin>
  84.          <artifactId>maven-compiler-plugin</artifactId>
  85.          <version>3.8.0</version>
  86.        </plugin>
  87.        <plugin>
  88.          <artifactId>maven-surefire-plugin</artifactId>
  89.          <version>2.22.1</version>
  90.        </plugin>
  91.        <plugin>
  92.          <artifactId>maven-war-plugin</artifactId>
  93.          <version>3.2.2</version>
  94.        </plugin>
  95.        <plugin>
  96.          <artifactId>maven-install-plugin</artifactId>
  97.          <version>2.5.2</version>
  98.        </plugin>
  99.        <plugin>
  100.          <artifactId>maven-deploy-plugin</artifactId>
  101.          <version>2.8.2</version>
  102.        </plugin>
  103.      </plugins>
  104.    </pluginManagement>
  105.  </build>
  106. </project>

1.1、spring整合axis2的配置

1.2、把交给spring容器管理的对象让axis2去访问它。

2.2、新建个services.xml的文件:

内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得SpringApplicationContext对象 -->
  3. <!--访问的终端名称-->
  4. <service name="ServiceServer">
  5.    <description>axis2</description>
  6.    <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得SpringApplicationContext对象 -->
  7.    <parameter name="ServiceObjectSupplier" locked="false">
  8.        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
  9.    </parameter>
  10.    <!--
  11.    SpringBeanName固定的不能改
  12.    userServiceImplspring中注册的实现类得id,@Component注解
  13.    -->
  14.    <parameter name="SpringBeanName">userService</parameter>
  15.    <!--

  16.    <messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
  17.    例如,hello方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,
  18.    update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。
  19.    -->
  20.    <messageReceivers>
  21.        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
  22.                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
  23.        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
  24.                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
  25.    </messageReceivers>
  26. </service>

1.3,新建个web.xml文件,内容如下:

1.4、提供暴漏出的接口以及对应的实现类,代码如下:

  1. package com.ws.axis2.service;

  2. import com.ws.axis2.domain.User;

  3. public interface UserService {
  4.    User add(User user);
  5.    User  get(Integer userId);
  6. }
  7. package com.ws.axis2.service;

  8. import com.ws.axis2.domain.User;
  9. import org.springframework.stereotype.Service;

  10. @Service("userService")
  11. public class UserServiceImpl implements  UserService {
  12.    @Override
  13.    public User add(User user) {
  14.        user.setUserName(user.getUserName());
  15.        return user;
  16.    }

  17.    @Override
  18.    public User get(Integer userId) {
  19.        User user =new User();
  20.        user.setUserId(userId);
  21.        user.setUserName("魏朝阳");
  22.        return user;
  23.    }
  24. }

1.5、User的实体类如下:

  1. package com.ws.axis2.domain;

  2. public class User {
  3.    private Integer userId;
  4.    private String userName;

  5.    public Integer getUserId() {
  6.        return userId;
  7.    }

  8.    public void setUserId(Integer userId) {
  9.        this.userId = userId;
  10.    }

  11.    public String getUserName() {
  12.        return userName;
  13.    }

  14.    public void setUserName(String userName) {
  15.        this.userName = userName;
  16.    }
  17. }

1.6、去访问相应的wsdl文件如下:这时的参数类型是User.这里如果用原生的WebSrvice会报错误的。

访问下get方法:下面的结果就能证明,webService就是通过xml来定义格式的。

2、如何去在客户端做调用呢?

①、先生成客户端调用的类,拷贝到项目中的位置,先去apache Axis2的官网去下载axis2的文件

②、使用wsdl2java的批处理文件。命令如下:

wsdl2java -uri http://localhost:8080/services/ServiceServer?wsdl         -p client -s -o stub

-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。-p参数指定了生成的Java类的包名,

-o参数指定了生成的一系列文件保存的根目录。

-s 同步模式代码

-a 异步模式代码

③、将生成的客户端代码类拷贝到客户端测试调用

④、客户端的主函数的代码如下:

  1. package org.example;
  2. import client.ServiceServerStub;
  3. import org.apache.axis2.AxisFault;
  4. import java.rmi.RemoteException;
  5. public class App {
  6.    public static void main( String[] args ) {
  7.        try {
  8.            ServiceServerStub  serviceServerStub=new ServiceServerStub();
  9.            ServiceServerStub.Get  get = new ServiceServerStub.Get();
  10.            get.setUserId(1003);
  11.            try {
  12.                ServiceServerStub.GetResponse response=serviceServerStub.get(get);
  13.                ServiceServerStub.User user = response.get_return();
  14.                System.out.println(user.getUserName()+","+user.getUserId());
  15.            } catch (RemoteException e) {
  16.                e.printStackTrace();
  17.            }
  18.        } catch (AxisFault axisFault) {
  19.            axisFault.printStackTrace();
  20.        }

  21.    }
  22. }

运行的结果如下:

上面的过程就是客户端调用服务端的接口,从而返回相应的结果。

相关文章
|
JSON 网络协议 Dubbo
RPC框架(技术总结)
RPC框架(技术总结)
RPC框架(技术总结)
|
11小时前
|
XML JSON 网络协议
分布式(基础)-WebService(一)
分布式(基础)-WebService(一)
|
11小时前
|
XML Java 数据格式
分布式(基础)-WebService(四)
分布式(基础)-WebService(四)
|
11小时前
|
XML JSON Java
分布式(基础)-WebService(五)
分布式(基础)-WebService(五)
|
11小时前
|
XML Java 数据格式
分布式(基础)-WebService(二)
分布式(基础)-WebService(二)
|
4月前
|
XML 网络协议 网络架构
WebService - 基础详解
WebService - 基础详解
288 0
|
XML SQL JSON
开源SPL,WebService/Restful广泛应用于程序间通讯,如微服务、数据交换、公共或私有的数据服务等。
开源SPL,WebService/Restful广泛应用于程序间通讯,如微服务、数据交换、公共或私有的数据服务等。
134 0
开源SPL,WebService/Restful广泛应用于程序间通讯,如微服务、数据交换、公共或私有的数据服务等。
|
JSON 负载均衡 算法
轻量级RPC框架(0 - 项目概述)
轻量级RPC框架(0 - 项目概述)
轻量级RPC框架(0 - 项目概述)
|
Java 数据格式 网络架构
主流RPC框架详解,以及与SOA、REST的区别
什么是RPC RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。
1615 0