在项目中使用AXIS2发布WebService

Axis2+tomcat7.0 实现webService 服务端发布与客户端的调用。  
Aixs2开发webService的方法有很多,在此只介绍一种比较简单的实现方法。
第一步:首先要下载开发所需要的jar   
下载:
下载完后将axis2.war放至tomcat安装目录下的webapps文件夹下,然后启动tomcat后,在webapps目录下会生成axis2文件夹。  访问http://localhost:8080/axis2/能看到以下页面表示axis2运行成功。   
第二步  MyEclipse下新建Web Project,工程名:elecProject。新建包cn.itcast.elec.service,在cn.itcast.elec.service下新建类WebSystemDDLServiceImpl
代码如下:  
package cn.itcast.elec.service.impl;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.elec.dao.IElecSystemDDLDao;
import cn.itcast.elec.domain.ElecSystemDDL;
public  class WebSystemDDLServiceImpl {
    public String findSystemByKeyword(String keyword) {
      ApplicationContext ac =  new ClassPathXmlApplicationContext("beans.xml");
      IElecSystemDDLDao elecSystemDDLDao = (IElecSystemDDLDao) ac.getBean(IElecSystemDDLDao. SERVICE_NAME);
      
      //组织查询条件
      String condition = "";
      List<Object> paramsList =  new ArrayList<Object>();
       if(StringUtils. isNotBlank( keyword)){
         condition += " and o.keyword = ?";
         paramsList.add(keyword);
      }
      Object [] params = paramsList.toArray();
      //排序语句
      Map<String, String> orderby =  new LinkedHashMap<String, String>();
      orderby.put("o.ddlCode","asc");//按照数据项的编号升序排列
      //数据字典进行查询的时候,使用二级缓存增强检索的效率
      List<ElecSystemDDL> list = elecSystemDDLDao.findColectionByConditionNoPageWithCache(condition, params, orderby);
//    List<ElecSystemDDL> list = elecSystemDDLDao.findColectionByConditionNoPage(condition, params, orderby);
      StringBuffer webObject  =  new StringBuffer("");//axis2支持String类型和XML的类型
       if(list!= null && list.size()>0){
          for( int i=0;i<list.size();i++){
            webObject.append(list.get(i).getDdlName()+",");//值之间用逗号分隔
         }
         webObject.deleteCharAt(webObject.length()-1);
      }
       return webObject.toString();
   }
}
WEB-INF目录下修改web.xml文件,内容如下:  
<?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">     
<!--Axis2 config start-->
<servlet>  
<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>   
<!--Axis2  end-->   
</web-app>   
tomcat安装目录下的webapps/axis2/WEB-INF下的modulesserviceconf文件拷至itcastProject下的WEB-INF目录下。同时把lib下的如下jar包也拷到项目的lib包下。
为了与项目的其他包不发生冲突,需要的jar包有:
然后在WEB-INF/services下新建systemDDLService/META-INF路径,
META-INF下新建services.xml
内容如下:  
<?xml version="1.0" encoding="UTF-8"?>
<service name="systemDDLService">      
   <description>elecProject Service Example</description>        
   <parameter name="ServiceClass">cn.itcast.elec.service.impl.WebSystemDDLServiceImpl</parameter>  
   <operation name="findSystemByKeyword">           
      <messageReceiver  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />      
   </operation>
</service>
   
启动tomcat后访问:
http://127.0.0.1:8080/elecProject/services/systemDDLService?wsdl能看到服务信息了。  到此Axis2WebService服务已成功发布。
Axis2客户端调用:
下面看看利用axis2 客户端调用实例   
客户端程序需要的jar
新建一个客户端的程序中使用:
/***调用网络服务axis调用webservice**/
       this.webElecSystemDDLService();
/***********************************/
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
private  void webElecSystemDDLService() {
      String xmlStr = "性别";   
      String  url = "http://127.0.0.1:8080/itcast0408elec/services/systemDDLService";   
      String method="findSystemByKeyword";   
      String  webObject =  null;   
        try {         
             RPCServiceClient serviceClient =  new RPCServiceClient();   
             Options options = serviceClient.getOptions();     
             EndpointReference targetEPR =  new EndpointReference(url);   
             options.setTo(targetEPR);     
             /** 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,
                也就是<wsdl:definitions>元素的targetNamespace属性值,method指定方法名  
             */
             QName opAddEntry =  new  QName("http://impl.service.elec.itcast.cn",method);
              /** 参数,如果有多个,继续往后面增加即可,不用指定参数的名称*/   
              Object[] opAddEntryArgs =  new Object[] {xmlStr};      
             // 返回参数类型,这个和axis1有点区别      
             /**invokeBlocking方法有三个参数,
              *  其中第一个参数的类型是QName对象,表示要调用的方法名;      
                 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];      
                第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。  
              */
             /**注意: 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}      
                      如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,     
                      该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
              */     
             Class[] classes =  new Class[] { String. class };      
             webObject = (String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];      
             System. out.println(webObject);      
          }
        catch (Exception e) {   
          e.printStackTrace();     
           long end = System. currentTimeMillis();   
       }
      
   }
总结:以上就是Axis2 服务发布与调用的简单案例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值