最近要提供一个搜索的服务 那天老师就说是用axis 之后我就是一顿百度 gogole 各种陌生英文单词扑面而来 搞了一个多星期终于弄出了点东西
第一次做 之前听说过SOA 不过不是很了解 这次算是有一定认识了
使用的是Axis2作为Web Service开发框架
下载了axis2.war直接放到tomcat的webapp目录下
下载axis2-1.4.rar axis2-std-1.0-bin.zip
配置环境变量(有点多 可能也有很多多余的东西)
引用
%CATALINA_HOME% E:\Tomcat 6.0
AXIS_HOME =%CATALINA_HOME%\webapps\axis2\WEB-INF
AXIS_CLASSPATH %AXIS_LIB%\axis2-ant-plugin-1.4.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.1.1.jar;%AXIS_LIB%\axis2-kernel-1.4.jar
AXIS_LIB %AXIS_HOME%\lib
在classpath中加入%AXIS_HOME%;%AXIS_CLASSPATH%
path中加入%AXIS_HOME%\bin
下面开始做服务端开发 我的步骤有点麻烦 看了看官方文档也没大明白 应该是面向接口开发
然后生成skeleton 在这里写实现代码
我直接写的实现类
BaseOperation.java
DBConnection.java
Fileinfo.java
FileSearchServiceImpl.java
IFileSearchService.java
五个文件 用的数据库是Mysql
META-INF目录下的services.xml内容为
<service name="FileSearch" scope="application" targetNamespace="http://filesearch.hpdfs/">
<description>
FileSearchManage Service
</description>
<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://filesearch.hpdfs/xsd"/>
<parameter name="ServiceClass">hpdfs.FileSearchServiceImpl</parameter>
</service>
包结构为FileSerch\hpdfs
\META-INF\services.xml
将FileSearch文件夹复制到E:\Tomcat 6.0\webapps\axis2\WEB-INF\services下 或者将文件夹打包成.aar文件
启动tomcat 在浏览器中输入http://localhost:8080/axis2/services/FileSearch?wsdl如果看到有内容则说明此服务部署成功
在地址栏中调用服务中的方法就可以返回相应的xml数据结果
现在来实现客户端
进入E:\axis2-1.4\bin目录 运行
wsdl2java.bat -uri http://localhost:8080/axis2/services/*?wsdl -o F:\ -p Stub
这样就可以将相应地址的wsdl文件生成java文件 不同的参数会生成不同的文件
上述参数设置是生成的FileSearchCallbackHandler.java和FileSearchStub.java两个文件
注意生成的包名为Stub
在客户端工程中建立一个包 Stub 将上述生成的两个文件拷入 编写测试代码
public static void main(String[] args)
{
try {
FileSearchStub stub=new FileSearchStub("http://localhost:8080/axis2/services/FileSearch");
FileSearchStub.SearchFilebyanyattr request=new FileSearchStub.SearchFilebyanyattr();
request.setAttrvalue("1");
FileSearchStub.SearchFilebyanyattrResponse response=stub.searchFilebyanyattr(request);
FileSearchStub.Fileinfo[] obj=response.get_return();
for(int i=0;i<obj.length;i++)
System.out.println(obj[i].getDir());
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
这样就实现了一个提供搜索功能的webservice
实现这个一直挺顺利 传递单个对象也很顺利 不过传递list对象的时候就出问题了 客户端无法解析
在地址栏中输入相应的方法和参数是可以查询出多个对象结果的
问题可能是处在wsdl文件上 不过这个是自动生成的 我试着改了一下 然后用
引用
wsdl2java.bat -uri *?wsdl
的方法来生成客户端 不过还是不行
查了很多资料也不得其解 试了Arraylist还有axis2中的Array都不行
后来更改了服务端代码 不用返回List<>的方式 而是用Fileinfo[]数组的类型
这样在客户端才终于获取到了 终于实现了 吸口气~
客户端的代码做了改变 数组的大小是通过下面方法设置
int rowcount=0;
try {
rs.last();//让游标指向最后
rowcount=rs.getRow();//获取rs长度
rs.beforeFirst();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
然后Fileinfo[] fileinfolist=new Fileinfo[rowcount];//初始化数组大小
总之是实现了 webservice算是稍微入了点门了 革命尚未成功 同志仍需努力啊~
ps:axis2中文学习文档
http://tech.surfcareer.com/axis2/ug_installtestclient.html