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

WebService - Axis2与Spring整合

发布时间:2020-12-16 21:42:06 所属栏目:安全 来源:网络整理
导读:上一篇Axis2使用services.xml进行开发server与client演示了axis2使用service.xml开发webservice服务端与客户端,并未与Spring进行整合。 本篇演示与spring整合下服务端的开发(客户端如何调用,参考上篇)。 参考官方文档:axis2与spring整合 【1】环境配置 服

上一篇Axis2使用services.xml进行开发server与client演示了axis2使用service.xml开发webservice服务端与客户端,并未与Spring进行整合。

本篇演示与spring整合下服务端的开发(客户端如何调用,参考上篇)。

参考官方文档:axis2与spring整合


【1】环境配置

服务端继续沿用上一个项目,不过要添加spring包/axis2与spring整合jar并修改配置。

① 添加jar;


② 添加applicationContext.xml并配置如下:

只配了自动扫描,说明类使用了注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


 <context:component-scan base-package="com.web"></context:component-scan>

</beans>

③ 为接口实现类添加@Service注解

这里写图片描述


④ 修改web.xml 如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

     <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>classpath:applicationContext.xml</param-value>  
     </context-param>  
     <listener>  
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
     </listener>  

    <servlet>
        <display-name>Apache-Axis Servlet</display-name>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>


   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

⑤ 修改services.xml

路径如下:

这里写图片描述


配置如下:

<service name="AxisSpringService">  
    <description>AxisSpringService</description>  

<!-- SpringBeanName作用类似于普通配置中的ServiceClass,都是用来创建服务类对象,只不过普通配置使用反射来创建 。 加入Spring之后,对象的创建交给了Spring的IOC容器,SpringBeanName指定要发布成WebService的Java类,SpringBeanName参数是JavaBean的名称。 SpringBeanName固定的不能改,因为springWebService是spring中注册的实现类得id。 如果不使用spring,可以使用ServiceClass属性,ServiceClass参数要指定要发布成WebService的Java类,并指定全类名的方式。 -->     
    <parameter name="SpringBeanName">  
        myServiceImpl
    </parameter>  

<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 --> 

    <parameter name="ServiceObjectSupplier">
    org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
    </parameter>  

<!-- 在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。 例如,getAge方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类, 而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。 -->   
   <operation name="sayHello">  
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />  
    </operation>  
</service>

【2】部署到Tomcat,运行

获取的wsdl地址:

http://localhost:8080/Axis2/services/AxisSpringService?wsdl

即,schema+IP+port+contextPath+services+serviceName+?wsdl


targetNamespace如下:

http://impl.service.Axis2.web.com

如果要为option设置action,则示例如下:

Options options = new Options();  
// 指定调用WebService的URL 
EndpointReference targetEPR = new EndpointReference(url);                          options.setAction("http://impl.service.Axis2.web.com/sayHello");

即,targetNamespace+method


浏览器出现如下图说明正常:

这里写图片描述


使用上一篇中的客户端进行测试(url变了)

客户端输出结果如下:

这里写图片描述


服务端输出结果如下:

这里写图片描述

(编辑:李大同)

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

    推荐文章
      热点阅读