加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService框架整理(二) Axis1+Spring

发布时间:2020-12-16 23:01:56 所属栏目:安全 来源:网络整理
导读:初识Axis1就要把它集成到Spring框架上。一方面是当时的项目要求,在一方面更是我对于Spring情有独钟。? Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。? 相关链接:? WebService框架整理(一) Axis1 ? WebService框架整理(
初识Axis1就要把它集成到Spring框架上。一方面是当时的项目要求,在一方面更是我对于Spring情有独钟。?
Axis1+Spring比较简单,这种便利得益于Spring的ServletEndpointSupport类支持。?

相关链接:?
WebService框架整理(一) Axis1 ?
WebService框架整理(二) Axis1+Spring 我们将用到以下Jar:?
引用

activation.jar?
axis.jar?
commons-discovery.jar?
commons-logging.jar?
jaxrpc.jar?
log4j-1.2.15.jar?
mail.jar?
wsdl4j.jar?
spring.jar?

主要就是加入了spring.jar包!?
再看看web.xml,加入了Spring的相关内容。大家都熟悉Spring,我就不废话了!?
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>??

我们定义一个用于计算的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. }??

给出对应的实现内容:?
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类型!?
    现在我们将该服务植入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);??
  • 我们验证一下结果!?

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


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

    完整项目实例见附件!

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    • 推荐文章
        热点阅读