这段时间重构一下原来的代码,把东西整理一下。这个为AXIS的整理
webService 是一种基于http协议的组件
webService 开发框架:AXIS(Apache eXtensible Interaction System),xFire,
WSDD就是WEB服务分布描述(Web Service Deployment Descriptor), 它定义了WEB服务的接口,如服务名、提供的方法、方法的参数等信息。
AXIS 发布的两种方式
一,即时发布 jws java web service 只要把.java 文件改成.jws文件,发布就可以了,但是不支持带包类
二,定制发布 wsdd web service Deployment Descriptor 这个发布需要配置文件server-config.wsdd文件
针对AXIS集成到Spring 的项目中。
一个简单的AXIS应用
1, 需要的jar 包
activation.jar
axis.jar
axis-ant.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.15.jar
mail.jar
wsdl4j.jar
spring.jar
.......
这里其他包就省略了
我用的框架是struts2+spring +.....
2, web.xml配置
<!-- axis webservice配置 start-->
<servlet> <servlet-name>AdminServlet</servlet-name>
<!-- <display-name>Axis Admin Servlet</display-name> -->
<servlet-class> org.apache.axis.transport.http.AdminServlet </servlet-class>
</servlet>
<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>
<servlet-name>SOAPMonitorService</servlet-name>
<!-- <display-name>SOAP Monitor Service</display-name> -->
<servlet-class> org.apache.axis.monitor.SOAPMonitorService </servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name> <param-value>5001</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping> <servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern> </servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern> </servlet-mapping>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet
</url-pattern>
</servlet-mapping>
<!-- axis webservice配置 end-->
3,定制webservice 服务端接口
(1) 接口类
package com.ot.test.service; import java.util.HashMap; import java.util.List; /** * 统一对外服务接口 * */ public interface ITest { public String test() throws Exception; }
(2)接口实现类
package com.ot.test.service; import java.util.HashMap; import java.util.List; /** * 统一对外服务接口 * */ public class TestImpl implements ITest { //todo private ITestDAO testDao; public String test() throws Exception { //todo 调用DAO testDao.test(); } }
这里针对DAO 就不在描述了,这个服务只是一个业务接口。
(3)定义一个针对Webservice 服务的接口实现
在 Spring 中对 Web Service 进行封装很简单,仅仅需要继承
org.springframework.remoting.jaxrpc.ServletEndpointSupport 类,实现里面的一些方法,包装一次,将其发布出来就可以。
package com.ot.test.service; import java.util.HashMap; import java.util.List; /** * 统一对外服务接口 * */ public class TestImplAdaptor extends ServletEndpointSupport implements ITest { //todo private ITest test; public String test() throws Exception { //todo 调用test test.test(); } }
以上已经定义好了服务端接口,下面需要对AXIS WEBSERVICE进行部署
(4)下面的部署我采用了ant 来部署的 ,其中一个重要的包axis-ant.jar现在需要了。
<?xml version="1.0" encoding="UTF-8" ?> <project name="testclient" default="tojar" basedir="../wsdl"> <property name="wsdl" value="test.wsdl" /> <path id="classpath"> <fileset dir="../WEB-INF/lib" includes="*.jar,*.zip" /> <fileset dir="D:"tomcat6.0"lib" includes="*.jar,*.zip" /> </path> <target name="copy" > <copy todir="${basedir}/test/src"> <fileset dir="../../src"> <include name="**"*.java" /> </fileset> </copy> </target> <!-- **********************************编译********************************** --> <target name="compile" description="Compile Java sources" depends="copy"> <javac srcdir="${basedir}/test/src" destdir="${basedir}/test/classes" optimize="yes" encoding="utf-8"> <classpath refid="classpath" /> <compilerarg value="-Xlint:none" /> </javac> </target> <target name="axis" depends="compile"> <taskdef resource="axis-tasks.properties" classpathref="classpath" /> <axis-java2wsdl classname="com.ot.service.ITest" location="http://localhost:8080/services/testService" namespace="http://service.ot.com" output="${wsdl}" style="RPC"> <classpath> <pathelement path="${basedir}/opf/classes" /> </classpath> </axis-java2wsdl> <axis-wsdl2java all="true" url="${wsdl}" deployscope="Request" output="${basedir}/client" serverside="true" skeletondeploy="false" testcase="true" noimports="false" typemappingversion="1.3"> </axis-wsdl2java> <delete file="${basedir}/client/service/ITestServiceLocator.java" /> <delete file="${basedir}/client/service/undeploy.wsdd" /> <delete file="${basedir}/client/service/deploy.wsdd" /> </target> <target name="tojar" depends="axis"> <javac destdir="${basedir}/classes" debug="on" optimize="on" classpathref="classpath"> <src path="${basedir}/client" /> </javac> <jar jarfile="../clientdate/testclient.jar"> <fileset dir="${basedir}/classes"> <include name="**/*.class" /> <exclude name="**/*Locator.class" /> </fileset> </jar> <delete dir="${basedir}/opf" /> </target> </project>
针对上面的ant build.xml文件要说明一下啊,整个过程就是把webservice中需要用到的实体对象,复制出来编译一下,提供给客户端使用webservice的用户。再webservice中复杂的对象,不是java基本类型的,wsdl无法直接转换,需要序列化。
这就是为什么要生成这个客户端,其中下面部分要重点说明一下,这也是这个部署的核心。通过axis-java2wsdl 是将webservice 接口生成相应的描述文件和客户端调用代码。执行后会多生成几个类,
ITest.java,ITestService.java,ITestServiceLocator.java,ITestSoapBindingStub.java ,以及undeploy.wsdd 和deploy.wsdd 以及test.wsdl 三个配置文件 但是我会把ITestServiceLocator.java 会删除了,原因是我在服务端自己定义了一个这样的类,目的是我可以自己修改服务单的地址,这样对于开发而言服务端地址改变不需要重新生成客户端代码。 undeploy.wsdd 和deploy.wsdd 两个文件是部署和取消部署的文件,这里我也不他们删除,我要手动定义这个文件
<target name="axis" depends="compile">
<taskdef resource="axis-tasks.properties" classpathref="classpath" />
<axis-java2wsdl classname="com.ot.service.ITest" location="http://localhost:8080/services/testService" namespace="http://service.ot.com" output="${wsdl}" style="RPC">
<classpath>
<pathelement path="${basedir}/opf/classes" />
</classpath>
</axis-java2wsdl>
<axis-wsdl2java all="true" url="${wsdl}" deployscope="Request" output="${basedir}/client" serverside="true" skeletondeploy="false" testcase="true" noimports="false" typemappingversion="1.3">
</axis-wsdl2java>
<delete file="${basedir}/client/service/ITestServiceLocator.java" />
<delete file="${basedir}/client/service/undeploy.wsdd" />
<delete file="${basedir}/client/service/deploy.wsdd" />
</target>
(5)服务器部署配置文件server-config.wsdd <?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassword" value="admin" /> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" /> <parameter name="disablePrettyXML" value="true" /> <parameter name="sendXsiTypes" value="true" /> <parameter name="sendMultiRefs" value="true" /> <parameter name="sendXMLDeclaration" value="true" /> <parameter name="axis.sendMinimizedElements" value="true" /> <parameter name="dotNetSoapEncFix" value="true" /> <parameter name="enableNamespacePrefixOptimization" value="false" /> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session" /> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request" /> <parameter name="extension" value=".jwr" /> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" /> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" /> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" /> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService" /> <parameter name="enableRemoteAdmin" value="false" /> <parameter name="className" value="org.apache.axis.utils.Admin" /> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion" /> <parameter name="className" value="org.apache.axis.Version" /> </service> <service name="test" provider="java:RPC" style="rpc" use="encoded"> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="Request" /> <parameter name="className" value="com.ot.service.impl.TestImplAdaptor " /> <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns1:Object" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:java.lang.Object" xmlns:ns1="http://lang.java" /> <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns2:List" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:java.util.ArrayList" xmlns:ns2="http://util.java" /> <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns3:TestVO" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.ot.model.TestVO" xmlns:ns3="http://model.ot.com" /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper" /> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" /> </requestFlow> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder" /> </responseFlow> </transport> </deployment>
指定好部署文件服务端已经全部构建好了,下面我们来构建客户端
(6)客户端有两种方式
(6-1) 客户端直接调用Webservice,就不需要刚才生成的***Locator.java 和 ***SoapBindingStub.java....等
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class WSTestServiceClient{
public static void main(String []args){
Service service = new Service();
try {
Call call = (Call) service.createCall();
String url = http://localhost:8080/services/testService?wsdl;
QName qname = new QName(url,"test");
call.setOperationName(qname);
call.setTargetEndpointAddress(new java.net.URL(url));
String result = (String) call.invoke(new Object[]{});
System.out.println("===="+result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
(6-2) 就是通过刚才客户端定义的***Locator.java 和 ***SoapBindingStub.java....等方法来调用
客户端同样也增加了一个使用WEB SERVICE的接口,这样的目的是代码结构更完整。具体接口不再说明。。
主要是相应的spring 配置。看如何调用相应的web service
<!-- 调用服务配置 --> <bean id="testAdaptor" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="targetSource" ref="axistestPoolTargetSource" /> <!--<property name="interceptorNames"> <list> <value>methodCachePointCut</value> <value>methodCachePointCutAdvice</value> </list> </property> --> </bean> <bean id="axistestPoolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource"> <property name="targetBeanName" value="axistestServiceImpl" /> <property name="maxSize" value="30" /> </bean> <bean id="axistestServiceImpl" class="com.ot.client.impl.TestServcieAdaptor" singleton="false"> <property name="testService"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" > <property name="singleton"><value>false</value></property> <property name="targetObject"> <bean class="com.ot.client.ITestServiceLocator"></bean> </property> <property name="targetMethod"><value>getTestService</value></property> </bean> </property> </bean>