WebService的开发模版(Axis2 1.5环境)
今天在做apache-cxf-2.2.2整合Spring时候,出现了包版本与JDK1.5包冲突的问题,折腾了不少时间,还是没解决。于是改为Axis2开发,暂时绕过问题,完成任务要紧。
Axis2已经升级到1.5了,下载地址:
两个包,bin是开发包,war是部署包,实际上,如果只用jar包,两个里面的jar都一样用。
在IDEA环境下,Axis2的开发环境稍微麻烦些,毕竟没CXF或XFire方便。每次写build.xml脚本也挺麻烦的。这次写个项目模版,方便以后使用。
环境:
axis2-1.5
jdk1.5
idea-8.13
apache-tomcat-6.0.20
ant-1.6以上即可
一、创建项目
使用idea向导创建一个Java项目,项目结构如下图:
testaxis2
├─lib
├─resources
│ └─META-INF
└─src
└─mytest
├─lib
├─resources
│ └─META-INF
└─src
└─mytest
二、导入axis2的包
解压缩
axis2-1.5-bin.zip到一个目录下,然后直接在idea中引入axis2的包即可。也可以讲axis2的包复制到lib下。
三、创建服务
axis2服务的创建过程就是写Java类,不要求有接口,有也不会错。
这里创建一个最简单的Service,作为发布对象。
package mytest;
/**
* 测试Service
*
* @author leizhimin 2009-8-4 15:46:39
*/
public interface MyService {
String doSomething(String taskname);
}
/**
* 测试Service
*
* @author leizhimin 2009-8-4 15:46:39
*/
public interface MyService {
String doSomething(String taskname);
}
package mytest;
/**
* 测试Service
*
* @author leizhimin 2009-8-4 15:48:13
*/
public class MyServiceImpl implements MyService {
public String doSomething(String taskname) {
System.out.println( "MyServiceImpl is calling doSomething with " + taskname + "!");
return taskname + "is finished!";
}
}
/**
* 测试Service
*
* @author leizhimin 2009-8-4 15:48:13
*/
public class MyServiceImpl implements MyService {
public String doSomething(String taskname) {
System.out.println( "MyServiceImpl is calling doSomething with " + taskname + "!");
return taskname + "is finished!";
}
}
四、描述服务
在项目的resources\META-INF文件夹下面,创建services.xml,内容如下:
<
service
name
="myws"
scope
="application"
targetNamespace
="http://lavasoft.blog.51cto.com/"
>
< 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 >
< schema schemaNamespace ="http://lavasoft.blog.51cto.com/xsd" />
< parameter name ="ServiceClass" >mytest.MyServiceImpl </ parameter >
</ service >
< 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 >
< schema schemaNamespace ="http://lavasoft.blog.51cto.com/xsd" />
< parameter name ="ServiceClass" >mytest.MyServiceImpl </ parameter >
</ service >
服务名:myws
服务实现类:mytest.MyServiceImpl
五、编写构建脚本
用Ant编写build.xml,放到项目根文件夹下。
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
< project name ="ccmws" basedir ="." default ="generate.service" >
< property environment ="env" />
< property name ="AXIS2_HOME" value ="F:/axis2-1.5" />
< property name ="wsdl.uri" value ="./build/myws.wsdl" />
< property name ="build.dir" value ="build" />
< path id ="axis2.classpath" >
< fileset dir ="${AXIS2_HOME}/lib" >
< include name ="*.jar" />
</ fileset >
< fileset dir ="./lib" >
< include name ="*.jar" />
</ fileset >
</ path >
< target name ="compile.service" >
< mkdir dir ="${build.dir}" />
< mkdir dir ="${build.dir}/classes" />
< javac debug ="on"
fork ="true"
encoding ="UTF-8"
destdir ="${build.dir}/classes"
srcdir ="${basedir}/src"
extdirs ="**/test/**"
classpathref ="axis2.classpath" >
</ javac >
</ target >
< target name ="generate.wsdl" depends ="compile.service" >
< taskdef name ="java2wsdl"
classname ="org.apache.ws.java2wsdl.Java2WSDLTask"
classpathref ="axis2.classpath" />
< java2wsdl className ="mytest.MyServiceImpl"
outputLocation ="${build.dir}"
targetNamespace ="http://lavasoft.blog.51cto.com/"
schemaTargetNamespace ="http://lavasoft.blog.51cto.com/xsd" >
< classpath >
< pathelement path ="${axis2.classpath}" />
< pathelement location ="${build.dir}/classes" />
</ classpath >
</ java2wsdl >
</ target >
< target name ="generate.service" depends ="compile.service" >
< copy toDir ="${build.dir}/classes" failonerror ="false" >
< fileset dir ="${basedir}/resources" >
< include name ="**/*.xml" />
</ fileset >
</ copy >
< jar destfile ="${build.dir}/myws.aar" >
< fileset excludes ="**/Test.class" dir ="${build.dir}/classes" />
< fileset includes ="lib/*.jar" dir ="${basedir}" />
</ jar >
</ target >
< target name ="clean" >
< delete dir ="${build.dir}" />
</ target >
</ project >
< project name ="ccmws" basedir ="." default ="generate.service" >
< property environment ="env" />
< property name ="AXIS2_HOME" value ="F:/axis2-1.5" />
< property name ="wsdl.uri" value ="./build/myws.wsdl" />
< property name ="build.dir" value ="build" />
< path id ="axis2.classpath" >
< fileset dir ="${AXIS2_HOME}/lib" >
< include name ="*.jar" />
</ fileset >
< fileset dir ="./lib" >
< include name ="*.jar" />
</ fileset >
</ path >
< target name ="compile.service" >
< mkdir dir ="${build.dir}" />
< mkdir dir ="${build.dir}/classes" />
< javac debug ="on"
fork ="true"
encoding ="UTF-8"
destdir ="${build.dir}/classes"
srcdir ="${basedir}/src"
extdirs ="**/test/**"
classpathref ="axis2.classpath" >
</ javac >
</ target >
< target name ="generate.wsdl" depends ="compile.service" >
< taskdef name ="java2wsdl"
classname ="org.apache.ws.java2wsdl.Java2WSDLTask"
classpathref ="axis2.classpath" />
< java2wsdl className ="mytest.MyServiceImpl"
outputLocation ="${build.dir}"
targetNamespace ="http://lavasoft.blog.51cto.com/"
schemaTargetNamespace ="http://lavasoft.blog.51cto.com/xsd" >
< classpath >
< pathelement path ="${axis2.classpath}" />
< pathelement location ="${build.dir}/classes" />
</ classpath >
</ java2wsdl >
</ target >
< target name ="generate.service" depends ="compile.service" >
< copy toDir ="${build.dir}/classes" failonerror ="false" >
< fileset dir ="${basedir}/resources" >
< include name ="**/*.xml" />
</ fileset >
</ copy >
< jar destfile ="${build.dir}/myws.aar" >
< fileset excludes ="**/Test.class" dir ="${build.dir}/classes" />
< fileset includes ="lib/*.jar" dir ="${basedir}" />
</ jar >
</ target >
< target name ="clean" >
< delete dir ="${build.dir}" />
</ target >
</ project >
注意设定一些环境参数,Axis2的目录、发布服务的aar包名字,生成wsdl时候所用的服务实现类,以及命名空间等等。
六、运行build脚本打包
![](https://i-blog.csdnimg.cn/blog_migrate/a0167e55384d4d934b2cdcab9bb9974f.png)
运行后:
![](https://i-blog.csdnimg.cn/blog_migrate/646303bc4e6de241545f0de6f1a66df8.png)
七、部署服务
将下载的
axis2-1.5-war.zip解开,得到一个axis2.war,将其解压缩到tomcat的默认的web发布目录webapp下面,比如,在我机器上会产生C:\apache-tomcat-6.0.20\webapps\axis2的目录,目录下面就是axis2.war里面的内容。
然后,将生成的my.aar复制到C:\apache-tomcat-6.0.20\webapps\axis2\WEB-INF\services下面。
八、启动tomcat,查看发布服务
![](https://i-blog.csdnimg.cn/blog_migrate/bc4eeca289ffe3b1e9b08f170b470b7f.png)
浏览axis2的服务
![](https://i-blog.csdnimg.cn/blog_migrate/81a8c8999cc73f3e1f8350df6143ad6c.png)
点Services,查看所有的服务
![](https://i-blog.csdnimg.cn/blog_migrate/2619285e75f08906a867554e71301a92.png)
在点击服务连接,就可以查看wsdl了:
![](https://i-blog.csdnimg.cn/blog_migrate/fd8bd4873400ffd2369fefd49e222672.png)
经过这样的测试后,服务发布算成功了。