`
bnmnba
  • 浏览: 288900 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics