shimiso

Axis2是目前比较流行的WebService引擎。WebService被应用在很多不同的场景。例如,可以使用WebService来发布服务端 Java类的方法,以便使用不同的客户端进行调用。这样可以有效地集成多种不同的技术来完成应用系统。WebService还经常被使用在SOA中,用于 SOA各个部分交换数据。本课程重点讲解了Axis2的核心功能,并提供了三个项目以使大家理解并掌握如何将Axis2应用于不同的场景。

本课程站在初学者角度介绍了Axis2的大多数常用的功能,例如:以多种方式编写和发布WebService、JAX-RPC、JAX-WS、复杂类型传输、WebService会话管理、Axis2模块等

Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物。Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持Spring、JSON等技术。这些都将在后面的系列教程中讲解。

 

以下软件版本为本人使用版本

Myeclipse 10.0

Tomcat 6.0

Jdk 6

以下两项为myeclipse插件 (http://axis.apache.org/axis2/java/core/tools/index.html)

axis2-eclipse-codegen-plugin-1.6.0.zip

axis2-eclipse-service-plugin-1.6.0.zip

以下zip包为tomcat插件 (http://axis.apache.org/axis2/java/core/download.cgi)

axis2-1.6.0-war.zip

axis2中的jar,在客户端开发中需要用到这些jar包

axis2-1.6.0-bin.zip

 

myeclipse插件安装 (可参阅 http://blog.csdn.net/shimiso/article/details/8122596 ) 将axis2-eclipse-codegen-plugin-1.6.0.zip和axis2-eclipse-service-plugin-1.6.0.zip解压 D:\Users\Administrator\AppData\Local\MyEclipse为我的myeclipse安装的根目录

将解压后的plugins中的jar包复制到myeclipse的安装目录下的common中的plugins中(只需jar包即可)如: D:\Users\Administrator\AppData\Local\MyEclipse\Common\plugins

进入到myeclipse安装目录中的org.eclipse.equinox.simpleconfigurator中,如下: D:\Users\Administrator\AppData\Local\MyEclipse\MyEclipse 9\configuration\org.eclipse.equinox.simpleconfigurator

用记事本打开org.eclipse.equinox.simpleconfigurator 中的bundles.info文件

项bundles.info文件中添加下面内容 org.apache.axis2.eclipse.codegen.plugin,1.6.0,file:/d:/Users/Administrator/AppData/Local/MyEclipse/Common/plugins/org.apache.axis2.eclipse.codegen.plugin_1.6.0.jar,4,false

org.apache.axis2.eclipse.service.plugin,1.6.0,file:/d:/Users/Administrator/AppData/Local/MyEclipse/Common/plugins/org.apache.axis2.eclipse.service.plugin_1.6.0.jar,4,false

启动myeclipse后 点击新建other后如果能看到Axis2 Wizards文件夹下有两个内容表示安装成功。

 

tomcat安装 将tomcat6.0解压到某个目录中,这里使用的时免安装版的

在系统环境变量中添加: TOMCAT_HOME=D:\webservice\tomcatapache-tomcat-6.0.32 CATALINA_BASE=D:\webservice\tomcat\apache-tomcat-6.0.32 CATALINA_HOME=D:\webservice\tomcat\apache-tomcat-6.0.32

在path中添加 %TOMCAT_HOME%/lib

在浏览器中输入http://localhost:8080看到tomcat的主页说明安装配置成功

 

解压axis2-1.6.0-war.zip后得到一个axis2.war的文件,将该文件复制到D:\webservice\tomcat\apache-tomcat-6.0.32\webapps目录下

启动tomcat,之后会自动生成一个axis2的文件夹,该文件夹在D:\webservice\tomcat\apache-tomcat-6.0.32\webapps下

在浏览器中输入http://localhost:8080/axis2/看到

说明axis2 web服务器搭建成功 到此开发环境搭建完成.

 

服务器端发布 启动myeclipse新建一个java工程 写一个服务器端的类向客户端返回一个字符串

package com.test;

public class Test {
 public String server(String name)
 {
  return "Hello " + name;
 }
}

进行打包: 1在当前项目上右击 选择export

 

选择当前项目并且点击browse选择jar的输入目录和输入生成的jar包文件名称

点击finish 完成jar包打包

打包arr 在该项目上右击newotherAxis2 WizardsAxis2 Service Archiver

 

选择arr包得输入目录,点击next

 

选择skip wsdlnext

点击brows 选择前面已经打包好jar包,点击addànext

选中复选框next

 

输入自己的定的servername,输入完整Class名点击load,下面出来该类中的所有方法,如果选中,就会发布此方法,客户掉便可以调用

nextbrowse选择output file location,选择arr文件的数据路径,输入arr文件的名称,点击finish完成。

 

发布arr,将打包好的arr文件复制到D:\webservice\tomcat\apache-tomcat-6.0.32\webapps\axis2\WEB-INF\services目录下,重新启动tomcat,在浏览其中输入http://localhost:8080/axis2,点击service进入如下界面

 

可以看到testAxis(在打包arr时自定义的名称)点击testAxis可以查看xml,表示发布成功,可以在客户端进行调用。

 

客户端,新建一个java工程,编写客户端代码 右击newother Axis2 WizardsAxis2 Code Generatornext

 

选择第一个单选按钮next

 

在浏览器中输入http://localhost:8080/axis2 -->选择services-->testAxis,进入一个xml界面,复制浏览器中地址(http://localhost:8080/axis2/services/llspServer?wsdl),粘贴到WSDL file location中,点击next

保持默认,next

 

选择客户端的工程,output path的值是新建的这个客户端的工程,nextokfinish

 

在myeclipse界面中的package explorer窗口中可以看到新建的testAxis工程自动生成了一个包,包名是服务器端工程的包名,目前有很多错误,是因为没有导入jar包,导入jar,首先解压axis2-1.6.0-bin.zip,将lib中的所有jar导入到客户端工程中

在axisClient工程中新建一个类Test

import java.rmi.RemoteException;
import com.test.Server;
import com.test.TestAxisStub;

public class TestClient {
 public static void main(String args[]) throws RemoteException
 {
  //首先新建一个stub的对象(桩)
  TestAxisStub tas = new TestAxisStub();
  //实例化服务端的方法的对象
  Server s = new Server();
  //为server方法设置参数
  s.setName("john");
  //执行该方法,并且接受返回值
  String str = tas.server(s).get_return();
  System.out.println(str);
 }
}

开启tomcat,运行Test.java,得到服务器返回的值。

 

分类:

技术点:

相关文章: