转自:http://www.itkeyword.com/doc/7529577946427268306/Apache-Servlet-WebSOAPWebService
所谓Web Service就是客户端以标准的SOAP消息将服务请求发给服务提供者,不论服务提供者用什么样的技术,Java、EJB、或者.NET执行相应的程序得到结果,然后将结果以SOAP消息返回给服务请求者。以axis为例,创建一个简单的Web Service
首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-src-1_4.zip
NEWS (April 22, 2006): Axis 1.4 Final is now available!
第一步:创建WebService项目,命名为Webservice_Begin
解压axis-src-1_4.zip后,拷贝lib目录下最基本的jar包至项目WEB-INF/lib目录下,如下列表:
axis.jar
commons-discovery-0.2.jar
commons-logging-1.1.jar
jaxrpc.jar
mailapi_1_3_1.jar
wsdl4j-1.5.1.jar
junit-4.9-SNAPSHOT-20100512-0041.jar
第二步:创建WebService服务器端接口和实现类
- package com.unis.p2p.server;
- /**
- * WebService服务器端业务逻辑接口
- *
- * @author Posey 2010-12-17
- */
- public interface Hello {
- /**
- * 接口方法:接收客户端WebService请求
- *
- * @author Posey 2010-12-17
- * @param message
- * @return
- */
- public String executeTaskList(String message);
- }
- package com.unis.p2p.server;
- /**
- * WebService服务器端业务逻辑实现类
- * @author Posey 2010-12-17
- *
- */
- public class HelloImpl implements Hello {
- /**
- * 接收客户端WebService请求,具体执行相关操作
- *
- * @author Posey 2010-12-17
- */
- public String executeTaskList(String message) {
- return "OK|调用成功! " + message;
- }
- }
第三步:修改WEB-INF/web.xml配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- ********************************************** -->
- <!-- WebService核心处理类 -->
- <servlet>
- <servlet-name>AxisServlet</servlet-name>
- <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- <!-- ********************************************** -->
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
第四步:创建WebRoot/WEB-INF/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">
- <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper" />
- <service name="executeTask" provider="java:RPC">
- <parameter name="className" value="com.unis.p2p.server.HelloImpl" />
- <parameter name="allowedMethods" value="executeTaskList" />
- </service>
- <transport name="http">
- <requestFlow>
- <handler type="URLMapper" />
- </requestFlow>
- </transport>
- </deployment>
第五步:启动应用服务器,发布Web Service服务。
地址栏输入:http://127.0.0.1:8080/Webservice_Begin/services回车,如果没有错误提示,则恭喜你,你的Web Service已经发布成功。
点击wsdl,还可以看到具体的wsdl的配置信息如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- - <wsdl:definitions targetNamespace="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:intf="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- - <!--
- WSDL created by Apache Axis version: 1.4
- Built on Apr 22, 2006 (06:55:48 PDT)
- -->
- - <wsdl:message name="executeTaskListResponse">
- <wsdl:part name="executeTaskListReturn" type="soapenc:string" />
- </wsdl:message>
- - <wsdl:message name="executeTaskListRequest">
- <wsdl:part name="message" type="soapenc:string" />
- </wsdl:message>
- - <wsdl:portType name="HelloImpl">
- - <wsdl:operation name="executeTaskList" parameterOrder="message">
- <wsdl:input message="impl:executeTaskListRequest" name="executeTaskListRequest" />
- <wsdl:output message="impl:executeTaskListResponse" name="executeTaskListResponse" />
- </wsdl:operation>
- </wsdl:portType>
- - <wsdl:binding name="executeTaskSoapBinding" type="impl:HelloImpl">
- <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- - <wsdl:operation name="executeTaskList">
- <wsdlsoap:operation soapAction="" />
- - <wsdl:input name="executeTaskListRequest">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://server.p2p.unis.com" use="encoded" />
- </wsdl:input>
- - <wsdl:output name="executeTaskListResponse">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" use="encoded" />
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- - <wsdl:service name="HelloImplService">
- - <wsdl:port binding="impl:executeTaskSoapBinding" name="executeTask">
- <wsdlsoap:address location="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" />
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
第六步:创建WebService客户端测试类
/Webservice_Begin/test/com/unis/p2p/client/WebServiceTest.java
- package com.unis.p2p.client;
- import java.net.URL;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.junit.Test;
- /**
- * 类说明:Client端测试类,发出请求
- * @author Posey 2010-12-17
- *
- */
- public class WebServiceTest {
- @Test
- public void testExecuteTask() throws Exception {
- String res = "";
- String ss = "ERR|2352|5683|2235|8428";
- String[] str = ss.split("\\|");
- String tempMsg = "";
- for (int i = 1; i < str.length; i++) {
- tempMsg += str[i] + ",";
- }
- tempMsg = tempMsg.substring(0, tempMsg.length()-1);
- //调接口
- res = this.executeTask("http://127.0.0.1:8080/Webservice_Begin/services/executeTask",
- "executeTaskList",
- new Object[]{ tempMsg });
- System.out.println(res); //输出结果
- }
- //核心处理方法
- private synchronized String executeTask(String url, String method, Object[] args) throws Exception {
- // 创建Service实例
- Service service = new Service();
- // 通过Service实例创建Call实例
- Call call = (Call) service.createCall();
- // 将WebService的服务路径加入到Call实例中,并为Call设置服务的位置
- URL webServiceUrl = new URL(url);
- call.setTargetEndpointAddress(webServiceUrl);
- // 调用WebService方法
- call.setOperationName(method);
- // 调用WebService传入参数
- String result = (String) call.invoke(args);
- return result;
- }
- }
第七步:Run As - JUnit Test,执行成功