标签:
下载axis1.4开发包,解压开发包,将webapps目录下的axis文件夹拷贝到tomcat的webapps目录下。启动tomcat,在浏览器输入http://localhost:8080/axis得到如下页面:
实用axis的发布服务平台,创建和发布WebService服务有两种方式:即时发布和定制发布。即时发布服务很少用,但是为了知识体系的完整性,这里还是讲一下。本篇文章只讲即时发布服务。
写一个没有包的Java类HelloWS.java:
public class HelloWS{ public String test(String a ,String b){ String result = "a="+a+",b="+b; return "server response ok ,u send "+result; } }
然后将此文件拷贝到tomcat_home\webapps\axis目录下,并将文件后缀改为jws。在浏览器输入http://localhost:8080/axis/HelloWS.jws得到如下页面:
点击链接,浏览器展现一个wsdl文档:
1 <wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/HelloWS.jws" xmlns:intf="http://localhost:8080/axis/HelloWS.jws" 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" targetNamespace="http://localhost:8080/axis/HelloWS.jws"> 2 <!-- 3 WSDL created by Apache Axis version: 1.4 4 Built on Apr 22, 2006 (06:55:48 PDT) 5 --> 6 <wsdl:message name="testResponse"> 7 <wsdl:part name="testReturn" type="xsd:string"/> 8 </wsdl:message> 9 <wsdl:message name="testRequest"> 10 <wsdl:part name="a" type="xsd:string"/> 11 <wsdl:part name="b" type="xsd:string"/> 12 </wsdl:message> 13 <wsdl:portType name="HelloWS"> 14 <wsdl:operation name="test" parameterOrder="a b"> 15 <wsdl:input message="impl:testRequest" name="testRequest"/> 16 <wsdl:output message="impl:testResponse" name="testResponse"/> 17 </wsdl:operation> 18 </wsdl:portType> 19 <wsdl:binding name="HelloWSSoapBinding" type="impl:HelloWS"> 20 <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> 21 <wsdl:operation name="test"> 22 <wsdlsoap:operation soapAction=""/> 23 <wsdl:input name="testRequest"> 24 <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/> 25 </wsdl:input> 26 <wsdl:output name="testResponse"> 27 <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloWS.jws" use="encoded"/> 28 </wsdl:output> 29 </wsdl:operation> 30 </wsdl:binding> 31 <wsdl:service name="HelloWSService"> 32 <wsdl:port binding="impl:HelloWSSoapBinding" name="HelloWS"> 33 <wsdlsoap:address location="http://localhost:8080/axis/HelloWS.jws"/> 34 </wsdl:port> 35 </wsdl:service> 36 </wsdl:definitions>
这就已经发布了服务。下面我们测试一下:我们新建一个Java工程,导入axis1.4开发包下的所有jar,然后写一个测试类Client:
1 package com.client; 2 3 import java.rmi.RemoteException; 4 5 import javax.xml.namespace.QName; 6 import javax.xml.rpc.ServiceException; 7 8 import org.apache.axis.client.Call; 9 import org.apache.axis.client.Service; 10 11 public class Client { 12 13 public static void main(String[] args) throws ServiceException, RemoteException { 14 15 String url = "http://localhost:8080/axis/HelloWS.jws"; 16 Service service = new Service(); 17 Call call = (Call) service.createCall(); 18 call.setTargetEndpointAddress(url); 19 call.setOperationName(new QName(url,"test")); 20 String result = (String) call.invoke(new Object[]{"Tomcat","Jetty"}); 21 System.out.println(result); 22 } 23 24 }
运行,控制台输出:
这就是即时发布。即时发布在实际应用中几乎不用。下篇文章我们讨论如何利用Apache提供的axis服务平台发布定制服务。
标签:
原文地址:http://www.cnblogs.com/build-up/p/4916022.html