Axis2实践

Axis2是一个开源的、基于Java的Web服务框架,用于构建和发布Web服务。它是Apache软件基金会的一个顶级项目,并提供了丰富的功能和灵活性。

Axis2以轻量级和易于使用为设计目标,并提供了以下主要特性:

  1. 支持多种协议:Axis2支持多种常见的Web服务协议,如SOAP、HTTP、REST等。可以根据需要选择合适的协议来传输和交换数据。

  2. 高度可扩展:Axis2使用模块化的体系结构,允许用户根据自己的需求添加或移除特定功能的模块。这使得框架非常灵活,并能够适应各种不同的应用场景。

  3. 兼容性:Axis2遵循Web服务相关的标准和规范,如WSDL、SOAP、XML等。它与其他Web服务平台和工具具有良好的互操作性,可以与各种客户端和服务端进行集成。

  4. 强大的消息处理:Axis2提供了丰富的消息处理功能,包括消息传输、消息路由、消息转换等。它支持多种消息格式,如XML、JSON等,以及多种消息编码方式,如SOAP 1.1、SOAP 1.2等。

  5. 易于部署和管理:Axis2提供了一个简单易用的部署和管理工具,使得开发人员可以轻松地部署和管理Web服务。它还提供了一套完整的监控和管理API,方便进行性能调优和故障排除。

使用Axis2,您可以快速构建和发布高性能、可扩展的Web服务。它提供了丰富的功能和工具,使得开发和管理Web服务变得更加简单和高效。通过使用Axis2,您可以实现跨平台、跨语言的Web服务集成,并实现与其他系统的无缝交互。

1. 构建server项目

 1.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 
  8   <dependencies>
  9     <!--axis2 begin-->
 10     <dependency>
 11       <groupId>org.apache.axis2</groupId>
 12       <artifactId>axis2</artifactId>
 13       <version>${axis2.version}</version>
 14       <type>pom</type>
 15     </dependency>
 16 
 17     <dependency>
 18       <groupId>org.apache.axis2</groupId>
 19       <artifactId>axis2-spring</artifactId>
 20       <version>${axis2.version}</version>
 21       <exclusions>
 22         <exclusion>
 23           <groupId>org.springframework</groupId>
 24           <artifactId>spring-beans</artifactId>
 25         </exclusion>
 26         <exclusion>
 27           <groupId>org.springframework</groupId>
 28           <artifactId>spring-core</artifactId>
 29         </exclusion>
 30         <exclusion>
 31           <groupId>org.springframework</groupId>
 32           <artifactId>spring-web</artifactId>
 33         </exclusion>
 34         <exclusion>
 35           <groupId>org.springframework</groupId>
 36           <artifactId>spring-context</artifactId>
 37         </exclusion>
 38       </exclusions>
 39     </dependency>
 40 
 41     <dependency>
 42       <groupId>org.springframework</groupId>
 43       <artifactId>spring-context</artifactId>
 44       <version>4.3.18.RELEASE</version>
 45     </dependency>
 46 
 47     <dependency>
 48       <groupId>org.springframework</groupId>
 49       <artifactId>spring-web</artifactId>
 50       <version>4.3.18.RELEASE</version>
 51     </dependency>
 52 
 53     <dependency>
 54       <groupId>org.apache.axis2</groupId>
 55       <artifactId>axis2-transport-http</artifactId>
 56       <version>${axis2.version}</version>
 57     </dependency>
 58 
 59     <dependency>
 60       <groupId>org.apache.axis2</groupId>
 61       <artifactId>axis2-transport-local</artifactId>
 62       <version>${axis2.version}</version>
 63     </dependency>
 64 
 65     <dependency>
 66       <groupId>org.apache.axis2</groupId>
 67       <artifactId>axis2-xmlbeans</artifactId>
 68       <version>${axis2.version}</version>
 69     </dependency>
 70 
 71     <dependency>
 72       <groupId>junit</groupId>
 73       <artifactId>junit</artifactId>
 74       <version>4.11</version>
 75       <scope>test</scope>
 76     </dependency>
 77   </dependencies>
 78 
 79   <build>
 80     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 81       <plugins>
 82         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 83         <plugin>
 84           <artifactId>maven-clean-plugin</artifactId>
 85           <version>3.1.0</version>
 86         </plugin>
 87         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 88         <plugin>
 89           <artifactId>maven-resources-plugin</artifactId>
 90           <version>3.0.2</version>
 91         </plugin>
 92         <plugin>
 93           <artifactId>maven-compiler-plugin</artifactId>
 94           <version>3.8.0</version>
 95         </plugin>
 96         <plugin>
 97           <artifactId>maven-surefire-plugin</artifactId>
 98           <version>2.22.1</version>
 99         </plugin>
100         <plugin>
101           <artifactId>maven-jar-plugin</artifactId>
102           <version>3.0.2</version>
103         </plugin>
104         <plugin>
105           <artifactId>maven-install-plugin</artifactId>
106           <version>2.5.2</version>
107         </plugin>
108         <plugin>
109           <artifactId>maven-deploy-plugin</artifactId>
110           <version>2.8.2</version>
111         </plugin>
112         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
113         <plugin>
114           <artifactId>maven-site-plugin</artifactId>
115           <version>3.7.1</version>
116         </plugin>
117         <plugin>
118           <artifactId>maven-project-info-reports-plugin</artifactId>
119           <version>3.0.0</version>
120         </plugin>
121       </plugins>
122     </pluginManagement>
123   </build>

1.2创建实体类

 1 public class User {
 2     public int getUserId() {
 3         return userId;
 4     }
 5 
 6     public void setUserId(int userId) {
 7         this.userId = userId;
 8     }
 9 
10     public String getUserName() {
11         return userName;
12     }
13 
14     public void setUserName(String userName) {
15         this.userName = userName;
16     }
17 
18     private int userId;
19     private String userName;
20 }

1.3 创建发布的接口以及其实现类

1 public interface UserService {
2     User addUser(User user);
3     User get(int userId);
4 }
 1 import com.mike.xfire.domain.User;
 2 import org.springframework.stereotype.Service;
 3 
 4 /*
 5   Author:Mike
 6   创建时间:2019/11/4
 7   描述:
 8 */
 9 @Service("userService")
10 public class UserServerImpl implements UserService {
11     public User addUser(User user) {
12         user.setUserName("新增->" + user.getUserName());
13         return user;
14     }
15 
16     public User get(int userId) {
17         User user = new User();
18         user.setUserId(userId);
19         user.setUserName("如约而至");
20         return user;
21     }
22 }

1.3 添加配置

(1)创建资源文件spring-axis2.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xmlns:context="http://www.springframework.org/schema/context"
5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6     <context:component-scan base-package="com.mike.xfire.server" />
7     <bean id="application" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
8 </beans>

base-package="com.mike.xfire.server"为接口所在的包路径。

(2) 创建WEB-INF/web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 5           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 6          version="2.5">
 7     <!--配置请求处理的servlet-->
 8     <servlet>
 9         <servlet-name>AxisServlet</servlet-name>
10         <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
11     </servlet>
12 
13     <servlet-mapping>
14         <servlet-name>AxisServlet</servlet-name>
15         <url-pattern>/services/*</url-pattern>
16     </servlet-mapping>
17 
18 
19     <listener>
20         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
21     </listener>
22 
23     <context-param>
24         <param-name>contextConfigLocation</param-name>
25         <param-value>classpath:spring-axis2.xml</param-value>
26     </context-param>
27 </web-app>

(3) 创建WEB-INF/services/webserver/META-INF/services.xml

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

至此,server已经构建完成。

1.4 在浏览器上输入http://localhost:8080/axis2/services/ServiceServer?wsdl访问,http://localhost:8080/axis2为Tomcat部署的地址。

 2. 构建客户端

2.1 使用axis2第三方工具生成调用类

在axis2官网(https://axis.apache.org/axis2/java/core/)下载合适的版本,在bin目录cmd输入command

1 wsdl2java -o D:\workspace\study\webserver\axis2\wsdl -uri http://localhost:8080/axis2/services/ServiceServer?wsdl -s -p com.mike.study.axis2.client

-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。

-p参数指定了生成的Java类的包名,

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

-s 同步模式代码

-a 异步模式代码

2.2 构建maven项目,添加依赖和server端一样。

2.4 将axis2生成的Java类,放置项目中

 2.5 客户端调用ServiceServerStub,实现远程调用

 1 import com.mike.study.axis2.client.ServiceServerStub;
 2 import org.apache.axis2.AxisFault;
 3 
 4 import java.rmi.RemoteException;
 5 
 6 /**
 7  * @Classname Axis2ClientApplication
 8  * @Created by Michael
 9  * @Date 2023/7/31
10  * @Description TODO
11  */
12 public class Axis2ClientApplication {
13   public static void main(String[] args) {
14     try {
15       ServiceServerStub serverStub = new ServiceServerStub();
16       ServiceServerStub.User user = new ServiceServerStub.User();
17       user.setUserId(10001);
18       user.setUserName("axis2");
19 
20       ServiceServerStub.AddUser addUser = new ServiceServerStub.AddUser();
21       addUser.setUser(user);
22       ServiceServerStub.AddUserResponse addUserResponse = serverStub.addUser(addUser);
23       System.out.println(addUserResponse.get_return().getUserName());
24     } catch (AxisFault axisFault) {
25       axisFault.printStackTrace();
26     } catch (RemoteException e) {
27       e.printStackTrace();
28     }
29 
30   }
31 }

2.5 测试结果

 

 

 

posted @ 2023-07-31 15:57  天晴修屋顶  阅读(989)  评论(0编辑  收藏  举报