java-eclipse-jee-indigo+axis2的webservice初步使用

参考资料:

 使用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会不成功

(2)连按两次 “Next>” 后,选中 “Generate the service xml automatically”
(3)按下一步,输入 service 名称和类名,我这里输入的服务名是:wshello;类名是我们刚刚写的类名:ws.Hello,这里需要注意加入完整的包名。
(4) 按下一步,输入 service 文件的保存路径和文件名,完成。
选择生成目录为:C:\Program Files\apache-tomcat-7.0.27\webapps\axis2\WEB-INF\services,(tomcat中axis2的环境)
也可以选择其他目录,然后copy到这一目录下。

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,  表明客户端调用成功.

 

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值