天天看點

Maven項目中使用Axis1整合Spring配置

webservice有兩大類:RPC和Document。

共有五種風格:RPC/encoded 、RPC/literal、Document/literal、Document/literal wrapped、Document/literal non-wrapped

RPC/encoded :可讀性高。格式驗證困難,且性能受限于其格式的解析,不被ws-i接受。

RPC/literal :可讀性高。格式驗證困難,被ws-i接受。

Document/literal:格式驗證簡單,被ws-i有條件接受。無方法名,可讀性差。

Document/literal wrapped:格式驗證簡單,被ws-i接受。有方法名,可讀性非常差。不支援方法重載。

Document/literal non-wrapped:格式驗證簡單,被ws-i接受。無方法名,可讀性非常差。支援方法重載。

其中Document格式被任務是趨勢,RPC是較為陳舊的方式,但為什麼要用RPC/encoded?

很簡單已經存在的項目正在使用,而改造費用太大。

是以要用十年前的axis1(2006年的1.4版,相比其他webservice實作,其性能是最差的) !

方法: 建立一個maven webapp項目,加入相關依賴。添加springmvc的servlet和axis的servlet。添加類和wsdd檔案。關鍵代碼如下。

除了spring自己的包,還需要依賴:

<dependency>
	  <groupId>org.apache.axis</groupId>
	  <artifactId>axis</artifactId>
	  <version>1.4</version>
	</dependency>
	<dependency>
	  <groupId>commons-discovery</groupId>
	  <artifactId>commons-discovery</artifactId>
	  <version>0.5</version>
	</dependency>
	<dependency>
	  <groupId>javax.persistence</groupId>
	  <artifactId>persistence-api</artifactId>
	  <version>1.0.2</version>
	</dependency>
	<dependency>
	  <groupId>javax.xml.rpc</groupId>
	  <artifactId>javax.xml.rpc-api</artifactId>
	  <version>1.1.1</version>
	</dependency>
	<dependency>
	  <groupId>wsdl4j</groupId>
	  <artifactId>wsdl4j</artifactId>
	  <version>1.6.3</version>
	</dependency>      

web.xml同級增加檔案:server-config.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
       xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
       <handler name="URLMapper"
              type="java:org.apache.axis.handlers.http.URLMapper" />   
       <!-- 系統服務 -->
       <service name="AdminService" provider="java:MSG">
              <parameter name="allowedMethods" value="AdminService" />
              <parameter name="enableRemoteAdmin" value="false" />
              <parameter name="className" value="org.apache.axis.utils.Admin" />
              <namespace>http://xml.apache.org/axis/wsdd/</namespace>
       </service>
       <service name="Version" provider="java:RPC">
              <parameter name="allowedMethods" value="getVersion" />
              <parameter name="className" value="org.apache.axis.Version" />
       </service>     
       <!-- 自定義服務 -->
       <service name="myWebService" provider="java:RPC">
              <parameter name="className"
                     value="test.HelloWorldWebService" />
              <parameter name="allowedMethods" value="*" />
       </service>
       <transport name="http">
              <requestFlow>
                     <handler type="URLMapper" />
              </requestFlow>
       </transport>
</deployment>      

web.xml需要增加:

<!-- Spring MVC配置 -->
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 可以自定義servlet.xml配置檔案的位置和名稱,預設為WEB-INF目錄下,名稱為[<servlet-name>]-servlet.xml,如spring-servlet.xml
			<init-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>/WEB-INF/spring-servlet.xml</param-value>  預設
			</init-param>
			-->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
<!--axis 需要引入的 Servlet -->
	<servlet>
		<servlet-name>axis</servlet-name>
		<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>axis</servlet-name>
		<url-pattern>/services/*</url-pattern><!--axis 的 Web Service 的 Web 釋出路徑 -->
	</servlet-mapping>      

webservice類:

package test;


public class HelloWorldWebService implements HelloWorldRemote {
	//private HelloWorldRemote helloWorld;

	protected void onInit() throws Exception {
	}

	public String getMessage(String name) {
		return "xxxx";
		// 在 Spring 容器中擷取 Bean 的執行個體
		//helloWorld = (HelloWorldRemote) SpringUtil.getBean("myHelloWorldBean");
		// 執行 Bean 中的相同的方法
		//String msg=helloWorld.getMessage(name);
		//return msg;
	}
}

package test;
//Spring 工程中要使用的接口檔案
public interface HelloWorldRemote
{
       public String getMessage(String name);
}
           

部署到tomcat,浏覽器通路:http://localhost:8080/front/services/myWebService?wsdl

front是指項目名,可由mavn的pom或者eclipse的項目屬性指定。

一切正常的話(這種機率非常小,很可能在啟動時就出現了一些錯誤。缺少類什麼了。)可以看到如下代碼:

<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://192.168.1.6:8080/front/services/myWebService" xmlns:intf="http://192.168.1.6:8080/front/services/myWebService" 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://192.168.1.6:8080/front/services/myWebService">
<!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
<wsdl:message name="getMessageRequest">
<wsdl:part name="name" type="soapenc:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="getMessageResponse">
<wsdl:part name="getMessageReturn" type="soapenc:string"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorldWebService">
<wsdl:operation name="getMessage" parameterOrder="name">
<wsdl:input message="impl:getMessageRequest" name="getMessageRequest"></wsdl:input>
<wsdl:output message="impl:getMessageResponse" name="getMessageResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="myWebServiceSoapBinding" type="impl:HelloWorldWebService">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getMessage">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getMessageRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://test" use="encoded"/>
</wsdl:input>
<wsdl:output name="getMessageResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://192.168.1.6:8080/front/services/myWebService" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldWebServiceService">
<wsdl:port binding="impl:myWebServiceSoapBinding" name="myWebService">
<wsdlsoap:address location="http://192.168.1.6:8080/front/services/myWebService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>      

用戶端:

package test.axis14;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

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

public class AxisClient {

	public static void main(String[] args) {
		//String endPoint = "http://127.0.0.1:8081/axis/services/SCService?wsdl";
		String endPoint = "http://127.0.0.1:8080/front/services/myWebService?wsdl";
		 String operation = "getMessage";//
		 Service service = new Service();
		 String result = "";
		 try {
		 Call call = (Call) service.createCall();
		  call.setTargetEndpointAddress(new java.net.URL(endPoint));
		  call.setOperationName(operation);
		 //ִ
		  result = (String) call.invoke(new Object[] { " 1" });
		  System.out.println(result);
		  } catch (ServiceException e) {
		  e.printStackTrace();
		  } catch (MalformedURLException e) {
		   e.printStackTrace();
		} catch (RemoteException e) {
		   e.printStackTrace();
		 }

	}

}

           

 參考:http://blog.csdn.net/thinker28754/article/details/2318236

http://www.ibm.com/developerworks/library/ws-whichwsdl/index.html

http://axis.apache.org/axis/java/index.html