---
title: "使用eclipse插件实现webservice调用"
author: "Ajoker"
date: "2015-03-24T11:22:13Z"
tags: []
categories: ["编程语言"]
slug: "eclipse-webservice"
draft: false
lastmod: "2015-03-24T11:22:13Z"
id: 2194992
url: "https://ajoker.iteye.com/blog/2194992"
---

实现webservice的调用的方法，有好几种，在这里说一种最简单的方法，使用eclipse直接生webservice客户端进行调用，这样做的好处，既提高了调用效率，而且实现起来也很简单。

 

因为项目本来就比较老，现在是后续开发，服务器的jdk版本是1.5的，项目本身也已经有了axis1.4的jar包，直接使用eclipse就可以生成客户端了，具体操作如下图----

 

1.新建web service client

![](http://dl2.iteye.com/upload/attachment/0106/9228/d21d4060-ec61-3511-8039-f4313a24e84d.png)

2.输入wsdl地址，点击下一步

![](http://dl2.iteye.com/upload/attachment/0106/9230/9a048b6e-77e2-35fe-82fc-a3b2c323a8e8.png)

3.选择项目以及存放路径

![](http://dl2.iteye.com/upload/attachment/0106/9232/579a23d3-cea3-3b44-b122-72a9f52b0071.png)  
 4.生成了如图的几个java文件，我初步看了下，下面几个文件是实体bean，上面几个是接口以及实现类

![](http://dl2.iteye.com/upload/attachment/0106/9234/53479037-5a47-375a-a4c4-d2f02ce338c4.png)  
 

上面这么多只是得到将webservice服务器端定义的的接口以及方法，实例化到了我们客户端，使用它得到我们想要的数据，才是我们的目的

 

就我现在手头上这个项目而论，我需要实现的功能是调用webservice实现发送短信的功能。初步查看5个类，可以看出，SMService和SMServicePort是接口，通过SMService对象可以得到SMServicePort，SMServicePort接口可以得到我们需要的方法，而且SMServiceLocator和SMServicePortStub分别实现了这两个接口，所以首先我们需要得到SMService对象，通过SMService得到SMServicePortStub，最后调用定义的方法就可以实现我们需要的功能了，以下是实例：

```
SMService smService = new SMServiceLocator();
SMServicePortStub portStub = (SMServicePortStub)smService.getSMServicePort();
		
SMAuthReq authReq = new SMAuthReq(); 
authReq.setUserName("");
authReq.setPassword("");
		
//得到短信网关返回对象
SMAuthResp smAuthResp = portStub.smAuth(authReq);
		
logger.info(smAuthResp.getSmsg()+"================"+smAuthResp.getCheckNbr());
```

ok，这样就算是完成了简单的调用了。最后，如果想项目简洁的话，可以手动打包成jar包，应用到项目中