axis2调用webservice

  先说一下基本的知识点。

  SOAP: Simple Object AccessProtocol。它是一种用于访问 Web 服务的协议。因为 SOAP 基于XML 和 HTTP ,其通过XML 来实现消息描述,然后再通过 HTTP 实现消息传输。SOAP 是用于在应用程序之间进行通信的一种通信协议。它包括四个部分:SOAP封装(envelop),封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;SOAP编码规则(encoding rules),用于表示应用程序需要使用的数据类型的实例; SOAP RPC表示(RPC representation),表示远程过程调用和应答的协定;SOAP绑定(binding),使用底层协议交换信息。

  axis:Axis本质上就是一个SOAP引擎,提供创建服务器端、客户端和网关SOAP操作的基本框架。能够处理SOAP和来自W3C的各种XML标准。

  在axis2调用webservice服务之前,需要做几项准备工作:

  1、 导入axis2的包。pom.xml文件如下:

  

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4 
 5     <groupId>com.bigbang.cxf</groupId>
 6     <artifactId>cxfclient</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <packaging>jar</packaging>
 9 
10     <name>cxfclient</name>
11     <url>http://maven.apache.org</url>
12 
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15         <axis.version>1.7.5</axis.version>
16     </properties>
17 
18     <dependencies>
19         <dependency>
20             <groupId>junit</groupId>
21             <artifactId>junit</artifactId>
22             <version>3.8.1</version>
23             <scope>test</scope>
24         </dependency>
25 
26         <!-- axis2 -->
27         <dependency>
28             <groupId>org.apache.axis2</groupId>
29             <artifactId>axis2-kernel</artifactId>
30             <version>${axis.version}</version>
31         </dependency>
32         <dependency>
33             <groupId>org.apache.axis2</groupId>
34             <artifactId>axis2-adb</artifactId>
35             <version>${axis.version}</version>
36         </dependency>
37         <dependency>
38             <groupId>org.apache.axis2</groupId>
39             <artifactId>axis2-transport-http</artifactId>
40             <version>${axis.version}</version>
41         </dependency>
42         <dependency>
43             <groupId>org.apache.axis2</groupId>
44             <artifactId>axis2-transport-local</artifactId>
45             <version>${axis.version}</version>
46         </dependency>
47 
48     </dependencies>
49     <build>
50         <plugins>
51             <plugin>
52                 <groupId>org.mortbay.jetty</groupId>
53                 <artifactId>maven-jetty-plugin</artifactId>
54                 <version>6.1.26</version>
55                 <configuration>
56                     <webAppConfig>
57                         <contextPath>/</contextPath>
58                     </webAppConfig>
59                 </configuration>
60             </plugin>
61 
62             <plugin>
63                 <groupId>org.apache.maven.plugins</groupId>
64                 <artifactId>maven-compiler-plugin</artifactId>
65                 <configuration>
66                     <source>1.8</source>
67                     <target>1.8</target>
68                 </configuration>
69             </plugin>
70         </plugins>
71     </build>
72 </project>
View Code

  

  2、获取wsdl文件或者xml文件,访问webservice的访问地址,在浏览器中复制所有的文字,保存为xml或者wsdl文件。

  

  3、使用axis2生成客户端调用的代码需要eclipse的插件支持,下载axis2-eclipse-codegen-plugin-1.7.5插件之后,根据如下操作生成客户端调用代码: 

  

  

      

  然后会在目标目录下生成如下文件,并且是按照修改的包名来存放文件的:

  

  将文件导入到项目中,编写一个类来做测试请求:

  

  Main.java代码如下:

  

 1 /**
 2  * 
 3  */
 4 package com.bigbang.cxf.main;
 5 
 6 import com.bigbang.cxf.cxfclient.Login;
 7 import com.bigbang.cxf.cxfclient.LoginE;
 8 import com.bigbang.cxf.cxfclient.LoginResponse;
 9 import com.bigbang.cxf.cxfclient.LoginResponseE;
10 import com.bigbang.cxf.cxfclient.LoginServiceImplServiceStub;
11 
12 /**
13  * @author pzj 2017年5月18日 上午9:29:57
14  *
15  */
16 public class Main {
17 
18     /**
19      * @param args
20      */
21     public static void main(String[] args) {
22         try {
23             LoginServiceImplServiceStub serviceStub = new LoginServiceImplServiceStub(
24                     "http://localhost:8080/service/login");
25             LoginE loginE = new LoginE();
26             Login loginParam = new Login();
27             loginParam.setName("bigbang");
28             loginParam.setPassword("123456");
29             loginE.setLogin(loginParam);
30             LoginResponseE responseE = serviceStub.login(loginE);
31             LoginResponse response = responseE.getLoginResponse();
32             String returStr = response.get_return();
33             System.out.println(returStr);
34         } catch (Exception e) {
35             e.printStackTrace();
36         }
37 
38     }
39 
40 }

  

  启动webservice服务之后,再运行main代码,在控制台将会输出如下语句:

  bigbang登录成功!密码是:123456

至此,整个axis2调用webservice接口成功!

原文地址:https://www.cnblogs.com/bigbang92/p/webservice_axis2.html