spring集成axis2-1.5.6实现webservice服务器端及客户端

1、下载axis2-1.5.6,相关的文件我已经上传,可以到我的资源里去下载

2、新建一个WebProject

以下是myeclipse WebProject的文件目录

spring集成axis2-1.5.6实现webservice服务器端及客户端_第1张图片

 

3、添加jar包,简单起见将axis2-1.5.6-bin中lib下的jar全部导入,同时导入spring.jar

4、编写接口类IFileManagerDao

 

package com.alpha.dao;

public interface IFileManagerDao {

	public abstract String add();

	public abstract String del();

	public abstract String upload();

	public abstract String down();

}


5、编写实现类FileManagerDao

package com.alpha.dao.impl;

import com.alpha.dao.IFileManagerDao;

public class FileManagerDao implements IFileManagerDao {
	public String add(){
		System.out.println("add success..");
		return "add";
	}
	
	public String del(){
		System.out.println("del success..");
		return "del";
	}
	
	public String upload(){
		System.out.println("upload success..");
		return "upload";
	}
	
	public String down(){
		System.out.println("down success..");
		return "down";
	}
}


6、编写webservice FileWebService

package com.alpha.service;

import com.alpha.dao.IFileManagerDao;

public class FileWebService {
	IFileManagerDao fileManagerDao;
	
	public String add(){
		return fileManagerDao.add();
	}
	
	public String del(){
		return fileManagerDao.del();
	}
	
	public String upload(){
		return fileManagerDao.upload();
	}
	
	public String down(){
		return fileManagerDao.down();
	}

	public IFileManagerDao getFileManagerDao() {
		return fileManagerDao;
	}

	public void setFileManagerDao(IFileManagerDao fileManagerDao) {
		this.fileManagerDao = fileManagerDao;
	}
}

 

基础的类都写好了,接下来编写配置文件(不存在的文件自己创建)

7、web.xml



	
	
	   
		contextConfigLocation
		classpath:conf/applicationContext.xml
	   
	  
	
		
			org.springframework.web.context.ContextLoaderListener
		
	
	
	
    
        AxisServlet
        
            org.apache.axis2.transport.http.AxisServlet
        
        1
    
    
        AxisServlet
        /services/*
    
    
  
    index.html
    index.jsp
  


8、spring配置文件applicationContext.xml



	 
	 
	
	
	
	
	
	
		
	


 

9、services.xml(在WebRoot/services/axis/META-INF/下,需自己创建,axis可任意命名)



    
        spring集成axis2案例
        
        
            org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
        
        
        
            fileWebService
        
        
        
            
            
        
    

 


至此服务器端就已经编写完成。接下来只需启动tomcat就可以发布webservice,可以通过下面的路径查看wsdl

http://localhost:8080/WebServiceTest/services/fileWebService?wsdl
http://ip:port/工程名/services/webservice名?wsdl(webservice名是在services.xml中定义的)

 

10、客户端调用webservice

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class WebServiceClient {

	public static void main(String[] args) throws AxisFault {
		// 建立一个远程连接客户端
		RPCServiceClient serviceClient = new RPCServiceClient();
		// 设置参数
		Options options = serviceClient.getOptions();
		// 远程webservice服务地址 ,http://ip:port/工程名/services/webservice名
		EndpointReference targetEPR = new EndpointReference(
				"http://localhost:8080/WebServiceTest/services/fileWebService");
		options.setTo(targetEPR);
		// 方法的参数 (如果没有参数opArgs不能为NULL,Object[] opArgs = new Object[] {
		// }就可以,然后把opArgs传入invokeBlocking)
		Object[] opArgs = new Object[] { "filename" };
		// 返回参数类型
		Class[] classes = new Class[] { String.class };
		serviceClient.setTargetEPR(targetEPR);
		// 此处的是 实现类所在的包的倒序
		QName opName = new QName("http://service.alpha.com", "add");
		// 可使用在services.xml中targetNamespace中指定的命名
		// QName opName = new QName("http://service.crystal.com", "down");
		String ret = serviceClient.invokeBlocking(opName, opArgs, classes)[0]
				.toString();
		System.out.println(ret);
		opArgs = new Object[] { 1 };
		classes = new Class[] { String.class };
		opName = new QName("http://service.crystal.com", "del");
		ret = serviceClient.invokeBlocking(opName, opArgs, classes)[0]
				.toString();
		System.out.println(ret);
		serviceClient.cleanupTransport();
		System.out.println(ret);
	}

}



 

你可能感兴趣的:(spring集成axis2-1.5.6实现webservice服务器端及客户端)