微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService框架整理二 Axis1+Spring

初识Axis1就要把它集成到Spring框架上。一方面是当时的项目要求,在一方面更是我对于Spring情有独钟。  @H_404_6@Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。  @H_404_6@@H_404_6@相关链接:  @H_404_6@WebService框架整理(一) Axis1   @H_404_6@WebService框架整理(二) Axis1+Spring 我们将用到以下Jar:  @H_404_6@
引用

activation.jar 
axis.jar 
commons-discovery.jar 
commons-logging.jar 
jaxrpc.jar 
log4j-1.2.15.jar 
mail.jar 
wsdl4j.jar 
spring.jar 
@H_404_6@主要就是加入了spring.jar包!  @H_404_6@再看看web.xml,加入了Spring的相关内容。大家都熟悉Spring,我就不废话了!  @H_404_6@
Xml代码  

收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns="http://java.sun.com/xml/ns/javaee"  
  5.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  7.     id="WebApp_ID"  
  8.     version="2.5">  
  9.     display-name>spring-axis-1</context-param         param-name>log4jConfigLocationparam-value>classpath:log4j.xml>log4jRefreshInterval>60000>contextConfigLocation>/WEB-INF/applicationContext.xmlfilterfilter-name>UTF-8 Filterfilter-class>org.springframework.web.filter.CharacterEncodingFilterinit-param             >encoding>UTF-8>forceEncoding>truefilter-mappingurl-pattern>/*listenerlistener-class>org.springframework.web.util.Log4jConfigListener>org.springframework.web.context.ContextLoaderListenerservlet>Apache-Axis Servletservlet-name>axisservlet-class>org.apache.axis.transport.http.AxisServletload-on-startup>0servlet-mapping>/services/*web-app>  
@H_404_6@我们定义一个用于计算的CalcService接口及其实现CalcServiceImpl,如下: 
Java代码  

收藏代码

    /** 
  1.  * 简单计算 
  2.  *  
  3.  * @author 梁栋 
  4.  * @version 1.0 
  5.  * @since 1.0 
  6.  */  
  7. public interface CalcService {  
  8.   
  9.          * 求和 
  10.      *  
  11.      * @param a 
  12.      * @param b 
  13.      * @return 
  14.      */  
  15.     int add(int a, int b);  
  16. }  
@H_404_6@给出对应的实现内容: 
import org.zlex.axis.service.CalcService;  
  •  * 计算 
  • class CalcServiceImpl implements CalcService {  
  •     @Override  
  • int b) {  
  •         return a + b;  
  •     }  
  • 再简单不过的1+1问题!

    将其注入到Spring的容器中,applicationContext.xml如下所示: 
    beans  
  •     xmlns="http://www.springframework.org/schema/beans"  
  •     xmlns:context="http://www.springframework.org/schema/context"  
  •     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  •         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"bean  
  •         id="calcService"  
  •         class="org.zlex.axis.service.impl.CalcServiceImpl" />  
  • beans作为spring与axis1对接,需要做一个ServletEndpointSupport继承实现WebService,如下所示: 
    import javax.xml.rpc.ServiceException;  
  • import org.springframework.context.ApplicationContext;  
  • import org.springframework.remoting.jaxrpc.ServletEndpointSupport;  
  •  * WebService入口 
  • class WebService extends ServletEndpointSupport {  
  • private ApplicationContext applicationContext;  
  • private CalcService calcService;  
  • /* 
  •      * (non-Javadoc) 
  •      * @see org.springframework.remoting.jaxrpc.ServletEndpointSupport#onInit() 
  • protected void onInit() throws ServiceException {  
  •         // 初始化Spirng 配置  
  •         applicationContext = super.getApplicationContext();  
  •         calcService = (CalcService) applicationContext.getBean("calcService");  
  • public String add(return String.valueOf(calcService.add(a, b));  
  • 这里为了便于在eclipse演示,将返回值定为String类型!  @H_404_6@现在我们将该服务植入Axis中,修改server-config.wsdd文件,在原文件中加入如下内容: 
    <!-- 自定义服务 -->  
  • service  
  •     name="WebService"  
  •     provider="java:RPC"parameter  
  •         name="className"  
  •         value="org.zlex.axis.WebService" service修改后的server-config.wsdd文件如下所示: 
    deployment
      
  •     xmlns="http://xml.apache.org/axis/wsdd/"  
  •     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"globalConfiguration             name="adminPassword"  
  •             value="admin"              name="sendxsitypes"  
  •             value="true"              name="sendMultiRefs"  
  •             name="sendXMLDeclaration"  
  •             name="axis.sendMinimizedElements"  
  • requestFlowhandler  
  •                 type="java:org.apache.axis.handlers.JWSHandler"                                      name="scope"  
  •                     value="session" handler                     value="request"                      name="extension"  
  •                     value=".jwr"          name="Authenticate"  
  •         type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"          name="LocalResponder"  
  •         type="java:org.apache.axis.transport.local.LocalResponder"          name="URLMapper"  
  •         type="java:org.apache.axis.handlers.http.URLMapper"          name="AdminService"  
  •         provider="java:MSG"             name="allowedMethods"  
  •             value="AdminService"              name="enableRemoteAdmin"  
  •             value="false"              name="className"  
  •             value="org.apache.axis.utils.Admin" namespace>http://xml.apache.org/axis/wsdd/         name="Version"  
  •         provider="java:RPC"             value="getVersion"              value="org.apache.axis.Version" transport  
  •         name="http"                 type="URLMapper"                  type="java:org.apache.axis.handlers.http.HTTPAuthHandler" transport         name="local"responseFlow                 type="LocalResponder"               name="WebService"  
  •             value="org.zlex.axis.WebService" deployment我们随机抽取2个数进行求和运算,并验证WebService和本地计算结果是否一致,测试用例WebServiceTest如下: 
     * WebService测试
     
  • class WebServiceTest {  
  • private String nameSpaceUri = "http://localhost:8080/axis/services/WebService";  
  • private String wsdlUrl = nameSpaceUri + "?wsdl";  
  • private Service service;  
  • private Call call;  
  • @Before  
  • final void init() throws Exception {  
  • // 创建调用对象  
  •         service = new Service();  
  •         call = (Call) service.createCall();  
  • // 调用 远程方法  
  •         call.setoperationName(new QName(nameSpaceUri, "add"));  
  • // 设置URL  
  •         call.setTargetEndpointAddress(new URL(wsdlUrl));  
  • @Test  
  • void testAdd()            
  • // 设置参数  
  •         Random rnd = new Random();  
  • int a = rnd.nextInt(100);  
  • int b = rnd.nextInt(// 执行远程调用,同时获得返回值  
  •         String r = (String) call.invoke(new Object[] { a, b });  
  •         assertEquals(String.valueOf(a + b), r);  
  •         System.out.println("a(" + a + ") + b(" + b + ") = " + r);  
  • 我们验证一下结果!  @H_404_6@

     顺利通过!  @H_404_6@我们在通过Eclipse验证一下这个服务!  @H_404_6@

    @H_404_6@Eclipse中输入参数验证WebService,如果要看到返回值就需要把返回值定为String类型。如果用int类型,我们只能通过测试用例检测这个结果!

    完整项目实例见附件!

  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐