webservice

来源:互联网 发布:短信轰炸机淘宝怎么找 编辑:程序博客网 时间:2024/05/22 07:46
 最近公司要搞webservic接口。自己以前没搞过,问朋友弄了个xfire的例子看了看,以为可以了,结果公司要用axis2做。。。没办法。只能自己百度,谷歌了。。

N久之后,找到了这个http://ws.apache.org/axis2/1_4_1/spring.html

官方网站上介绍两种方式整合,一种是我们所熟悉的applicationContext.xml放在和web.xml同一级的目录下,第二种是放在aar里面。

网上例子倒是一大把,蛋疼的是大多都是第二种,而且没涉及到数据库访问,也没提到spring中怎么配置

哎,官网上全是E问。幸好关于配置的还是看的点懂。我这里是选用第一种方式,而且我也只记录第一种,第二种涉及到spring注入我就不知道怎么搞了,

下面开始。

1、首先,需要axis2.war,根据它的目录结构我们修改自己的web应用。只需拷贝axis2/WEB-INF/目录下的所有文件到自己的web项目下就可以了,记得要覆盖所有(axis2-web这个文件夹是包含axis2整个的管理界面,可有可无,不过要用的话,记得它是放在和WEB-INF的同级目录下),其实我们就是有点像在整个的axis2工程上做二次开发。

WebRoot结构,如下图所示:

src结构,如图:

因为是小例子。所以就没那么规范了,webservice里放的是dao和entity以及.hbm.xml配置


2、我们像做所有其它的Web项目一样,用到spring的时候拷贝spring.jar到WEB-INF/lib目录下,然后在web.xml同级的目录下建立一个 applicationContext.xml,并且在applicationContext.xml引用相应的DTD或XSD(spring1.2引用的是DTD,spring2.0引用的是XSD),然后会在web.xml中加入这一段(当然,你可以选择用其它方式配置spring对web项目的支持)。

web.xml配置:

3、发布整个项目发部到tomcat上,测试起动服务有没有问题。起动没问题的话就访问下http://<host>:<port>/<project name>/services/Version?wsdl,只要成功就可以了,下面进入正题。

4、写一个接口。(spring面向接口编程)

[java] view plaincopy
  1. package com.zero.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.zero.webservice.Test;  
  6.   
  7. public interface ISpringwebservice {  
  8.     //查询所有   
  9.     public List<Test> getAll();  
  10. }  

5、实现这个接口。

[java] view plaincopy
  1. package com.zero.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.zero.service.ISpringwebservice;  
  6. import com.zero.webservice.Test;  
  7. import com.zero.webservice.TestDAO;  
  8.   
  9. public class SpringwebserviceImpl implements ISpringwebservice {  
  10.     // spring注入  
  11.     private TestDAO dao;  
  12.   
  13.     public void setDao(TestDAO dao) {  
  14.         this.dao = dao;  
  15.     }  
  16.   
  17.     // 查询所有  
  18.     public List<Test> getAll() {  
  19.         List<Test> list = dao.findAll();  
  20.         return list;  
  21.     }  
  22. }  

6、写一个Web Service。

[java] view plaincopy
  1. package com.zero.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.zero.service.ISpringwebservice;  
  6. import com.zero.webservice.Test;  
  7.   
  8. public class MyWebService {  
  9.     // spring注入  
  10.     private ISpringwebservice iservice;  
  11.     public void setIservice(ISpringwebservice iservice) {  
  12.         this.iservice = iservice;  
  13.     }  
  14.     // 查询所有  
  15.     public Test[] getAll() {  
  16.         Test[] arr = null;  
  17.         List<Test> list = iservice.getAll();  
  18.         if (!list.isEmpty()) {  
  19.             // 貌似webservice不能直接使用List传值,要么用数组,要么用对象封装List  
  20.             arr = new Test[list.size()];  
  21.             for (int i = 0; i < list.size(); i++) {  
  22.                 arr[i] = list.get(i);  
  23.             }  
  24.         }  
  25.         return arr;  
  26.     }  
  27. }  

src下applicationContext.xml配置:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.   
  7.   
  8.     <bean id="sessionFactory"  
  9.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  10.         <property name="configLocation"  
  11.             value="classpath:hibernate.cfg.xml">  
  12.         </property>  
  13.     </bean>  
  14.     <!-- basedao -->  
  15.     <bean id="TestDAO" class="com.zero.webservice.TestDAO">  
  16.         <property name="sessionFactory">  
  17.             <ref bean="sessionFactory" />  
  18.         </property>  
  19.     </bean>  
  20.     <!-- service -->  
  21.     <bean id="SpringWebService" class="com.zero.service.impl.SpringwebserviceImpl" >  
  22.         <property name="dao" ref="TestDAO"></property>  
  23.     </bean>     
  24.     </beans>  

WEB-INF下applicationContext.xml配置:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.     <import resource="classes/applicationContext.xml" /><!-- 导入src下的配置文件    相对路径 -->  
  7.     <!-- 这个一定要有 -->  
  8.     <bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"></bean>  
  9.       
  10.     <!-- MyWebservice -->  
  11.     <bean id="myservice" class="com.zero.service.impl.MyWebService" >  
  12.         <property name="iservice">  
  13.             <ref bean="SpringWebService" /><!-- src下applicationContext.xml中的service -->   
  14.         </property>  
  15.     </bean>  
  16. </beans>  
  17.       

7、在WEB-INF/services/下新建一个文件夹,这个名字可以起的随便,我们就起做teswst吧,然后在WEB-INF/services/testws/下建立META-INF这个目录,最后在WEB-INF/services/testws/META-INF/下建立services.xml,文件

结构如图:

services.xml内容:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <service name= "TestWebService" >   
  3.     <description>列表查询</description>   
  4.     <parameter name= "ServiceObjectSupplier" >   
  5.         org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>   
  6.     <parameter name= "SpringBeanName" >myservice</parameter> <!-- WEB-INF下applicationContext ben的id -->  
  7.     <messageReceivers>   
  8.         <messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-only"   
  9.              class = "org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"  />   
  10.         <messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-out"   
  11.              class = "org.apache.axis2.rpc.receivers.RPCMessageReceiver"  />   
  12.     </messageReceivers>   
  13. </service>   

 

OK.搞定,再发布启动服务,没问题,访问http://<host>:<port>/<project name>/services/TestWebService?wsdl看看



OK. 服务端搞定!

恩,因为axis2包比较多,所以源码就不上传了。  

最近闲来无事,又做了一遍,效果比第一次的要好,2个方法都没问题,现在打包放上去,包含服务端和客户端以及sql语句

axis2-1.5.4客户端

axis2-1.5.4服务端+sql

都放到百度云盘了

原创粉丝点击