当前位置: 代码迷 >> Web前端 >> webServices学习之一――axis1兑现helloWorld
  详细解决方案

webServices学习之一――axis1兑现helloWorld

热度:187   发布时间:2012-12-28 10:29:04.0
webServices学习之一――axis1实现helloWorld

上周由于工作需要,通过webservices发布服务,进行数据交换。于是果断找出09年在丽水开发时的学习笔记,webservice服务采用开源组件axis1的1.3版本实现:

?

一,新建web项目,并修改web.xml文件,将如下代码cope到web.xml文件中

?

 <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>  
?

? ? 二、在web-inf目录下新增server-config.wsdd文件,文件内容如下:

?

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
            xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
            xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler">

 <globalConfiguration>
   <parameter name="disablePrettyXML" value="true"/>
   <parameter name="dotNetSoapEncFix" value="true"/>
   <parameter name="enableNamespacePrefixOptimization" value="false"/>
   <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>
     <!-- uncomment this if you want the SOAP monitor -->
     <!--
     <handler type="java:org.apache.axis.handlers.SOAPMonitorHandler"/>
     -->
   </requestFlow>
   <!-- uncomment this if you want the SOAP monitor -->
   <!--
   <responseFlow>
     <handler type="java:org.apache.axis.handlers.SOAPMonitorHandler"/>
   </responseFlow>
   -->
 </globalConfiguration>

 <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>
 <handler type="java:org.apache.axis.transport.local.LocalResponder" name="LocalResponder"/>
 <handler type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" name="Authenticate"/>

 <service name="AdminService" provider="java:MSG">
  <namespace>http://xml.apache.org/axis/wsdd/</namespace>
  <parameter name="allowedMethods" value="AdminService"/>
  <parameter name="enableRemoteAdmin" value="false"/>
  <parameter name="className" value="org.apache.axis.utils.Admin"/>
 </service>

<!--具体的web service配置 -->
 <service name="Version" provider="java:RPC">
  <parameter name="allowedMethods" value="getVersion"/>
  <parameter name="className" value="org.apache.axis.Version"/>
 </service>
 <service name="MyServices" provider="java:RPC">
  <parameter name="allowedMethods" value="*"/>
  <parameter name="className" value="haohao.MyServices"/>
 </service>


 <transport name="http">
  <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:wsdl" 
               value="org.apache.axis.transport.http.QSWSDLHandler"/>
  <requestFlow>
   <handler type="URLMapper"/>
   <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
  </requestFlow>
 </transport>

 <transport name="local">
  <responseFlow>
   <handler type="LocalResponder"/>
  </responseFlow>
 </transport>

</deployment>

?

三、在server-config.wsdd中配置要发布的文件的内容:

?

? ? ? ?<service name="MyServices" provider="java:RPC">

<parameter name="className" value="haohao.MyServices"/>

? <parameter name="allowedMethods" value="*"/>

? </service>

?

其中,services标签的name属性配置,访问发布的文件的名字。如:http://localhost:8888/MyTest/services/MyServices?wsdl. 中的MyServices,就是上面配置文件中的name属性值。

parameter标签的name的allowedmethods,value="*" 指所有的方法。 parameter标签的name的className, value="haohao.MyServices" 指要发布的文件。

?

?

四、加入jar文件,可以在附件中获得。

?

?

五、启动tomcat,访问地址:

http://localhost:8080/axisTest/services/MyServices?wsdl

?

结果为:

?

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axisTest/services/MyServices" xmlns:intf="http://localhost:8080/axisTest/services/MyServices" 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/axisTest/services/MyServices">
<!--
WSDL created by Apache Axis version: 1.3 Built on Oct 05, 2005 (05:23:37 EDT)
-->
<wsdl:message name="sayHelloRequest"></wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="MyServices">
<wsdl:operation name="sayHello">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MyServicesSoapBinding" type="impl:MyServices">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://haohao" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axisTest/services/MyServices" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyServicesService">
<wsdl:port binding="impl:MyServicesSoapBinding" name="MyServices">
<wsdlsoap:address location="http://localhost:8080/axisTest/services/MyServices"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
#next_pages_container { width: 5px; hight: 5px; position: absolute; top: -100px; left: -100px; z-index: 2147483647 !important; }

?

?

服务端 发布完成。

?

?

?

六、客户端:

?

TestClient.java

package haohao;


import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestClient {
	
	//调用的webservice url路径
	private static String serviceUrl = "http://localhost:8080/axisTest/services/MyServices?wsdl";
	public String heihei(String name){
		String ret = null;
		try{
			Service service = new Service();
			Call cc = (Call) service.createCall();
			cc.setTargetEndpointAddress(new java.net.URL(serviceUrl));
			cc.setOperationName("sayHello");
			ret = (String) cc.invoke(new Object [] {name});
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return ret;
	}
	
	public static void main(String args[]) {
		//调用OA系统的登录表,验证OA用户登陆
		TestClient client = new TestClient();
		
		System.out.println(client.heihei("haohaoguo"));
		
		
	}
}
?

?