作者:Vashon
一、准备工作
导入需要的jar包:
二、配置web.xml
在web工程的web.xml中添加如下配置:
[html] view plain copy
- <servlet>
- <servlet-name>AxisServlet</servlet-name>
- <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
- <load-on-startup>15</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
三、配置server-config.wsdd文件
在WEB-INF下添加WebService核心文件server-config.wsdd:
[html] view plain copy
- <?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="sendMultiRefs" value="true" />
- <parameter name="disablePrettyXML" value="true" />
- <parameter name="adminPassword" value="admin" />
- <parameter name="attachments.Directory"
- value="D:\myproject\calileo\xxx\WebContent\WEB-INF\attachments" />
- <parameter name="dotNetSoapEncFix" value="true" />
- <parameter name="enableNamespacePrefixOptimization" value="true" />
- <parameter name="sendXMLDeclaration" value="true" />
- <parameter name="attachments.implementation"
- value="org.apache.axis.attachments.AttachmentsImpl" />
- <parameter name="sendXsiTypes" value="true" />
- <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" />
- <!-- 核心重点:服务发布,name:表示发布的service名 -->
- <service name="SjrkkService" provider="java:RPC">
- <!-- 发布的service接口类 -->
- <parameter name="className" value="com.xxx.ws.SjrkkService" />
- </service>
- <transport name="http">
- <requestFlow>
- <handler type="URLMapper" />
- <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
- </requestFlow>
- <parameter name="qs:list"
- value="org.apache.axis.transport.http.QSListHandler" />
- <parameter name="qs:wsdl"
- value="org.apache.axis.transport.http.QSWSDLHandler" />
- <parameter name="qs.list"
- value="org.apache.axis.transport.http.QSListHandler" />
- <parameter name="qs.method"
- value="org.apache.axis.transport.http.QSMethodHandler" />
- <parameter name="qs:method"
- value="org.apache.axis.transport.http.QSMethodHandler" />
- <parameter name="qs.wsdl"
- value="org.apache.axis.transport.http.QSWSDLHandler" />
- </transport>
- <transport name="local">
- <responseFlow>
- <handler type="LocalResponder" />
- </responseFlow>
- </transport>
- </deployment>
四、测试WSDL
在浏览器输入:http://localhost:8080/xxx/services/SjrkkService?wsdl //xxx和server-config.wsdd文件中的xxx一致,访问结果如下:
五、调WebService
[java] view plain copy
- /**
- * 调用webservice的方法.
- * @param url wsdl链接
- * @param method 调用的接口
- * @param idCard 传入的参数
- * @return
- * @throws Exception
- */
- private String getWebServiceResult( String url, String method, String idCard) throws Exception{
- String rtnXml = null;
- try {
- String endpoint = url;
- Service service = new Service();
- Call call = (Call) service.createCall();
- call.setTargetEndpointAddress(new java.net.URL(endpoint));
- call.setOperationName(method);
- rtnXml = (String) call.invoke(new Object[]{idCard});
- } catch (Exception e) {
- e.printStackTrace();
- }
- return rtnXml;
- }
备注:调用结果就不演示了,到这里读者应该明白是怎么调了。
时间: 2024-10-23 19:31:56