一、环境准备
使用Axis来开发Web services 需要准备 web 服务器,Axis API。本文使用的Web container 是Tomcat5.5, Axis API 的版本为Axis2_1.1.1。
1.1软件下载准备
Tomcat下载地址:http://tomcat.apache.org/download-55.cgi#5.5.20
Axis标准包:
http://apache.justdn.org/ws/axis2/1_1_1/axis2-1.1.1-src.zip
Axis War包:
http://apache.justdn.org/ws/axis2/1_1_1/axis2.war
Axis Eclipse plug-in(代码生成工具和打包工具):
http://apache.justdn.org/ws/axis2/tools/1_1_1/axis2-eclipse-codegen-wizard.zip
http://apache.justdn.org/ws/axis2/tools/1_1_1/axis2-eclipse-service-archiver-wizard.zip
Eclipse+MyEclipse:可以到官方网站下载(本文为3.2+5.0GA)
1.2安装
1》.首先搭建开发环境,需要将下载到的Eclipse解压缩到一个目录。
2》.将下载到的Axis 的两个plug-in解压缩到Eclipse安装目录下的plug-in子目录。
3》.安装MyEclipse5.0GA。然后启动MyEclipse,并选择“File->New->Other”可以找到下面的这些Wizards,这些将是本文中用到的很重要的工具。
4》.下面开始搭建Web Services的部署环境。将下载的tomcat报解压缩到一个目录。完成web container 的安装。
5》.将axis2.war包拷贝到tomcat安装目录下的webapps目录中。
6》.启动Tomcat(windows 下为TOMCA_HOME/bin中的startup.bat;Linux,unix环境为startup.sh文件),打开浏览器输入并访问:http://ip:8080/axis2(或2http://localhost:8080/axis)来查看,如果能看到下面的页面则说明已经安装完成。
二、进入开发
2.1 建立要发布的WebService
1. 在Eclispse中添加一个用户库命名为axis2,将axis2/lib下的包均添加进来。
2. 建立一个JavaProject命名为AccountService,将axis2用户库加入到build path中。
3. 现在开始编写要发布的WebSevice,在src中建包account,建立AccountOper类如下:
package account;
public class AccountOper {
private static int[] fund = {10000, 500, 700, 5800};
public static int accountID = 0;//in {0,1,2}
public boolean setAccountID( int id ) {
if(id < 0 || id > fund.length)
return false;
this.accountID = id;
return true;
}
public boolean checkInput(int money)
{
if (money > fund[accountID]) {
return false;
} else {
return true;
}
}
//deposit
public int deposit(int money)
{
fund[accountID] = fund[accountID] + money;
return fund[accountID];
}
//withdraw
public int withdraw(int money) {
if (checkInput(money)) {
fund[accountID] = fund[accountID] - money;
}
return fund[accountID];
}
public int getAccount() {
return fund[accountID];
}
}
2.2 发布WebService
1. 在META-INF下建立services描述文件services.xml如下:
<service name="AccountService">
<parameter name="ServiceClass"
locked="false">account.AccountOper</parameter>
<operation name="setAccountID">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name="checkInput">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name="deposit">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name="withdraw">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<operation name="getAccount">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>
2. 打包要发布的Service,Eclipse中New --> File --> Other --> Axis2 wizards -->
Axis2 Services Archiver,按照向导选择刚建立的类编译后的class文件和services.xml文件。
1》、选择class文件目录,注意,不是java源文件,而是WEB-INF/classes目录,
2》、连按两次下一步,选择service.xml文件,
3》、按下一步,输入service文件的保存路径和文件名,完成。
这里将保存路径设为D盘,文件名为AccountService.jar,按完成保存成功,然后修改后缀为aar(也可不修改),将其拷贝到Tomcat 5.5/webapps/axis2/WEB-INF/services目录下,重新打开http://localhost:8080/axis2/services/listService就会发现AccountService已经发布成功,现在就可以应用这个Web服务了。
2.3 测试
6. Eclipse中New --> File --> Other --> Axis2 wizards --> Axis2 Code Generator,按向导通过java source文件,即AccoutOper.class生成WSDL文件。这步其实可以省略,当webservice发布后可通过访问http://192.168.1.213:8080/axis2/services/AccountService?wsdl就可以得到wsdl文件,然后根据此URL就可以生成客户端代码,这里主要想演示一下用Axis2 Code Generator工具根据java source文件如何生成WSDL文件。
1》选择Generate a WSDL from a Java source file,按下一步
2》、填入class文件的包及文件名,我这是account.AccountOper,然后按Add Folder增加AccountOper.class的所在目录路径,按Test Class Loading..按钮测试是否成功装载这个类文件,若未装载成功请检查包名、类名和路径是否正确。
3》、按下一步,这里的参数设置我们不用管它,用默认的就行了。
4》、选择wsdl文件的保存位置及确定wsdl文件的文件名AcountServices.wsdl,按完成,WSDL文件生成成功。
7、通过WSDL文件生成AccountServiceCallbackHandler.java和AccountServiceStub.java,可以重新建立Project,我在做的过程中为了方便仍利用刚才建立的Project.
1》、New --> File --> Other --> Axis2 wizards --> Axis2 Code Generator
2》、选择Generate Java source code from a WSDL file,按下一步
3》、按Browse选择wsdl文件,按下一步
4》、使用默认的配置,按下一步。
5》、选择生成文件的存放路径,完成AccountServiceCallbackHandler.java和AccountServiceStub.java的生成。
6》、在工程目录按F5刷新,就可以看到刚生成的文件,但提示都有错误,这是因为包名不一样。
解决方法是将生成的两个文件直接移动到account包下,如图:
8. 编写服务测试程序AccountTest.java如下:
package account;
public class AccountTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String endpoint = "http://localhost:8080/axis2/services/AccountService";
AccountServiceStub stub = new AccountServiceStub(endpoint);
AccountServiceStub.SetAccountID set = new AccountServiceStub.SetAccountID();
set.setId( 2 );
stub.setAccountID( set );
AccountServiceStub.GetAccountResponse get = stub.getAccount();
System.out.println( "Current: " + get.get_return() );
AccountServiceStub.Deposit dep = new AccountServiceStub.Deposit();
dep.setMoney( 120 );
AccountServiceStub.DepositResponse depRes = stub.deposit( dep );
System.out.println( "After Deposit 120: " + depRes.get_return() );
AccountServiceStub.Withdraw draw = new AccountServiceStub.Withdraw();
draw.setMoney( 50 );
AccountServiceStub.WithdrawResponse drawRes = stub.withdraw( draw );
System.out.println( "After Withdraw 50: " + drawRes.get_return() );
AccountServiceStub.GetAccountResponse getLast = stub.getAccount();
System.err.println( "At last: " + getLast.get_return() );
}
}
运行结果如下:
测试成功
三、在局域网其他客户机调用
以下介绍在局域网客户端调用上面发布的AccountService。为了不受杀毒软件及防火墙的影响,测试时我们把杀毒软件及防火墙关闭。
1、新建一个web project
2、New --> File --> Other --> Axis2 wizards --> Axis2 Code Generator,按下一步
3、选择Generate Java source code from a WSDL file,按下一步
4、在文本框中填入http://192.168.1.213:8080/axis2/services/AccountService?wsdl,按下一步
5、这里都使用默认的设置,按下一步
6、选择输出文件的存放路径,我选择输出在工程的src目录下,按完成按钮,客户端代码生成完毕。
7、刷新一下项目,就可以看到刚生成的客户端文件了,AccountServiceStub.java和AccountServiceCallbackHandler.java,但都有错误,别急,这是生成的包名的问题,我们稍微改一下:把包名src.account改成account就行了。
8、写测试类,和在本机测试的一样。
package account;
public class AccountTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String endpoint = "http://localhost:8080/axis2/services/AccountService";
AccountServiceStub stub = new AccountServiceStub(endpoint);
AccountServiceStub.SetAccountID set = new AccountServiceStub.SetAccountID();
set.setId( 2 );
stub.setAccountID( set );
AccountServiceStub.GetAccountResponse get = stub.getAccount();
System.out.println( "Current: " + get.get_return() );
AccountServiceStub.Deposit dep = new AccountServiceStub.Deposit();
dep.setMoney( 120 );
AccountServiceStub.DepositResponse depRes = stub.deposit( dep );
System.out.println( "After Deposit 120: " + depRes.get_return() );
AccountServiceStub.Withdraw draw = new AccountServiceStub.Withdraw();
draw.setMoney( 50 );
AccountServiceStub.WithdrawResponse drawRes = stub.withdraw( draw );
System.out.println( "After Withdraw 50: " + drawRes.get_return() );
AccountServiceStub.GetAccountResponse getLast = stub.getAccount();
System.err.println( "At last: " + getLast.get_return() );
}
}
运行结果:
和在本机测试一样,调用成功!
官方文檔非常多及齊全, 若想進一步了解, 需要花更長的時間:
http://ws.apache.org/axis2/1_0/index.html
Axis2 的文檔及教學非常充足. 官方文檔如下連結:
http://ws.apache.org/axis2/1_0/index.html
Axis2 官方的安裝教學:
http://ws.apache.org/axis2/1_0/installationguide.html
Axis2 用戶指南:
http://ws.apache.org/axis2/1_0/userguide.html
Axis2 WEB Administration 介面的使用教學:
http://ws.apache.org/axis2/1_0/webadminguide.html
Axis2 的設定文檔:
http://ws.apache.org/axis2/1_0/axis2config.html
AXIOM (Axis Object Model) 教學:
http://ws.apache.org/axis2/1_0/OMTutorial.html
Axis2 Eclipse plugin 教學:
http://ws.apache.org/axis2/tools/1_0/eclipse/wsdl2java-plugin.html
分享到:
相关推荐
axis-sample1.4.rar包,有需要的请下载
ET_TSend_Axis2_Sample 使用 Java 和 Axis2 触发发送触发的示例 必须在 /src/porps.xml 文件中更改身份验证和端点数据
Web Services实例,初学者入门,Web Services using Apache Axis2- Sample
Unity中将SampleScenes/AircraftJet2Axis飞行棋替换自己模型
1.解压axis1.4.lib.zip; 2.运行 ResultNotify.bat脚本 ,生成代码。 3.把生成的java代码放到工程中。 4.把deploy.wsdd中的service 元素节的代码放到 server-config.wsdd中。
his S7 project is a sample program for controlling the "Single Axis Positioning" for MOVIAXIS / Technology Editor. A function (FC150) can be used to control the servo inverter with 6 process datas ...
at a frequency sample of 200 Hz.Each file contains nine columns and a different number of rows depending on the test length.1st column is the acceleration data in the X axis measured by the sensor ...
post build 5Axis this is a sample
o 1, 2 or 3 axes offered in same package o Electronically calibrated axis alignment o Single-crystal silicon technology o No intrinsic wear-out effects o Insensitive to magnetic fields o Full EMI...
b)) (4)⾏组合:row_stack((a,b)) 数组的分割函数 (1)⽔平分割:hsplit(a,3) 或者 split(a,3,axis=1) (2)垂直分割:vsplit(a,3) 或者 split(a,3,axis=0) 四、⽂件处理——os库 1.os.system() 运⾏shell命令 2.os....
axis([0, 10, 0, 10]); %限定X,Y轴的范围 grid on; %画出网格线 mic1_positon=[-10,0]; mic2_positon=[0,0]; mic3_positon=[10,0]; wave = audioread('sample.wav'); wave = wave(:,1); %数组第一列 scale = 0.8/...
2.This problem will be judged automatically. Your answer must match exactly, including the capitalization, punctuation, and white-space. This includes the periods at the ends of the lines. 3.All ...
// PLL with X axis gyroscope reference and disable sleep mode while (i2cRead(0x75, i2cData, 1)); if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register Serial.print(F("Error reading sensor")); ...
NetSuite Web服务定制API的示例应用程序。 此应用程序具有以下属性:1)UI驱动2)使用开源Eclipse SWT小部件用Java编写3)依赖于Axis开源库和NetSuite Axis cookie
包括jar包、doc、sample 超越axis、替代XFire,轻松发布及使用WebService
9.1 Sample Rates and Frequency 9.1.1 Bit Depth 9.2 Sound and Picture 9.3 The Next Step 10 Resolutions, Compression, and Rendering 10.1 Resolutions 1o.1.1 Computer Resolution 10.1.2 Interlacing...
Just rotate on your axis and add a small Z translation. FlipImageView Description Small android lib allowing you to flip an imageview easily, by extending FlipImageView. This lib is based on the ...