论坛首页 Java企业应用论坛

axis1.4集成和应用示例

浏览 3189 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-10-20   最后修改:2010-10-20
SOA

axis1.4集成和应用示例

 

前言:网上很多有关axis1.4的应用示例,大部分是官方doc的翻译和example的翻译,helloword等等。

          最近有个需求是提供带参数的方法接口,返回自定义对象,刚好熟悉下axis1.4用法。

 

 

步骤:

 

1:下载axis1.4的包解压到本地,至于axis的环境变量看情况,想配就配,不想配也无所谓,就是使用axis的命令时候会麻烦点。

 

2:查看axis-1_4\docs\install.html下的 《

Advanced Installation: adding Axis to your own Webapp

》一项内容,这个是将axis1.4集成到自己的web应用里的方法,网上大部分教你的是直接将axis1.4包里的web应用放入tomcat里,然后在此基础上开发自己的webservice,其实是一样的道理,将该web里的web.xml里的内容copy到自己原有web应用的web.xml里,然后将axis1.4的lib里的相关依赖包加到自己的web里就可以在自己的应用里开发webservice了。

 

3:相关代码

    3.1:pojo类

 

package com.asiainfo.boss.channel.channelservices;

import java.io.Serializable;

public class MenuNodeInterface implements Serializable{
	/**
	 * serId
	 */
	private static final long serialVersionUID = 4859157035701539929L;
	/**
	 * 菜单名称
	 */
	private String S_Caption;

	public MenuNodeInterface(){
		this.S_Caption="陈超";
	}

	public String getS_Caption() {
		return S_Caption;
	}

	public void setS_Caption(String caption) {
		S_Caption = caption;
	}
	
	
}

 

   3.2:封装的结果类

package com.asiainfo.boss.channel.channelservices;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class EsopResult1 implements Serializable{

	/**
	 * seriaId
	 */
	private static final long serialVersionUID = -1922115715606536898L;
	
	/**
	 * 0:成功
	   1:失败
	 */
	private Short returnCode=1;
	
	private List<MenuNodeInterface> sysMenuNodeList;
	
	private String errorMsg;
	
	public EsopResult1(){
		sysMenuNodeList=new ArrayList<MenuNodeInterface>();
		sysMenuNodeList.add(new MenuNodeInterface());
		
	}

	public Short getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(Short returnCode) {
		this.returnCode = returnCode;
	}

	public List<MenuNodeInterface> getSysMenuNodeList() {
		return sysMenuNodeList;
	}

	public void setSysMenuNodeList(List<MenuNodeInterface> sysMenuNodeList) {
		this.sysMenuNodeList = sysMenuNodeList;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
	
	
	
	
}

  

   3.3:服务端类

package com.asiainfo.boss.channel.channelservices;

import com.asiainfo.boss.channel.channelservices.EsopResult1;

public class EsopWBService1 {
	
	public EsopResult1 qryMenuNodeInfoList(long opId,long orgId){
		EsopResult1 er1=new EsopResult1();
		er1.setReturnCode((short)0);
		er1.setErrorMsg("成功");
		return er1;
	}
}

  

   3.4:客户端类

package com.asiainfo.boss.channel.channelservices;

import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;

public class EsopWBClient1 {
	
	public static void main(String...args){
		getResult();
	}
	public static void getResult(){
		try {   
            String endpoint = "http://localhost:8091/bjchannel/services/EsopWBService1";   
            Long opId = new Long(1009);   
            Long orgId = new Long(1000);   
               
            Service service = new Service();   
            Call call = (Call) service.createCall();   
            QName qn = new QName("urn:EsopWBService1","EsopResult1"); 
            QName qn1 = new QName("urn:EsopWBService1","MenuNodeInterface"); 
            //注册 bean   
            call.registerTypeMapping(EsopResult1.class, qn,    
                    new BeanSerializerFactory(EsopResult1.class, qn),    
                    new BeanDeserializerFactory(EsopResult1.class, qn));  
            call.registerTypeMapping(MenuNodeInterface.class, qn1,    
                    new BeanSerializerFactory(MenuNodeInterface.class, qn1),    
                    new BeanDeserializerFactory(MenuNodeInterface.class, qn1));  
  
            call.setTargetEndpointAddress(new java.net.URL(endpoint));   
            call.setOperationName(new QName("EsopWBService1","qryMenuNodeInfoList"));
//            Object[] sss=new Object[2];
//            sss[0]=i1;
//            sss[1]=i2;
            //EsopResult1 ret = (EsopResult1) call.invoke("qryMenuNodeInfoList",new Object[]{i1,i2});
            //以下两个参数必须加,不然后报数组越界异常,因为源码里的operation为的params需要手动设置
            //invoke只负责往里传递数,不管原来方法参数的初始化,以下两句表明调用的方法需要初始化两个参数
            call.addParameter("opId", XMLType.XSD_LONG, ParameterMode.IN);
            call.addParameter("orgId", XMLType.XSD_LONG, ParameterMode.IN);
            EsopResult1 ret = (EsopResult1) call.invoke(new Object[]{opId,orgId});
            
  
            System.out.println("the operation result following:");   
            System.out.println("ErrorMsg = " + ret.getErrorMsg());   
            System.out.println("ReturnCode = " + ret.getReturnCode());   
            System.out.println("MenuNodeList().size() = " + ret.getSysMenuNodeList().size());
            
            List<MenuNodeInterface> reList=ret.getSysMenuNodeList();
            
            for(MenuNodeInterface mni : reList){
            	System.out.println("菜单名称:"+mni.getS_Caption());
            }
             
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
	
}

 

注:以上类涉及到的包名和应用路径名称是我现在所在公司的应用名,大家自行修改,比如bjchannel等等,根据自己情况修改。还要注意客户端类里的参数add一定要加,我开始没加就一直报数组越界异常,不知道是axis1.4相关包的问题,还是什么,我看网上和官方的ORDER示例都没有加也没事,不过加了反正没坏处。

 

4:我直接使用定制方式发布了,即时发布的方式大家参看附件文档

 

    4.1:新建server-config.wsdd文件,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
    xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler" xmlns="http://xml.apache.org/axis/wsdd/">
    <globalConfiguration name="GlobalConfiguration1" type="" regenerateElement="false">
        <requestFlow name="RequestFlow1" type="" regenerateElement="false">
            <handler name="Handler1"
                type="java:org.apache.axis.handlers.JWSHandler" regenerateElement="false">
                <parameter name="scope" value="session" regenerateElement="false"/>
            </handler>
            <handler name="Handler2"
                type="java:org.apache.axis.handlers.JWSHandler" regenerateElement="false">
                <parameter name="scope" value="request" regenerateElement="false"/>
                <parameter name="extension" value=".jwr" regenerateElement="false"/>
            </handler>
        </requestFlow>
    </globalConfiguration>
    <handler name="URLMapper"
        type="java:org.apache.axis.handlers.http.URLMapper" regenerateElement="false"/>
    <handler name="LocalResponder"
        type="java:org.apache.axis.transport.local.LocalResponder" regenerateElement="false"/>
    <handler name="Authenticate"
        type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" regenerateElement="false"/>
    <transport name="http" type="" regenerateElement="false">
        <requestFlow name="RequestFlow1" type="" regenerateElement="false">
            <handler name="Handler1" type="URLMapper" regenerateElement="false"/>
            <handler name="Handler2"
                type="java:org.apache.axis.handlers.http.HTTPAuthHandler" regenerateElement="false"/>
        </requestFlow>
    </transport>
    <transport name="local" type="" regenerateElement="false">
        <responseFlow name="ResponseFlow1" type="" regenerateElement="false">
            <handler name="Handler1" type="LocalResponder" regenerateElement="false"/>
        </responseFlow>
    </transport>
    <service name="AdminService" type="" regenerateElement="false" provider="java:MSG">
        <parameter name="allowedMethods" value="AdminService" regenerateElement="false"/>
        <parameter name="enableRemoteAdmin" value="false" regenerateElement="false"/>
        <parameter name="className" value="org.apache.axis.utils.Admin" regenerateElement="false"/>
        <namespace>http://xml.apache.org/axis/wsdd/</namespace>
    </service>
    <service name="Version" type="" regenerateElement="false" provider="java:RPC">
        <parameter name="allowedMethods" value="getVersion" regenerateElement="false"/>
        <parameter name="className" value="org.apache.axis.Version" regenerateElement="false"/>
    </service>

          
		<service name="com.asiainfo.boss.channel.channelbj.mobileplat.MobilePlatform" type="" regenerateElement="false" provider="java:AIRPC">
		  <parameter name="scope" value="session" regenerateElement="false"></parameter>
		  <parameter name="className" value="com.asiainfo.boss.channel.channelbj.mobileplat.IMobilePlatform" regenerateElement="false"></parameter>
		  <parameter name="allowedMethods" value="*" regenerateElement="false"></parameter>
		</service>
		
	<!-- 商机处理 提供给crm接口 -->
	<service name="com.asiainfo.boss.channel.channelbj.channelcomp2.webservice.busiinfo.BusiInfoDeal" provider="java:AIRPC">
	   <parameter name="scope" value="session" regenerateElement="false"></parameter>
	   <parameter name="className" value="com.asiainfo.boss.channel.channelbj.channelcomp2.webservice.busiinfo.IBusiInfoDeal" regenerateElement="false"></parameter>
	   <parameter name="allowedMethods" value="*" regenerateElement="false"></parameter>
	</service>
	
<!-- 本文示例接口 -->
	<service name="EsopWBService1" provider="java:RPC">
	   <parameter name="scope" value="session" ></parameter>
	   <parameter name="className" value="com.asiainfo.boss.channel.channelservices.EsopWBService1" ></parameter>
	   <parameter name="allowedMethods" value="*" ></parameter>
	   
	   <beanMapping languageSpecificType="java:com.asiainfo.boss.channel.channelservices.EsopResult1"  
         qname="ccs:EsopResult1" xmlns:ccs="urn:EsopWBService1"  
         />
       <beanMapping languageSpecificType="java:com.asiainfo.boss.channel.channelservices.MenuNodeInterface"  
         qname="ccs:MenuNodeInterface" xmlns:ccs="urn:EsopWBService1"  
         />
	</service>
	
</deployment>
 

    注:大家可能看到该配置文件里的前两个服务采用的provider="java:AIRPC",而在axis-1_4\docs\apiDocs\index.html文档里的org.apache.axis.providers.java包下并没有个叫AIRPC的,这个不用管,因为这个是公司扩展的providers,主要是取service方便,按照公司自己的框架取service方式来取。大家看最后的示例接口,都采用provider="java:RPC"即可。自己跑示例将java:AIRPC的service都删除即可。

   另外该文件要放在你的web应用的WEB-INF目录下,例如:WEB-INF\server-config.wsdd

5:启动你的servlet容器,然后跑EsopWBClient1类看结果

OK,附件是相关代码,配置文件和文档,还有就是参看axis1.4的doc和apiDoc以及examples

另外以下是参考链接

http://log-cd.iteye.com/blog/469491

http://chenjumin.iteye.com/blog/240206

http://watching2008.iteye.com/blog/406102

http://topic.csdn.net/u/20080121/16/EE41AF56-CB1D-499E-8F4E-2BED110A74AA.html 我是看了这个才想到要在客户端代码里addParameter的

http://www.pusuo.net/2010-09-26/111294895.html

http://group.gimoo.net/review/116408

 

 

服务测试路径,我的是http://localhost:8091/bjchannel/services/EsopWBService1

wsdl是http://localhost:8091/bjchannel/services/EsopWBService1?wsdl

 

大家自己看情况修改吧,路径中的“services”是你的web.xml里axisServlet对应的urlMapping,你改成别的也行

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics