Java 使用Axis实现WebService实例

版权声明:本文为博主原创文章,转载请标明出处!博客地址:http://blog.csdn.net/qazwsxpcm https://blog.csdn.net/qazwsxpcm/article/details/78003905

上一篇WebService实例中,基于jdk1.6以上的javax.jws 发布webservice接口。这篇博文则主要用eclipse/myeclipse 使用axis插件进行发布和调用WebService。

1. 下载axis,并解压到tomcat/webapps目录下

官网地址:http://axis.apache.org/axis2/java/core/download.cgi
下载 axis2-xx-war.zip,并解压到tomcat/webapps

这里写图片描述
这里写图片描述

2. 在tomcat部署axis2

启动tomcat, 可以看到多了个axis2文件
在浏览器输入:http://localhost:8080/axis2/
看到axis界面,则成功发布
这里写图片描述

这里写图片描述

3. 在eclipse/myeclipse 安装axis插件

将下载下来的axis2-eclipse-codegen-plugin-x.x.x.zip和axis2-eclipse-service-plugin-x.x.x.zip 解压,解压之后的jar文件复制到eclipse/myeclipse 的dropins目录下,重启eclipse/myeclipse,右键File->New->Other 可以看到axis插件已经安装成功。
这里写图片描述

这里写图片描述

这里写图片描述

4. 发布WebService

将下载下来的axis2-x.x.x-bin 解压,将其中的lib架包添加置项目中。
新建class类,用于发布。
编译该类之后,用axis2发布该类。
右键New -> File -> Other -> Axis2 wizards -> Axis2 Services Archiver 。
选择该class类生成的路径,注意只到classes目录下, 然后next,勾上Skip WSDL,点击next,点击next,service填写发布的名称, class name填写路径,包名加上类名,然后选择发布的方法。继续next,选择tomcat/webapps目录下的axis/web-inf/service。
发布成功后,启动tomcat,在浏览器输入:http://localhost:8080/axis2/services/listServices 。可以看到要发布的webservice ,点击该项目,进入wsdl界面。

/**
 * 
* Title: AxisServiceHello
* Description: Axis2 发布
* Version:1.0.0  
* @author panchengming
 */
public class AxisServiceHello {

    /** 供客户端调用方法  
     * @param name  传入参数
     * @return String 返回结果
     * */
    public String getValue(String name){
        return "Axis 欢迎你! "+name;
    }
}

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

5. 调用WebService

新建一个class类,用于调用发布的webservice。
可以使用rpc或document两种方法调用,运行main方法,看到打印消息,调用成功。
注:调用需要将tomcat服务启动,在浏览器输入wsdl地址能够查看。

import java.io.IOException;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;


/**
 * 
* Title: AxisClientHello
* Description: webService 客户端调用
* Version:1.0.0  
* @author panchengming
 */
public class AxisClientHello {
    private final static String url="http://192.168.1.105:8080/axis2/services/AxisServiceHello?wsdl"; //wsdl地址 
    private final static String data="PanChengMing";                                                   //参数
    private final static String tns = "http://service.pcm.com";                                        //命名空间
    private final static String method="getValue";                                                //调用的方法

     //调用webservice
    public static void main(String[] args) throws  IOException{
            getRPC();           //调用方法一
            getDocument();      //调用方法二
    }



    /**  
     * 方法一:  
     * 应用rpc的方式调用 这种方式就等于远程调用,  
     * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。  
     * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService  
     *  
        【注】:  

            如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数  
              第一个参数的类型是QName对象,表示要调用的方法名;  
              第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];  
                当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。  
              第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。  
            如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法  
              该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。  

            在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,  
            也就是 <wsdl:definitions>元素的targetNamespace属性值。  
     *  
     */  
     @SuppressWarnings("rawtypes")
    public static void getRPC() throws AxisFault{ 
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        // 指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference(url);
        options.setTo(targetEPR);

        // 指定要调用的WSDL文件的命名空间及getValue方法
        QName qn = new QName(tns, method);
        // 指定getValue方法的参数值
        Object[] ob = new Object[] { data };
        // 指定getValue方法返回值的数据类型的Class对象
        Class[] classes = new Class[] { String.class };
        // 调用getValue方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(qn, ob, classes)[0]);
    }

     /**  
      * 方法二: 应用document方式调用  
      * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合  
      */  
     public static void getDocument() throws AxisFault{ 
          OMElement result = null;  
          try {  
              Options options = new Options();  
              // 指定调用WebService的URL    
              EndpointReference targetEPR = new EndpointReference(url);  
              options.setTo(targetEPR);         
              ServiceClient sender = new ServiceClient();  
              sender.setOptions(options);  

              OMFactory fac = OMAbstractFactory.getOMFactory();  
              // 命名空间
              OMNamespace omNs = fac.createOMNamespace(tns, "");  

              OMElement ot = fac.createOMElement(method, omNs);  
              OMElement symbol = fac.createOMElement("name", omNs);  
              symbol.addChild(fac.createOMText(symbol, data));  
              ot.addChild(symbol);  
              result=sender.sendReceive(ot);
              System.out.println(result);  
          } catch (AxisFault axisFault) {  
              axisFault.printStackTrace();  
          }  
     }

}

这里写图片描述

结语:使用axis实现webservice 暂时告一段落了,这次的demo和上篇的webservice的demo 我整合成了一个项目,发布到我的github上了 ,https://github.com/xuwujing/webservice_project 。 有兴趣的可以看看。

猜你喜欢

转载自blog.csdn.net/qazwsxpcm/article/details/78003905