Axis2与Spring集成发布

2024-06-03 14:32
文章标签 java spring 发布 集成 axis2

本文主要是介绍Axis2与Spring集成发布,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在现今的Web应用中经常使用spring框架来装载JavaBean。如果要想将某些在spring中装配的JavaBean发布成WebService,使用Axis2的Spring感知功能是非常容易做到的。

1、首先建立一个web工程,名字叫WebService,
2、把相应的axis2的jar文件考到WEB-INF的lib下 
3、 在项目的WebRoot下的目录结构要和以前用war包是的目录结构一样(否则可能就要报错了) 
目录结构如图所示: 

 

4、在src下建立package sample.service
5、建立提供服务的接口

[java] view plain copy
print ?
  1. package sample.service;    
  2.     
  3. /**  
  4.  * 定义服务接口  
  5.  * @author 11111  
  6.  *  
  7.  */    
  8. public interface ServiceServer {    
  9. //定义服务方法    
  10.     public String sayHello(String name);    
  11.         
  12. }    
package sample.service;  /** * 定义服务接口 * @author 11111 * */  
public interface ServiceServer {  
//定义服务方法  public String sayHello(String name);  }  

实现类:

[java] view plain copy
print ?
  1. package sample.service;    
  2.     
  3. public class ServiceServerImpl implements ServiceServer {    
  4.     
  5.     public String sayHello(String name) {    
  6.             
  7.         return "hello"+name;    
  8.     }    
  9.     
  10. }    
package sample.service;  public class ServiceServerImpl implements ServiceServer {  public String sayHello(String name) {  return "hello"+name;  }  }  


6、在src下建立applicationContext.xml文件
配置如下

[html] view plain copy
print ?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xmlns:aop="http://www.springframework.org/schema/aop"    
  5.     xmlns:tx="http://www.springframework.org/schema/tx"    
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd    
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd    
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">    
  9.     
  10.     
  11.     
  12. <bean id="SayHelloService" class="sample.service.ServiceServerImpl">    
  13. </bean>    
  14.     
  15.     
  16. </beans>    
<?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:aop="http://www.springframework.org/schema/aop"  xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  <bean id="SayHelloService" class="sample.service.ServiceServerImpl">  
</bean>  </beans>  

 

7、在WebRoor/WEB-INF/services/目录下建立目录sampleService(这个名字可以随便取)
然后建立在其下META-INF目录,然后再在其目录下建立services.xml 
目录结构如下

 

services.xml的内容如下:

 

[html] view plain copy
print ?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2.     
  3.     
  4. <service name="HelloWorld">    
  5.         <description>web service</description>    
  6.         <parameter name="ServiceObjectSupplier">    
  7.             org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier    
  8.         </parameter>    
  9.         <parameter name="SpringBeanName">SayHelloService</parameter>    
  10. //SpringBeanName名字是固定的不能改    
  11. //SayHelloService是spring中注册的实现类的id(这个大家肯定很清楚了)    
  12.         
  13.  <operation name="sayHello">    
  14.             <messageReceiver    
  15.                 class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />    
  16.         </operation>    
  17.     
  18. </service>    
<?xml version="1.0" encoding="UTF-8"?>  <service name="HelloWorld">  <description>web service</description>  <parameter name="ServiceObjectSupplier">  org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier  </parameter>  <parameter name="SpringBeanName">SayHelloService</parameter>  
//SpringBeanName名字是固定的不能改  
//SayHelloService是spring中注册的实现类的id(这个大家肯定很清楚了)  <operation name="sayHello">  <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  </operation>  </service>  

8、现在要配置一下web.xml了 
内容如下:

[html] view plain copy
print ?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.5"     
  3.     xmlns="http://java.sun.com/xml/ns/javaee"     
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    
  7.      
  8.     <servlet>    
  9.         <servlet-name>AxisServlet</servlet-name>    
  10. //注册axis2的servlet    
  11.         <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>    
  12.         <load-on-startup>1</load-on-startup>    
  13.     </servlet>    
  14.             
  15.     <servlet-mapping>    
  16.         <servlet-name>AxisServlet</servlet-name>    
  17.         <url-pattern>/services/*</url-pattern>    
  18.     </servlet-mapping>    
  19. //加载spring的配置文件    
  20.     <context-param>    
  21.       <param-name>contextConfigLocation</param-name>    
  22.     
  23.       <param-value>classpath*:applicationContext.xml</param-value>    
  24.     </context-param>    
  25. //增加spring监听器    
  26.     <listener>    
  27.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  28.     </listener>    
  29. </web-app>    
<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5"   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">  <servlet>  <servlet-name>AxisServlet</servlet-name>  
//注册axis2的servlet  <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>  
//加载spring的配置文件  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath*:applicationContext.xml</param-value>  </context-param>  
//增加spring监听器  <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  
</web-app>  


9、启动tomcat 在浏览器中输入http://localhost:8080/WebService/services/listServices
可以看到一下内容说明我们的服务已经发布成功了 

访问 
http://localhost:8080/WebService/services/HelloWorld?wsdl
可以查看wsdl 

 

现在我们开始访问我们的服务,我们采用用axis2 的eclipse 插件自动生成客户端
一、根据wsdl自动生成客户端的方式 

1、新建一个Java project ,取名为ServiceClient

2、建立一个User Library 取名AXIS2 将axis2 所需要的jar文件加到AXIS2中,然后在
ServiceClient 中引入这个library 

3、在eclipse中安装axis2插件
Service Archive Wizard - Eclipse Plug-in 和 Code Generator Wizard - Eclipse Plug-in
注:安装方式可见我的博客中的axis webservice 笔记 安装eclipse axis2 插件 (links 方式)文章

4、(1)选择new->other->Axis2 Wizards-> Axis2 code generator


 

(2)下一步


 

(3)下一步 
把浏览其中的wsdl地址考到里面 

 

(4) 下一步 直接点next 

(5)选择生成到我们刚建好的ServiceClient的src 目录中

然后点击finish,这样就可一生成客户端了,刷新项目就可以看到了

 

5、生成的目录结构如图所示:


 

二、新建ServiceClient.Java 内容如下

[java] view plain copy
print ?
  1.    package sample.service;    
  2.     
  3. public class ServiceClient {    
  4.     
  5.     /**  
  6.      * @param args  
  7.      */    
  8.     public static void main(String[] args) throws java.lang.Exception{    
  9. //创建存根类    
  10.         HelloWorldStub stub = new HelloWorldStub();    
  11. //设置相应的方法的值     
  12.             HelloWorldStub.SayHello sayHello = new HelloWorldStub.SayHello();    
  13.     
  14.         sayHello.setName("张三");    
  15. //调用服务的相应方法并获得返回值    
  16.         HelloWorldStub.SayHelloResponse response = stub.sayHello(sayHello);    
  17.             
  18.         System.out.println(response.get_return());    
  19.         
  20.     }    
  21.     
  22. }   
   package sample.service;  public class ServiceClient {  /** * @param args */  public static void main(String[] args) throws java.lang.Exception{  
//创建存根类  HelloWorldStub stub = new HelloWorldStub();  
//设置相应的方法的值   HelloWorldStub.SayHello sayHello = new HelloWorldStub.SayHello();  sayHello.setName("张三");  
//调用服务的相应方法并获得返回值  HelloWorldStub.SayHelloResponse response = stub.sayHello(sayHello);  System.out.println(response.get_return());  }  } 

运行这个java类,可以看到console中 
打印出:hello张三 ,说明我们已经调用成功了。

这篇关于Axis2与Spring集成发布的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1027257

相关文章

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll