现在项目中大量应用了Webservice技术,与别的核心系统交互。之前没有用过Webservice,所以,了解下,做了个测试的例子。
在Java中开发Webservice可以使用很多的框架,我们使用的Aixs。
需要的包如下:
activation.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
mail.jar
saaj.jar
wsdl4j-1.5.1.jar
服务器端:建立接口和实现类:
接口类:
package server; import java.rmi.Remote; import com.cpic.b2bcore.externalinterface.wss.type.MusterCollectRequest; import com.cpic.b2bcore.externalinterface.wss.type.MusterCollectResponse; public interface ISayHello extends Remote { public MusterCollectResponse getPolicyInfo(MusterCollectRequest mrequest)throws java.rmi.RemoteException; }
实现类:
package server; import java.rmi.RemoteException; import com.cpic.b2bcore.externalinterface.wss.type.MusterCollectRequest; import com.cpic.b2bcore.externalinterface.wss.type.MusterCollectResponse; public class SayHello implements ISayHello { public MusterCollectResponse getPolicyInfo(MusterCollectRequest mrequest) throws RemoteException { // TODO Auto-generated method stub MusterCollectResponse mresponse = new MusterCollectResponse(); mresponse.setPublicpolicies(null); return mresponse; } }
可以看出,这里传入参数和返回参数都是自定义对象,属于较为复杂的 service。
需要 在web.xml 里面加入 AXIS 的配置参数。。
<display-name>Apache-Axis</display-name> <listener> <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class> </listener> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> </servlet> <servlet> <servlet-name>AdminServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AdminServlet </servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet> <servlet-name>SOAPMonitorService</servlet-name> <servlet-class> org.apache.axis.monitor.SOAPMonitorService </servlet-class> <init-param> <param-name>SOAPMonitorPort</param-name> <param-value>5001</param-value> </init-param> <load-on-startup>100</load-on-startup> </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-mapping> <servlet-name>SOAPMonitorService</servlet-name> <url-pattern>/SOAPMonitor</url-pattern> </servlet-mapping> <mime-mapping> <extension>wsdl</extension> <mime-type>text/xml</mime-type> </mime-mapping> <mime-mapping> <extension>xsd</extension> <mime-type>text/xml</mime-type> </mime-mapping>
编辑 wsdd 文件,用以生成 配置文件
<!-- Use this file to deploy some handlers/chains and services --> <!-- Two ways to do this: --> <!-- java org.apache.axis.client.AdminClient deploy.wsdd --> <!-- after the axis server is running --> <!-- or --> <!-- java org.apache.axis.utils.Admin client|server deploy.wsdd --> <!-- from the same directory that the Axis engine runs --> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <!-- Services from YwxCommonService WSDL service --> <service name="PolicyInfoService" provider="java:RPC" style="wrapped" use="literal"> <parameter name="className" value="server.SayHello"/> <parameter name="allowedMethods" value="getPolicyInfo"/> <parameter name="wsdlTargetNamespace" value="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx"/> <parameter name="schemaQualified" value="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx,http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx/types"/> <parameter name="wsdlServiceElement" value="PolicyInfoService"/> <parameter name="wsdlServicePort" value="PolicyInfoService"/> <parameter name="wsdlPortType" value="PolicyInfoCommService"/> <parameter name="typeMappingVersion" value="1.2"/> <operation name="getPolicyInfo" qname="operNS:getPolicyInfo" xmlns:operNS="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx" returnQName="retNS:musterCollectResponse" xmlns:retNS="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx" returnType="rtns:MusterCollectResponse" xmlns:rtns="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx/types" soapAction="" > <parameter qname="pns:musterCollectRequest" xmlns:pns="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx" type="tns:MusterCollectRequest" xmlns:tns="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx/types"/> </operation> <typeMapping xmlns:ns="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx/types" qname="ns:MusterCollectRequest" type="java:com.cpic.b2bcore.externalinterface.wss.type.MusterCollectRequest" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" /> <typeMapping xmlns:ns="http://wss.cpic.com.cn/propertyinsurance/commonservice/ywx/types" qname="ns:MusterCollectResponse" type="java:com.cpic.b2bcore.externalinterface.wss.type.MusterCollectResponse" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" /> </service> </deployment>
用于执行部署的批处理脚本:
@echo off set AXIS_HOME=D:\workspace\testServices\WebRoot\WEB-INF rem set AXIS_HOME cd %AXIS_HOME% rem echo current dir=%cd% set JAVA_HOME=D:\MyEclipse 6.5\jre rem set JAVA_HOME set CLASSPATH=%JAVA_HOME%\lib\tools.jar set CLASSPATH=.;%CLASSPATH% set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\activation.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\axis.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\axis-ant.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\commons-discovery-0.2.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\commons-logging-1.0.4.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\jaxrpc.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\log4j-1.2.8.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\mail.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\saaj.jar set CLASSPATH=%CLASSPATH%;%AXIS_HOME%\WebRoot\WEB-INF\lib\wsdl4j-1.5.1.jar rem set CLASSPATH set _RUNJAVA="%JAVA_HOME%\bin\java" set _RUNJAVAW="%JAVA_HOME%\bin\javaw" set _RUNJDB="%JAVA_HOME%\bin\jdb" set _RUNJAVAC="%JAVA_HOME%\bin\javac" set OUTPUT_PATH=%AXIS_HOME%\src echo 开始执行wsddjava... java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://localhost:8080/testServices/services/PolicyInfoService deploy.wsdd echo ...执行wsdd java完成 pause
执行该批处理文件,顺利完成后,部署成功。
客户端的生成 见另外的 一篇 博客 。。