Java创建WebService服务及客户端实现
WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto.com/art/200908/147125.htm,不同的语言平台对WebService都有实现,Java的WebService实现,比较流行的有Axis2、Jaxws,本文介绍的是Axis2。
Axis2下载和部署
Axis2是Apache开发的一个开源项目,再次感叹Apache的伟大!
下载地址:
http://mirror.bit.edu.cn/apache//axis/axis2/java/core/1.6.2/axis2-1.6.2-war.zip
将其内axis2.war解压到<Tomcat安装目录>/webapps下,启动Tomcat,war包会自动解压,
访问http://localhost:8080/axis2/,如果看到欢迎主页,则说明部署成功。
配置Axis2
<Tomcat安装目录>/webapps/axis2/WEB-INF/conf/axis2.xml,配置其内两个属性,以便调试。
我打的名字是server.jar,更改后置为aar,所以最后是server.aar,Axis2建议使用aar发布WebService,
将server.aar放到<Tomcat安装目录>/webapps/axis2/WEB-INF/services下,访问http://localhost:8080/axis2/services/listServices,
多出了一个CalculateService,说明发布成功。
package webservice.client.test;import java.rmi.RemoteException;import webservice.client.test.RandomFontsWebServiceStub.ArrayOfString;import webservice.client.test.RandomFontsWebServiceStub.GetChineseFonts;public class ThirdClient {/** * @param args * @throws RemoteException */public static void main(String[] args) throws RemoteException {RandomFontsWebServiceStub stub = new RandomFontsWebServiceStub();GetChineseFonts getChineseFonts = new GetChineseFonts();getChineseFonts.setByFontsLength(10);// 免费使用有限制,最多8个ArrayOfString result = stub.getChineseFonts(getChineseFonts).getGetChineseFontsResult();for(String str : result.getString()) {System.out.println(str);}}}(完)