WSDL:Web Service Description Language
环境:Tomcat5.0.28+Axis1.4+MyEclipse6.5
对于前两种 Web Service的发布基本一样,客户端的访问也很类似,第一种发布就是直接将.java后缀改为.jws,并将生成的.class文件拷贝到WEB- INF/jwsclasses下面,这样的例子直接在Axis上就有,非常简单,但它也有缺陷,就是不适合程序部署和大型项目开发,而且不支持包(package)的形式(这个只是我的个人见解),第三种是目前比较流行的方式,stub意思是树桩,意味着服务端和客户端都是通过桩的形式来完成访问的,即在服务端将java转换成wsdl,在客户端将wsdl装换成java,这样就实现了良好的桩的分离。
二、 Dynamic Invocation Interface ( DII)开发方式
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
|
就是说如果向容器请求/servlet/AxisServlet、*.jws、/services/*这几种映射会引起调用AxisServlet这个类,我们要用的就这几行,其他的可以直接删掉。
public class myService {
public String getusername(String name){
return "Hello "+name+",this is an Axis DII Web Service";
}
}
|
5. 启动Tomcat输入http://localhost:8089/testaxis/myService.jws?wsdl,点击Click to see the WSDL,如果看到如下界面就表示你已经成功发布一个Web Service了,就这么简单,如果出现错误就表示你的配置错误,或者是你的web.xml有问题,或者是你的包引用有问题,多试几遍这个问题很容易搞定的,这个一定要正确,不然接下来的客户端就访问不到了,这时候你会在WEB-INF下面看到Axis会自动生成一个jwsClasses文件夹,jwsClasses下面会有一个myService.class文件。
/**
* */ package com.axistest; import java.net.MalformedURLException;
import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call;
import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class myServiceTestorByjws {
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException { String endpoint="http://localhost:8089/testaxis/myService.jws"; String name="毛毛"; Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.addParameter("param1",XMLType.XSD_STRING,ParameterMode.IN); call.setOperationName( "getusername" ); call.setReturnType( XMLType.XSD_STRING ); String ret = (String) call.invoke( new Object[] { name } ); System.out.println("返回结果:" + ret); } } |
1. 为了更好的区分,我们将myService放入com.service包下,这就是WSDD的好处,它不像DII不能建包,并修改代码如下
package com.service;
public class MyserviceDP {
public String getusername(String name){ return "Hello "+name+",this is an Axis Dynamic Proxy Web Service"; } } |
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/> <service name="myService" provider="java:RPC"> <parameter name="className" value="com.service.MyserviceDP"/> <parameter name="allowedMethods" value="getusername"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> </requestFlow> </transport> </deployment> |
http://localhost:8089/testaxis/servlet/AxisServlet;如果出现如下界面表示你的Web Service发布成功( 点 myServiceDP (wsdl) )
package com.axistest;
import java.net.MalformedURLException; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class myServiceTestorByWSDD { public static void main(String[] args) throws ServiceException,MalformedURLException, RemoteException { String endpoint = "http://localhost:8089/testaxis/services/myServiceDP"; Service service = new Service(); // 创建一个Service实例,注意是必须的! Call call = (Call) service.createCall(); // 创建Call实例,也是必须的! call.setTargetEndpointAddress(new java.net.URL(endpoint));// 为Call设置服务的位置 call.setOperationName("getusername"); // 注意方法名与JavaBeanWS.java中一样!! String res = (String) call.invoke(new Object[] { "毛毛" }); // 返回String,传入参数 System.out.println(res); } } |
5. 运行结果:Hello 毛毛,this is an Axis Dynamic Proxy Web Service
<?xml version="1.0" encoding="UTF-8"?> <project name="Generate WSDL from JavaBeans as Web Services" default="j2w-all" basedir="."> <property name="build.dir" value="../../WebRoot/WEB-INF/classes"/> <property name="axis.dir" location="D:/Downloads/axis-src-1_4/axis-1_4"/> <path id="classpath.id"> <fileset dir="${axis.dir}/lib"> <include name="*.jar"/> </fileset> <pathelement location="${build.dir}"/> </path> <taskdef name="axis-java2wsdl" classname="org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask" loaderref="axis" > <classpath refid="classpath.id"/> </taskdef> <target name="j2w-all"> <antcall target="j2w-JavaBeanWS"/> </target> <target name="j2w-JavaBeanWS"> <axis-java2wsdl classname="com.service.MyServiceDP" classpath="${build.dir}" methods="getusername" output="myService.wsdl" location="http://localhost:8089/testaxis/services/myServiceDP" namespace="http://localhost:8089/testaxis/services/myServiceDP" namespaceImpl="http://localhost:8089/testaxis/services/myServiceDP"> </axis-java2wsdl> </target> </project> |
<?xml version="1.0" encoding="UTF-8"?> <project name="wsclient" default="all" basedir="."> <property name="axis.home" location="D:/Downloads/axis-src-1_4/axis-1_4"/> <property name="options.output" location="../wsdl2java"/> <path id="axis.classpath"> <fileset dir="${axis.home}/lib"> <include name="**/*.jar"/> </fileset> </path> <taskdef resource="axis-tasks.properties" classpathref="axis.classpath" /> <target name="-WSDL2Axis" depends="init"> <mkdir dir="${options.output}"/> <axis-wsdl2java output="${options.output}" url="${options.WSDL-URI}" verbose="true"/> </target> <target name="init"> <echo>Warning: please update the associated WSDL file(s) in the folder wsdl before running the target!</echo> <echo>Warning: Just run the target(s) related with your developing work!</echo> <echo></echo> </target> <target name="all"> <antcall target="myService"/> </target> <target name="myService"> <antcall target="-WSDL2Axis"> <param name="options.WSDL-URI" location="../src/java2wsdl/myService.wsdl"/> </antcall> </target> </project> |
5. 编写客户端测试代码,在com.axistest新建一个myServiceTestorByStubs.java,内容如下:
package com.axistest; public class myServiceTestorByStubs { |
运行结果:Hello 毛毛,this is an Axis Dynamic Proxy Web Service(当然这里的“Dynamic Proxy ”就不准确了。)
工程结构如下: