spring的强悍之处就在与它能和很多框架进行无缝的结合;在这里讲述一下它和axis的步骤(注意是axis不是axis2);
为了能说明问题,以一个demo为例;说到webservice自然不外乎服务端和客户端的开发了 ,在这里分别说明;
一 服务端的开发:
1 把需要的jar放到工程目录下(必须的包为:wsdl4j-1.5.1.jar,spring.jar,saaj.jar,mail-1.4.jar,log4j-1.2.8.jar,
jaxrpc.jar,commons-logging-1.0.4.jar,commons-discovery-0.2.jar,axis-ant.jar,axis.jar,activation-1.1.jar)
2 开发源代码:因为这一步和业务相关,在这里不做讨论,但是必须有一个类继承ServletEndpointSupport,该类将发布成webService
具体的代码形如:
package com.ecgit.eccm.webservice; import javax.xml.rpc.ServiceException; import javax.xml.rpc.server.ServiceLifecycle; import org.springframework.remoting.jaxrpc.ServletEndpointSupport; import com.skywin.service.MyService; public class InterchangeDataServiceImpl extends ServletEndpointSupport { private MyService service; protected void onInit() throws ServiceException { service=(MyService) getApplicationContext().getBean("MyService"); } public String getMonthInterchange(String marketDate) { System.out.println("到达了Facade"); service.doTrans(marketDate); return marketDate; } }
业务层代码(仅仅写实现类,接口比较简单,所以省略):
package com.skywin.service; import com.skywin.dao.MyDao; public class MyServiceImpl implements MyService{ private MyDao myDao; public void doTrans(String message) { myDao.doTrans(message); System.out.println("服务层运行了"+this.getClass().getName()+"----"+message); } public MyDao getMyDao() { return myDao; } public void setMyDao(MyDao myDao) { this.myDao = myDao; } }
持久层代码(仅仅写实现类,接口比较简单,所以省略)
3 在web-inf目录下,建立一个名字为server-config.wsdd的文件,里面指定那些包的哪些类的哪些方法发布成webService,内容如下
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" /> <!-- 系统服务 --> <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="MPSWebService" provider="java:RPC"> <parameter name="className" value="com.ecgit.eccm.webservice.InterchangeDataServiceImpl" /> <parameter name="allowedMethods" value="*" /> </service> <!-- <beanMapping languageSpecificType="java:com.skywin.mps.sa.model.SPParamObject" qname="ns1:SPParamObject" xmlns:ns1="urn:BeanService" /> <beanMapping languageSpecificType="java:com.skywin.mps.sa.model.OrderSubItem" qname="ns2:OrderSubItem" xmlns:ns2="urn:BeanService" /> <beanMapping languageSpecificType="java:java.util.List" qname="ns3:List" xmlns:ns3="urn:BeanService" /> <beanMapping languageSpecificType="java:java.util.ArrayList" qname="ns4:ArrayList" xmlns:ns4="urn:BeanService" /> --> <transport name="http"> <requestFlow> <handler type="URLMapper" /> </requestFlow> </transport> </deployment>
4在web-inf目录下,建立spring配置文件(名字为applicationContext.xml),内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="serviceFace" class="com.ecgit.eccm.webservice.InterchangeDataServiceImpl"></bean> <bean id="MyService" class="com.skywin.service.MyServiceImpl"> <property name="myDao" ref="myDao"></property> </bean> <bean id="myDao" class="com.skywin.dao.MyDaoImpl"></bean> </beans>
5 修改web.xml文件,主要是目的是增加一个和axis相关的servlet和启动spring ,内容大家都很熟悉了,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
到这里服务端的开发完毕了,如果每一步都没有问题,则将工程发布到应用服务器,在地址栏中输入:http://localhost:8080/aspring/services/MPSWebService?wsdl 回车就可以看到刚才分布的服务了;
二 客户端的开发:
其实客户端没有怎么开发可言的,主要的工作就是利用服务的uri(即上面说的http://localhost:8080/aspring/services/MPSWebService?wsdl) 生成几个类,然后利用生成的类,传入参数就可以了,大体的步骤如下:
1 把 需要的jar包引入工程,需要的jar基本上和服务端的一样,只是不需要spring.jar而已,在这里不在赘述;
2 利用服务端提供的uri生成类:
需要输入的命令如下
java org.apache.axis.wsdl.WSDL2Java http://localhost:8080/aspring/services/MPSWebService?wsdl -p com.skywin.mps.webservice.client
其中org.apache.axis.wsdl.WSDL2Java为包axis里面的类,用户需要进入到对应目录执行上述的命令;该类在编译时候需要 其他的类,所以在执行上述命令中需要把下面的包加到classpath中 commons-logging-1.0.4.jar,commons-discovery-0.2.jar,jaxrpc.jar,saaj.jar,wsdl4j-1.5.1.jar
3 执行第2步的命令后,在client端可以得到四个类(InterchangeDataServiceImpl,InterchangeDataServiceImplService,InterchangeDataServiceImplServiceLocator,MPSWebServiceSoapBindingStub);
4,传入参数来调用服务端的服务:代码如下:
package aspring; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import com.skywin.mps.webservice.client.InterchangeDataServiceImpl; import com.skywin.mps.webservice.client.InterchangeDataServiceImplService; import com.skywin.mps.webservice.client.InterchangeDataServiceImplServiceLocator; public class Test { public static void main(String[] args) throws ServiceException, RemoteException { InterchangeDataServiceImplService server= new InterchangeDataServiceImplServiceLocator(); InterchangeDataServiceImpl serverDo=server.getMPSWebService(); serverDo.getMonthInterchange("funk"); } }
另外客户端也可以直接书写如下程序,运行下面的类也可以得到和上面一样的结果
package aspring; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class Test2 { public static void main(String[] args) { try { String wsdlUrl= "http://localhost:8080/aspring/services/MPSWebService?wsdl"; String nameSpaceUri = "http://localhost:8080/aspring/services/MPSWebService"; // 创建调用对象 <o:p> </o:p> Service service = new Service(); Call call = null; call = (Call) service.createCall(); // 调用 getMessage System.out.println(">>>getMessage"); call.setOperationName(new QName(nameSpaceUri, "getMonthInterchange")); call.setTargetEndpointAddress(new java.net.URL(wsdlUrl)); String ret = (String) call.invoke(new Object[] { "ABC" }); System.out.println("return value is " + ret); } catch (Exception e) { e.printStackTrace(); } } }
三下载说明:asrping为服务端工程,aspringClient为客户端工程;鉴于流量问题,两个工程中的lib目录中都没有jar包,如果想下载的 话,只需要把lib_001,lib_002中的包放到工程的lib目录就可以了(由于流量问题spring.jar和axis.jar没有上传)