第四步就是写客户端程序了。
WSClient.java
try {
String endpoint = "http://localhost:8080/ws/services/CalcService";
Service service = new Service();
Call call = service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName(new QName("http://ws.mstar.org", "plus"));
Object[] params = new Object[2];
params[0] = 10;
params[1] = 20;
Integer result = (Integer) call.invoke(params);
System.out.println("Result: " + result);
} catch (Exception e) {
e.printStackTrace();
}
这是动态的调用WebService的方法,并不需要根据WSDL生成客户端存根。但是对于Service中有复合类型的时候就不可以了。下一个例子讲的就是如何做客户端存根。如果这个例子能够正常运行就可以了。
然后是一个有FooBean的Service
很简单,就是返回一个FooBean,在参数上做了一些手脚J。
然后在server-config.wsdd中加入描述
这里多了typeMapping标记用来描述复杂数据类型FooBean。对入复杂类型的序列化可以是自定义的,在serializer deserializer属性中改。那个xmlns,要留意一下,因为在客户端生成存根时的AntTask中要写一些Mapping,来确定那些xmlns映射到哪些package。当然这些也可以在WSDL中找到。而WSDL中的xmlns也是在这里定义的。
下面也各客户端。由于这次的Service中有复杂类型所以要根据WSDL生成这些复杂类型和Service的存根。取得WSDL的方法可以使用java2WSDL来做,但那样太麻烦,其实在List页面中用右键-另存为就可以了。然后写一个ant文件
运行客户端看看!
做简单的WebService就基本上这样了。但这样的Webservice还远远不能用户真正的项目中,还有很多问题没有解决,比如安全问题,有状态会话问题等等。这些问题很多可以通过Hanlder来解决,他有点类似于FilterServlet。具体的东西我还有没有研究,axis在这方面的资料就很少了。研究明白以后再写一篇吧。
本文源码:http://www.blogjava.net/Files/mstar/WsLearn.rar
<?xml version="1.0" encoding="UTF-8"?>
<project default="GenJavaSub" basedir=".">
<taskdef name="wsdl2java" classname="org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask"/>
<target name="GenJavaSub">
<property name="output.dir" value="src"/>
<property name="generated.dir" value="src/org/mstar/wsclient/generated"/>
<property name="wsdl.url" value="wsdl/FooService.wsdl"/>
<delete dir="${generated.dir}"/>
<mkdir dir="${generated.dir}"/>
<wsdl2java all="true" debug="false" helperGen="true"
noimports="false"
output="${output.dir}"
serverside="false"
skeletonDeploy="false"
typeMappingVersion="1.1"
url="${wsdl.url}"
verbose="false"
noWrapped="false">
<mapping namespace="http://ws.mstar.org" package="org.mstar.wsclient.generated"/>
<mapping namespace="http://localhost:8080/ws/services/FooService" package="org.mstar.wsclient.generated"/>
</wsdl2java>
</target>
</project>
这里上面的东西比较好理解,在下面的mapping中一定要注意,因为每个Service和ComplexType都有自己的xmlns,这里就是把xmlns映射到指定的package 比如 把http://ws.mstar.org 映射到org.mstar.wsclient.generated包。这些namespace可以在wsdl文件中找到。运行ant就会看见在src中的org.mstar.wsclient.generated中多了几个java文件。接下在我们就可以用这几个类了。还有一点要注意wsdl2java 的output属性要指向src,而不是generated文件夹。
客户端调用就相对容易多了
FooServiceService serviceLocator = new FooServiceServiceLocator();
try {
FooService service = serviceLocator.getFooService();
FooBean fooBean = service.getFooBean("fooBean", 10, true);
System.out.println(fooBean);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
<service name="FooService" provider="java:RPC">
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="org.mstar.ws.FooService"/>
<parameter name="scope" value="session"/>
<typeMapping encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="http://ws.mstar.org"
qname="ns1:FooBean"
languageSpecificType="java:org.mstar.ws.FooBean"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
name="FooBean"/>
</service>
public class FooService {
public FooBean getFooBean(String foo1,Integer foo2,Boolean foo3){
return new FooBean(foo1+"(Remote)",foo2+10,!foo3);
}
}
第五步做一个稍微复杂一点的例子,对于WebServices不能仅仅对简单类型的数据进行操作,也应该能对复杂类型进行操作。下面的例子就是:
先要一个复杂类型的类
public class FooBean {
private String foo1;
private Integer foo2;
private Boolean foo3;
public FooBean(String foo1, Integer foo2, Boolean foo3) {
this.foo1 = foo1;
this.foo2 = foo2;
this.foo3 = foo3;
}
…. Setter and Getter
}
分享到:
相关推荐
axis-jaxrpc-1.4.jar jaxrpc.jar webservice java工具类jar包。
axis2-idea-plugin-1.7.9
axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,解压即可使用
目前axis2最高版本是2.0以上的版本,但是eclipse和myeclipse都不支持,无奈只能使用低版本的插件1.6.3;经实验,可以安装成功; 安装方法:右键解压到当前文件夹,Copy解压的文件到eclipse安装目录dropins下,重启...
共四个文件,都是最先版的,希望可以帮助大家。axis2-eclipse-service-archiver-wizard和axis2-eclipse-codegen-wizard和axis2-1.6.1-bin和axis2-1.6.1-war
axis2-kernel-1.3.jar axis2-kernel-1.3.jar
axis2c-bin-1.6.0-linux.tar.gz axis2c-bin-1.6.0-win32.zip axis2c-src-1.6.0.tar.gz axis2c-src-1.6.0.zip 加md5
axis2-1.6.2-war、axis2-1.6.1-war、axis2-1.6.2-bin
axis2-1.6.0-bin和axis2-1.6.0-war相关资源
axis2-std-1.0-bin.zip与axis2.war axis2-std-1.0-bin.zip与axis2.war
绝对可以用,里面配有myeclipse10安装AXIS2插件的步骤,希望大家工作愉快,谢谢,给好评哦
axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip axis2-1.5.3-bin.zip
Axis2-1.6.2-bin和Axis2-1.6.2-war
axis2-1.5.1-bin.zip axis2-1.5.1-war.zip webservice部署axis所需要是包
webservice 所需的jar包,包括axis.jar axis-saaj-1.4.jar,解压后导入即可
axis2-1.5.4-bin axis2-1.5.4-war
包括两个工具包axis2-1.4.1-bin.zip,和axis2-1.4.1-war.zip
Axis2--Webservice Axis2--Webservice Axis2--Webservice
axis2-eclipse-codegen-plugin-1.6.2+axis2-eclipse-service-plugin-1.6.2
1、axis2相关jar包如下: axiom-api-1.2.10.jar axiom-dom-1.2.10.jar axiom-impl-1.2.10.jar axis2-adb-1.5.4.jar axis2-adb-codegen-1.5.4.jar axis2-codegen-1.5.4.jar axis2-corba-1.5.4.jar axis2-fastinfoset-...