先简短的说一下Web Service的概念,简略的总结一下:所谓Web Service就是客户端以标准的SOAP消息将服务请求发给服务提供者,不论服务提供者用什么样的技术,Java、EJB、或者.NET执行相应的程序得到结果,然后将结果以SOAP消息返回给服务请求者。
下面以Axis为例,创建一个简单的Web Service。首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-1.4的。下载之后观察其目录,lib下就是所要用到的所有jar包了。这里少2个jar包,分别是activation.jar和mail.jar,需要单独下载。
好了,现在我们创建一个Web Service项目,命名为webservice_begin。首先把lib目录下的所有jar包拷贝到WEB-INF的lib目录下。ok,现在我们创建接口和实现类。代码如下
接着我们修改下web.xml文件。修改web.xml文件的目的是为创建一个Axis的Servlet。Axis Servlet最核心的实现类为org.apache.axis.transport.http.AxisServlet,然后定义一个Servlet的请求路径为services/*,客户端就可以通过这个路径发布SOAP的请求消息了,下面是修改后的web.xml文件
接下来,要配置wsdd文件server-config.wsdd。server-config.wsdd将定义服务的名称,具体的实现类,以及发布的方法和属性等。将其放在src目录下,也就是当前应用的classpath路径下,具体内容如下
好了,现在我们就可以发布Web Service服务了。
部署成功后,启动你的应用服务器,在地址栏下输入
http://localhost:8080/webservice_begin/services,如果没有错误提示,恭喜你,你的Web Service已经发布成功了。点击wsdl,还可以看到具体的wsdl的配置信息。
这里面主要定义了以下内容:本例子的目标空间和命名空间、输入和输出变量的结构类型、请求和响应的消息、wsdl的接口portType、接口操作(Operation)、绑定的传输协议,具体内容如下
到这里,服务器端的工作算是完成了。下面我们写一个客户端程序来试着调用一下。客户端程序可以是一个简单的javaProject。调用代码如下
下面以Axis为例,创建一个简单的Web Service。首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-1.4的。下载之后观察其目录,lib下就是所要用到的所有jar包了。这里少2个jar包,分别是activation.jar和mail.jar,需要单独下载。
好了,现在我们创建一个Web Service项目,命名为webservice_begin。首先把lib目录下的所有jar包拷贝到WEB-INF的lib目录下。ok,现在我们创建接口和实现类。代码如下
- package server;
- public interface Hello {
- public String getHello(String name);
- }
- package server;
- public class HelloService implements Hello {
- public String getHello(String name) {
- return "hello, " + name;
- }
- }
接着我们修改下web.xml文件。修改web.xml文件的目的是为创建一个Axis的Servlet。Axis Servlet最核心的实现类为org.apache.axis.transport.http.AxisServlet,然后定义一个Servlet的请求路径为services/*,客户端就可以通过这个路径发布SOAP的请求消息了,下面是修改后的web.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" 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>
- </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>
- </web-app>
接下来,要配置wsdd文件server-config.wsdd。server-config.wsdd将定义服务的名称,具体的实现类,以及发布的方法和属性等。将其放在src目录下,也就是当前应用的classpath路径下,具体内容如下
- <?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="hello" provider="java:RPC">
- <parameter name="className" value="server.HelloService"/>
- <parameter name="allowedMethods" value="getHello"/>
- </service>
- <transport name="http">
- <requestFlow>
- <handler type="URLMapper"/>
- </requestFlow>
- </transport>
- </deployment>
好了,现在我们就可以发布Web Service服务了。
部署成功后,启动你的应用服务器,在地址栏下输入
http://localhost:8080/webservice_begin/services,如果没有错误提示,恭喜你,你的Web Service已经发布成功了。点击wsdl,还可以看到具体的wsdl的配置信息。
这里面主要定义了以下内容:本例子的目标空间和命名空间、输入和输出变量的结构类型、请求和响应的消息、wsdl的接口portType、接口操作(Operation)、绑定的传输协议,具体内容如下
- <?xml version="1.0" encoding="UTF-8" ?>
- - <wsdl:definitions targetNamespace="http://localhost:8080/webservice_begin/services/hello" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/webservice_begin/services/hello" xmlns:intf="http://localhost:8080/webservice_begin/services/hello" 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="getHelloRequest">
- <wsdl:part name="name" type="soapenc:string" />
- </wsdl:message>
- - <wsdl:message name="getHelloResponse">
- <wsdl:part name="getHelloReturn" type="soapenc:string" />
- </wsdl:message>
- - <wsdl:portType name="HelloService">
- - <wsdl:operation name="getHello" parameterOrder="name">
- <wsdl:input message="impl:getHelloRequest" name="getHelloRequest" />
- <wsdl:output message="impl:getHelloResponse" name="getHelloResponse" />
- </wsdl:operation>
- </wsdl:portType>
- - <wsdl:binding name="helloSoapBinding" type="impl:HelloService">
- <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- - <wsdl:operation name="getHello">
- <wsdlsoap:operation soapAction="" />
- - <wsdl:input name="getHelloRequest">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://server" use="encoded" />
- </wsdl:input>
- - <wsdl:output name="getHelloResponse">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/webservice_begin/services/hello" use="encoded" />
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- - <wsdl:service name="HelloServiceService">
- - <wsdl:port binding="impl:helloSoapBinding" name="hello">
- <wsdlsoap:address location="http://localhost:8080/webservice_begin/services/hello" />
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
到这里,服务器端的工作算是完成了。下面我们写一个客户端程序来试着调用一下。客户端程序可以是一个简单的javaProject。调用代码如下
- package junit.test;
- import java.net.URL;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.junit.Test;
- public class WebServiceTest {
- @Test public void testInvoke() throws Exception{
- //标识WebService的具体路径
- String endpoint = "http://localhost:8080/webservice_begin/services/hello";
- //创建Service实例
- Service service = new Service();
- //通过Service实例创建Call实例
- Call call = (Call)service.createCall();
- //将WebService的服务路径加入到Call实例中,并为Call设置服务的位置
- URL url = new URL(endpoint);
- call.setTargetEndpointAddress(url);
- //调用WebService方法
- call.setOperationName("getHello");
- //调用WebService传入参数
- String res = (String)call.invoke(new Object[] {"fengfeng925"});
- System.out.println(res);
- }
- }
转载自:点击打开链接
后期自己搭建一个发现还是缺少很多包,在此把缺少的jar包列出:
axis-ant.jar
axis.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j.jar
axis.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.8.jar
saaj.jar
wsdl4j.jar
后期写client端,client端需要的jar包有
axis.jar
axis-ant.jar
commons-discovery-0.2.jar
commons-logging.jar
jaxrpc.jar