`
jyangzi5
  • 浏览: 207764 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Axis webservice实例

阅读更多

今天参考网上的资料实践了axis下的webservice实例,经过自己的整理和理解,在此与各位分享。

1。创建java工程: AxisWebservice

2。配置 web.xml 

  

   

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<servlet>
    <servlet-name>AxisServlet</servlet-name>
   
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>

 3.创建服务类 HelloWorld

 

package com.chnic.webservice;

public   class  HelloWorld {    
    
    public  HelloWorld(){            
   }        
    public  String hello(String str){    
        return   "Hello "  + str;    
   }       
    public   int  add( int  a,  int  b){    
        return  a + b;    
   }          
}  

 

4.编写部署文件 deploy.wsdd

 

 

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java = "http://xml.apache.org/axis/wsdd/providers/java" >    
     <!--HelloWorld为webservice的名字,java:RPC为服务类型,  -->   
     <service   name = "HelloWorld"   provider = "java:RPC" >  
     
        <!--参数className指定具体的类  -->  
         <parameter   name = "className"   value = "com.chnic.webservice.HelloWorld" />
         
         <!-- 此参数定义允许调用的方法,*号表示所有public方法 -->    
         <parameter   name = "allowedMethods"   value = "*" />         
    </service >    
</deployment >  

 5.webservice发布

     将项目部署好之后启动tomcat,在地址栏输入http://127.0.0.1:9091/Axis/servlet/AxisServlet,如果能看到两个webservice服务,Axis自带的两个默认的服务,说明部署成功,此时还是第一步,下面开始发布自己的服务,运行cmd,切换目录至WEB-INF下,运行命令(这里以我的目录为例 ),这期间tomcat必须处于启动状态。

D:\workspace\AxisWebservice\WebContent\WEB-INF>java -Djava.ext.dirs=lib org.apac
he.axis.client.AdminClient -lhttp://127.0.0.1:9091/Axis/servlet/AxisServlet depl
oy.wsdd

 如若看到  

Processing file deploy.wsdd
<Admin>Done processing</Admin>

两行文字,则说明自定义服务发布成功,在地址栏输入http://127.0.0.1:9091/Axis/servlet/AxisServlet,会看到多了一个自己定义的服务。

 

6.客户端调用类 AxisClient

 

package com.chnic.webservice.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

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

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class AxisClient {
	
	
public static void main(String []arg)
{

	String targetEndPoint="http://127.0.0.1:9091/Axis/services/HelloWorld";
	Service ser=new Service();
	try {
		Call call=(Call)ser.createCall();
		call.setTargetEndpointAddress(targetEndPoint);
		// call.setOperationName(new  QName(targetEndPoint,  "hello" )); 
		call.setOperation("hello" ); 
		String result=(String)call.invoke(new Object[]{new String("xiaowu")});
		System.out.println("result="+result);	
		call.setOperation("add");
		int addResult=(Integer)call.invoke(new Object[]{new Integer(1),new Integer(2)});
		System.out.println("addResult="+addResult);
	} catch (ServiceException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	catch (RemoteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

}

 

7.执行main方法,当返回

 result=Hello xiaowu
addResult=3

则说明调用成功,一个简单的webservice例子至此完毕,有不足的地方敬请谅解,多多留言指出。

详细的项目内容我放在了附件里,方便大家下载。

分享到:
评论
2 楼 ziliang871118 2010-10-23  
刚才那个问题 我调整了环境变量,就可以编译成功了
看到 

Processing file deploy.wsdd
<Admin>Done processing</Admin>


但最后运行客户端又报如下错误:

看到 

AxisFault
faultCode: {http://xml.apache.org/axis/}Server.NoService
faultSubcode:
faultString: The AXIS engine could not find a target service to invoke!  targetService is HelloWorld


能帮下忙啊嘛?
1 楼 ziliang871118 2010-10-23  
你好

我在这一步

1.D:\workspace\AxisWebservice\WebContent\WEB-INF>java -Djava.ext.dirs=lib org.apac  
2.he.axis.client.AdminClient -lhttp://127.0.0.1:9091/Axis/servlet/AxisServlet depl  
3.oy.wsdd 


报错 java.lang.NoClassDefFoundError org.apache.axis.client.AdminClient

请问可能是什么问题呢?

相关推荐

Global site tag (gtag.js) - Google Analytics