elicpse+maven的webservice应用

使用webservice 在一个服务中调用另外一个服务。

1、准备工作

使用elicpse的maven插件分别建立:

axis2-test

axis2-testServer

其中axis2-test中pom的依赖如下:

<dependencies>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-codegen</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>


2、在aix2-test中编写服务器类并使用eclipse插件部署服务器代码。


客户端类OprationClient 调用服务类OprationServiceImpl,服务类定义了一个接口OprationService。

服务类: 

package com.hualom.axis.service;
/**
 * @deprecated(操作接口)
 * @author Administrator
 *
 */
public interface OprationService {
	/**
	 * @deprecated(查询操作)
	 * @param message
	 * @return
	 */
	public String search(String message);
	
	/**
	 * @deprecated(非查询操作)
	 * @param message
	 * @return
	 */
	public String noSearch(String message);
}


package com.hualom.axis.service.impl;

import com.hualom.axis.service.OprationService;

public class OprationServiceImpl implements OprationService {

	public String search(String message) {
		
		return message+"  search finish!";
	}

	public String noSearch(String message) {
		return message+" noSearch finish!";
	}

}

   部署的时候需要导入新版的axis2  http://axis.apache.org/axis2/java/core/download.cgi 并解压。

    在eclipse的菜单栏中选择 window-->preferences-->web service -->axis2 preferences 在axis2 runtime 下的 axis2 runtime location中指定解压目录



右击oprationServiceImpl--->webservice --->create webservice  如下:



在service implemention中出现需要部署的服务类 将服务器的运行状态设置为start service 并且设置 service runtime (web服务器) web service runtime (axis的版本)

对应的web项目。勾选publish the web service 和monitor the service  ---> next --->next直到:


点击 start server 启动刚刚部署的web项目 --->next 直到 finish 此时部署就已经完成了。


3、在axis-test中编写客户端代码并调用服务器程序

package com.hualom.axis.client;

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 OprationClient {
    /** 
     * @param args 
     * @throws AxisFault 
     */  
    public static void main(String[] args) throws AxisFault {  
        // 使用RPC方式调用WebService  
        RPCServiceClient serviceClient = new RPCServiceClient();  
        Options options = serviceClient.getOptions();  
        // 指定调用WebService的URL  
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2-testServer/services/OprationServiceImpl");  
        options.setTo(targetEPR);  
        // 指定要调用的计算机器中的方法及WSDL文件的命名空间:impl.service.axis.hualom.com 服务类的包名称。  
        QName searchResult = new QName("http://impl.service.axis.hualom.com","search");
        QName noSearchResult = new QName("http://impl.service.axis.hualom.com","noSearch");
        // 设置发送到服务器的内容
        Object[] opAddEntryArgs = new Object[] { "你好 server"};  
        // 指定返回值的类型
        Class[] classes = new Class[] { String.class };
        // 调用服务器开放的接口并且返回结果   如果没有返回值可以直接调用  serviceClient.invokeRobust(opName, opAddEntryArgs)
        System.out.println(serviceClient.invokeBlocking(searchResult,opAddEntryArgs, classes)[0]);  
    }  
}

总结:

服务器访问路径默认

http://localhost:8080/axis2-testServer/services/OprationServiceImpl

实际是http://localhost:8080/axis2-testServer/services+刚才部署的服务类名称OprationServiceImpl

调用服务器的方法需要指定 命名空间:"http://impl.service.axis.hualom.com" 这个空间一般就是服务类包名称倒过来写。


注意:

如果部署服务类的时候出现问题

1、将服务器中部署的项目删除

2、可以为eclipse换一个 工作空间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值