参考资料:
使用Eclipse+Axis2開發Web Service應用 http://www.guan8.net/Java/74696.html
使用Eclipse+Axis2构建Web Service应用 http://panpan.blog.51cto.com/489034/119204
Axis2 WebService(配置、发布、调用) http://www.lifeba.org/arch/java_axis2_webservice.html
1.下载Eclipse的axis2插件
axis2-eclipse-codegen-plugin-1.5.4.zip
axis2-eclipse-service-plugin-1.5.4.zip
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.5.4/axis2-eclipse-codegen-plugin-1.5.4.zip
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.5.4/axis2-eclipse-service-plugin-1.5.4.zip
2.解压后,将plugins文件夹中的
Axis2_Codegen_Wizard_1.3.0
Axis2_Service_Archiver_1.3.0
copy到Eclipse的plugins文件夹中,然后重新启动Eclipse
3.启动后,File--New--Other--Axis2Wizards中会看到
Axis2 Code Generator
Axis2 Service Archiver
两个东东,表示插件安装成功。
4.File--New--JavaProject,新建一个项目,假设名字叫WebServiceServer
5.新建JavaPackage,叫ws,然后再其中新建Class,叫Hello.java,代码如下
package ws;
public class Hello {
public String sayHello(String user){
return "Hello,"+ user;
}
}
6.将其打包, Eclipse 中 New --> File --> Other --> Axis2 wizards --> Axis2 Services Archiver ,按照向导选择刚建立的类编译后的 class 文件。
(1) 选择 class 文件目录,注意,不是 java 源文件,而是 classes 目录,
这里需要注意由于你的类是带包ws的,因此不要选到包这个目录。
目录为 workspace\WebServiceServer\bin,这一步设置不对的话,最后的load会不成功
7.测试webservice
http://localhost:8080/axis2/ --> 点击 Services,会看到Hello--> 点击 Hello
8.编写客户端调用代码
(1).File--New--JavaProject,新建一个项目,假设名字叫WebServiceClient
(2) Bulid Path--Add External Archives,把axis2的jar包引入到项目中,路径
C:\Program Files\apache-tomcat-7.0.27\webapps\axis2\WEB-INF\lib
[在 Eclispse 中添加一个用户库,How to do ?????]
(3)新建JavaPackage,叫ws,然后再其中新建Class,叫HelloTest.java,代码如下
package ws;
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 HelloTest {
private RPCServiceClient serviceClient;
private Options options;
private EndpointReference targetEPR;
public HelloTest(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/axis2/services/wshello";
final String targetNamespace = http://ws;
HelloTest client = new HelloTest(endPointReference);
String opName = "sayHello";
Object[] opArgs = new Object[]{"WebServiceCenter"};
Class<?>[] opReturnType = new Class[]{String[].class};
Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType);
System.out.println(((String[])response[0])[0]);
}
}
(4)运行该程序,点击Run As->Java application,可以看到控制台端口的输出是:Hello,WebServiceCenter, 表明客户端调用成功.