分享

webservice 笔记(小结) axis2 集成spring <一>发布服务 - Th...

 昵称465563 2010-09-19
学习了一段时间的webservice,今天有空,稍微总结一下。
首先我们选的是axis2-1.4 +myeclipse6.5+tomcat 6.0,大家肯定不陌生了。我们以前都是把从官网上下载的war包考到tomcat的webapps下,但是现在我们自己建一个web项目

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

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

 
Java代码 复制代码
  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. }  


  实现类:
Java代码 复制代码
  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. }  


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

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www./schema/beans"  
  3.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www./schema/aop"  
  5.     xmlns:tx="http://www./schema/tx"  
  6.     xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-2.0.xsd   
  7.            http://www./schema/aop http://www./schema/aop/spring-aop-2.0.xsd   
  8.            http://www./schema/tx http://www./schema/tx/spring-tx-2.0.xsd">   
  9.   
  10.   
  11.   
  12. <bean id="SayHelloService" class="sample.service.ServiceServerImpl">   
  13. </bean>   
  14.   
  15.   
  16. </beans>  

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

services.xml的内容如下:

Java代码 复制代码
  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>  

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

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app version="2.5"    
  3.     xmlns="http://java./xml/ns/javaee"    
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java./xml/ns/javaee    
  6.     http://java./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>  

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

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

待会就是访问我们的服务了(用axis2 的eclipse 插件自动生成客户端),

如果有问题可以加群 : 84242283(webservice交流学习)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约