1.參考文献:

1.利用Java编写简单的WebService实例  http://nopainnogain.iteye.com/blog/791525

2.Axis2与Eclipse整合开发Web Service  http://tech.ddvip.com/2009-05/1242968642120461.html

3.http://blog.csdn.net/lightao220/article/details/3489015

4.http://clq9761.iteye.com/blog/976029

5.使用Eclipse+Axis2+Tomcat构建Web Services应用(实例解说篇)

2.实例1(主要看到[2])

2.1.系统功能:

开发一个计算器服务CalculateService,这个服务包括加(plus)、减(minus)、乘(multiply)、除(divide)的操作。

2.2.开发前准备:

  1. 安装Eclipse-jee;
  2. 下载最新版本号的Axis2,网址http://axis.apache.org/axis2/java/core/download.cgi ,选择Standard Binary Distribution的zip包,解压缩得到的文件夹名axis2-1.4.1,文件夹内的文件结构例如以下:

2.3.开发前配置:

在Eclipse的菜单条中,Window --> Preferences --> Web Service --> Axis2 Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点"OK"即行。(如图)

2.4.开发Web Service:

(1)新建一个Java Project,命名为"WebServiceTest1"

(2)新建一个class,命名为"CalculateService",完整代码例如以下:

package edu.sjtu.webservice;
/**
* 计算器运算
* @author rongxinhua
*/
public class CalculateService {
//加法
public float plus(float x, float y) {
return x + y;
}
//减法
public float minus(float x, float y) {
return x - y;
}
//乘法
public float multiply(float x, float y) {
return x * y;
}
//除法
public float divide(float x, float y) {
if(y!=0)
{
return x / y;
}
else
return -1;
}
}

(3)在"WebServiceTest1"项目上new --> other,找到"Web Services"以下的"Web Service";

(4)下一步(next),在出现的Web Services对象框,在Service implementation中点击"Browse",进入Browse Classes对象框,查找到我们刚才写的写的CalculateService类。(例如以下图)。点击"ok",则回到Web Service话框。

(5)在Web Service对话框中,将Web Service type中的滑块,调到"start service“的位置,将Client type中的滑块调到"Test client"的位置。

(6)在Web Service type滑块图的右边有个"Configuration",点击它以下的选项,进入Service Deployment Configuration对象框,在这里选择对应的Server(我这里用Tomcat6.0)和Web Service runtime(选择Apache Axis2),例如以下图:

(7)点OK后,则返回到Web Service对话框,同理,Client type中的滑块右边也有"Configuration",也要进行对应的置,步骤同上。完毕后,Next --> next即行。进入到Axis2 Web Service Java Bean Configuration,我们选择Generate a default services.xml,例如以下图所看到的:

(8)到了Server startup对话框,有个按键"start server"(例如以下图),点击它,则可启动Tomcat服务器了。

(9)等启完后,点击"next -- > next",一切默认即行,最后,点击完毕。最后,出现例如以下界面:(Web Service Explorer),我们在这里便可測试我们的Web服务。(使用浏览器打开的话使用例如以下地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp?org.eclipse.wst.ws.explorer=3)。例如以下图所看到的:

注:在浏览器中打开Web Service Explorer(有时候在eclipse中关闭了webservice explorer,能够用这样的方法打开)

首先登录地址:http://127.0.0.1:19189/wse/wsexplorer/wsexplorer.jsp。然后在网页右上角选择Web Service Exoplorer标签。然后输入WSDL地址:http://localhost:8080/WebServiceTest1/services/CalculateService?wsdl 。这个wsdl地址就是我们刚才公布服务的那个wsdl。点击go,例如以下图所看到的:

然后就能够看到例如以下界面了:

(10)測试比較简单,比如,我们选择一个"plus"的Operation(必须是CalculateServiceSoap11Binding),出现下图,在x的输入框中输入1,在y的输入框中输入2,点击"go",便会在status栏中显示结果3.0。其它方法的測试也类似。结果如上图所看到的。

2.5.CalculateServiceclient调用程序

前面我们已经定义好了加减乘除的方法并将这些方法公布为服务,那么如今要做的就是调用这些服务就可以。client调用程序例如以下代码所看到的:CalculateServiceTest.java
package edu.sjtu.webservice.test;

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 CalculateServiceTest { /**
* @param args
* @throws AxisFault
*/
public static void main(String[] args) throws AxisFault {
// TODO Auto-generated method stub // 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/WebServiceTest1/services/CalculateService");
options.setTo(targetEPR); // 指定要调用的计算机器中的方法及WSDL文件的命名空间:edu.sjtu.webservice。
QName opAddEntry = new QName("http://webservice.sjtu.edu","plus");//加法
QName opAddEntryminus = new QName("http://webservice.sjtu.edu","minus");//减法
QName opAddEntrymultiply = new QName("http://webservice.sjtu.edu","multiply");//乘法
QName opAddEntrydivide = new QName("http://webservice.sjtu.edu","divide");//除法
// 指定plus方法的參数值为两个,各自是加数和被加数
Object[] opAddEntryArgs = new Object[] { 1,2 };
// 指定plus方法返回值的数据类型的Class对象
Class[] classes = new Class[] { float.class };
// 调用plus方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]);
System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]);
System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]); }
}

执行结果:

3.0
-1.0
2.0
0.5

3.实例2.HelloService

(1)首先定义服务方法,代码例如以下所看到的:

package edu.sjtu.webservice;

public class HelloService {
public String sayHelloNew() {
return "hello";
} public String sayHelloToPersonNew(String name) {
if (name == null) {
name = "nobody";
}
return "hello," + name;
} public void updateData(String data) {
System.out.println(data + " 已更新。");
}
}

(2)參考实例1将这种方法公布为服务。

(3)编写client代码调用WebService(主要參考[5])

本文样例与其它样例最大的不同就在这里,其它样例一般须要依据刚才的服务wsdl生成clientstub,然后通过stub来调用服务,这样的方式显得比較单一,client必须须要stub存根才可以訪问服务,非常不方面。本样例的client不採用stub方式,而是一种实现通用的调用方式,不须要不论什么client存根就可以訪问服务。仅仅须要指定对于的web servce地址、操作名、參数和函数返回类型就可以。代码例如以下所看到的:

HelloServiceTest2.java

package edu.sjtu.webservice.test;

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 HelloServiceTest2 {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR; public HelloServiceTest2(String endpoint) throws AxisFault {
serviceClient = new RPCServiceClient();
options = serviceClient.getOptions();
targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
} public Object[] invokeOp(String targetNamespace, String opName,
Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
ClassNotFoundException {
// 设定操作的名称
QName opQName = new QName(targetNamespace, opName);
// 设定返回值
// Class<?>[] opReturn = new Class[] { opReturnType };
// 操作须要传入的參数已经在參数中给定,这里直接传入方法中调用
return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
} /**
* @param args
* @throws AxisFault
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws AxisFault,
ClassNotFoundException {
// TODO Auto-generated method stub
final String endPointReference = "http://localhost:8080/WebServiceTest1/services/HelloService";
final String targetNamespace = "http://webservice.sjtu.edu";
HelloServiceTest2 client = new HelloServiceTest2(endPointReference); String opName = "sayHelloToPersonNew";
Object[] opArgs = new Object[] { "My Friends" };
Class<?>[] opReturnType = new Class[] { String[].class }; Object[] response = client.invokeOp(targetNamespace, opName, opArgs,
opReturnType);
System.out.println(((String[]) response[0])[0]);
} }

执行该程序,点击Run As->Java application,能够看到控制台端口的输出是:Hello, My Friends,表明客户端调用成功。该样例最大的不同和优势表如今客户端的调用方式,或者说是发起服务调用的方式,尽管比起客户端stub存根的方式,代码稍多,可是这样的方式统一,不须要生产stub存根代码,攻克了客户端有非常多类的问题。假设读者对这些代码进一步封装,我想调用方式非常easy,仅仅须要传递相关參数,这更好地说明了服务调用的优势。并且这样的方式更加简单明了,一看便知详细含义。而不须要弄得stub类的一些机制。

(4)改写client调用服务的代码

(3)中提到的client应用代码写的稍微有些繁杂,以下将上面的client调用service程序进行改写,简洁了很多。代码例如以下:

HelloServiceTest.java

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 HelloServiceTest {
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/WebServiceTest1/services/HelloService");
options.setTo(targetEPR); // 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew");
// 指定sayHelloToPerson方法的參数值
Object[] opAddEntryArgs = new Object[] { "xuwei" };
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
Class[] classes = new Class[] { String.class };
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
}
}

eclipse+webservice开发实例的更多相关文章

  1. 转:Eclipse+webservice开发实例

    原文地址:http://blog.csdn.net/xw13106209/article/details/7049614 1.参考文献: 1.利用Java编写简单的WebService实例  http ...

  2. eclipse+axis2+webservice开发实例

    myeclipse10安装axis2插件 第一步:下载axis2-1.6的插件压缩包,axis2-eclipse-codegen-plugin-1.6.2.zip 和 axis2-eclipse-se ...

  3. 基于JAX-WS的webService开发实例

    最近因为工作原因接触到webService,所以记录下开发中碰到的问题,方便自己以后复习,顺便发扬一下开源精神.刚刚接触webServie如果有什么错误欢迎大家指正. 本地环境:myEclipse10 ...

  4. WebService开发实例(Axis2实现,无需安装,快速实现)

    曾经做过的项目里涉及Android客户端向服务器发送请求,服务器访问数据库获得数据并返回给Android客户端.当时Android客户端与服务器的通信已经实现,我只负责客户端布局和数据呈现的部分,近日 ...

  5. eclipse下的webservice开发

    关于eclipse下的webservice开发,有非常多的教程,这里只记下学习过程中的弯路: 1.无论是CXF模式还是AXIS模式,在出现start server之后,点击next报错:"s ...

  6. VS2008中C#开发webservice简单实例

    1.创建工程 文件-> 新建->网站 如下图. 工程建好后,会自动添加如下代码: using System; using System.Linq; using System.Web; us ...

  7. 【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解

    Eclipse插件开发6--eclipse在线翻译插件Translator开发实例详解 在上一篇文章中讲到了一个简单的eclipse插件开发实例,主要是对插件工程的基本创建步骤进行了讲解,这篇文章当中 ...

  8. 【eclipse插件开发实战】 Eclipse插件开发5——时间插件Timer开发实例详解

    Eclipse插件开发5--时间插件Timer开发实例详解 这里做的TimeHelper插件设定为在菜单栏.工具栏提供快捷方式,需要在相应地方设置扩展点,最后弹出窗体显示时间. 在上一篇文章里创建好了 ...

  9. 使用eclipse搭建第一个python+Django的web开发实例

    python+Django的web开发实例   一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的 ...

随机推荐

  1. 转载:C++ map的基本操作和使用

    声明:本文转自:http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html 1.map简介 map是一类关联式容器.它的特点是增 ...

  2. J2EE中你必须了解的13种技术规范

    1)JDBC(Java Database Connectivity): JDBC API为访问不同的数据库提供了一种统一的途径,象ODBC一样,JDBC对开发者屏蔽了一些细节问题,另外,JDCB对数据 ...

  3. JDK_Proxy_InvocationHandler_动态代理

    本文用jdk动态代理模拟了spring的AOP的实现技术 AOP面向切面编程,可用于权限验证,效率检查,事务,异常管理等 JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Pr ...

  4. bzoj3294

    感觉自己就是不怎么擅长计数的问题 设f[k,i,j]表示前k种颜色占据了i行j列的方案 g[k,i,j]表示第k种颜色占据了i行j列的方案,注意要减去并没占据满i行j列的情况 然后转移就很好写了 像这 ...

  5. poj2752 水题

    又2b了一次…… var s:ansistring; ans,pre:..] of longint; i,k,tot:longint; procedure main; begin pre[]:=;k: ...

  6. LeetCode Pascal's Triangle II (杨辉三角)

    题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...

  7. 【转】Android中的颜色设置

    原文网址:http://www.cnblogs.com/bluestorm/p/3644669.html 1.在android中经常看到设置的颜色为八位的十六进制的颜色值,例如: 1 2 3 publ ...

  8. 也用 Log4Net 之将日志记录到数据库的后台实现 (二)

    也用 Log4Net 之将日志记录到数据库的后台实现 (二)  大家下午好,昨天讲了配置,今天我们讲讲后台实现,在完成了后台实现后,我们才能真正意义上的解决把自定义属性字段值录入到数据库中. 在开写之 ...

  9. 问题与解答 [Questions & Answers]

    您可以通过发表评论的方式提问题, 我如果有时间就会思考,  并给出答案的链接. 如果您学过Latex, 发表评论的时候请直接输入Latex公式; 反之, 请直接上传图片 (扫描.拍照.mathtype ...

  10. java jvm学习笔记五(实践自己写的类装载器)

     欢迎装载请说明出处:http://blog.csdn.net/yfqnihao 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和 ...