zoukankan      html  css  js  c++  java
  • webService发布和调用--Axis2

     一、工具

    1、下载 Axis2以及eclipse的Axis2插件。http://axis.apache.org/axis2/java/core/download.cgi

    2、axis2-1.7.1-war.zip解压,将压缩包内的axis2.war部署到%TOMCAT-HOME%/webapps下,启动tomcat,访问http://localhost:8080/axis2/看是否正常。

            点击Service会进入Service列表页面,当前只有一个Version服务。http://localhost:8080/axis2/services/Version?wsdl

    3、解压缩eclipse插件 axis2-eclipse-codegen-plugin-1.7.1.zip,axis2-eclipse-service-plugin-1.7.1.zip。解压后将plugins 复制到%ECLIPSE_HOME%plugins。

           

    二、Axis2发布Webservice

    a、新建名称为Axis2Service1 的java工程。新建TestWs.java

           

    b.1、打包部署--arr部署方式

        有手动打包和插件打包两种方式,在此我用了插件打包的方式

    1. 手动打包
      • 新建Axis2Service1deploy文件夹,将Axis2Service1in下的class文件复制过来。
      • 新建Axis2Service1deployMETA-INFservices.xml文件

                 

    <service name="AxisService">
     <description>AxisService</description>
     <parameter name="ServiceClass">ws.TestWs</parameter>
     <operation name="showName">
      <messageReceiver
       class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
     </operation>
     <operation name="getName">
      <messageReceiver
       class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
     </operation>
    </service>

          生成aar包 Axis2Service1deploy>jar cvf AxisService.aar . (注意带.号)

      2 . 插件打包

      •  IDE中选择New->other->Axis2 Service Archiver,点击Next;
      • Class File Location:选择Axis2Service1in目录,点击Next;
      • 勾选Skip WSDL,点击Next;
      • Service Archiver 选择jar位置,如果没有jar包就直接点击Next;
      • 勾选Generate the service xml automatically 自动生成service.xml file文件,点击Next
      • service name,输入:AxisService,然后在class name 中填写要发布的类(全路径,如:ws.TestWs),点击load。勾选 Search declared methods only。点击next
      • output File location,输入:D: ; output File Name,输入artiver文件的名称 AxisService。点击finish。
      • 提示 Service Archvie generated successfully! 注册表明,生成成功。

     3、发布AxisService

          AxisService.aar复制到%TOMCAT-HOME%/webapps/axis2/WEB-INF/services下。(不打aar包,Axis2Service1deploy下面复制过去也是可以)

          打开http://localhost:8080/axis2/services/listServices 就可以看到刚才发布的AxisService服务了,下面有两个函数:showName,getName。

    b.2 独立部署

      1、新建java web project工程。

      2、文件复制

          %TOMCAT-HOME%webappsaxis2WEB-INFlib 复制到 Axis2Service2WebRootWEB-INFlib 下,并加入工程引用。

         %TOMCAT-HOME%webappsaxis2WEB-INFconf 复制到 Axis2Service2WebRootWEB-INFconf

         %TOMCAT-HOME%webappsaxis2WEB-INFmodules 复制到 Axis2Service2WebRootWEB-INFmodules

       3、web.xml 代码如下

      

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     <servlet>
      <servlet-name>AxisServlet</servlet-name>
      <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
      <servlet-name>AxisServlet</servlet-name>
      <url-pattern>/services/*</url-pattern>
     </servlet-mapping>
    </web-app>

     2、新建 Axis2Service2srcwsTestWs.java

    package ws;
    public class TestWs {
     public String showName(String name) {return name; }
     public String getName() {return "Axis2Service Sample"; }  
    }

     3、新建Axis2Service2WebRootWEB-INFservices目录。

     4、新建一个AxisService服务

         AxisServiceMETA-INFservices.xml

       

    <service name="AxisService">
     <description>AxisService</description>
     <parameter name="ServiceClass">ws.TestWs</parameter>
     <operation name="showName">
      <messageReceiver
       class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
     </operation>
     <operation name="getName">
      <messageReceiver
       class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
     </operation>
    </service>

     启动tomcat后,访问http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。

    三、AXIS2调用Web Services

     一、客户端stub文件生成

         我用的是插件生成方式

     1、脚本生成方式

         去AXIS2的解压目录下bin(%AXIS2_HOME%in)下执行下面语句

          wsdl2java -uri http://localhost:8085/Axis2Service2/services/AxisService?wsdl -p ws -s -o stub

       -p参数指定了生成的Java类的包名

       -o参数指定了生成的一系列文件保存的根目录

       在stubsrcws自动生成AxisServiceStub.java

    2、插件生成方式

         IDE中选择New->other->Axis2 Code Generator,点击Next;

        勾选Generate Java source code from a WSDL file,点击Next;

       WSDL file location,输入:http://localhost:8085/Axis2Service2/services/AxisService?wsdl,点击Next;

        如果路径不对会提示:Specified WSDL is invalid!, Please select a validated *.wsdl/*.xml file on previous page. 正确的话,点击next;

       指定输入路径,点击Next 提示:All operations completed successfully! 生成成功。

       在D:srcws 自动生成了stub一系列文件,其中ws是包名。

         

        上面2种方式生成的stub类有点不一样,脚本生成方式是单一文件,插件生成方式生成的一系列文件。

    二、客户端调用 脚本生成方式为例子,插件生成的类似。

        1、新建 java工程 Axis2Client

              新建Axis2Clientlib文件夹 将%AXIS2_HOME%lib 下的所有jar包复制到Axis2Clientlib,并加入工程引用中,加入工程引用的方式如下

         点击工程右键--》Build Path --->Configure Build Paht--->

                    

          将通过插件生成在stub文件加入到srcws下,(若是脚本生成方式,则是单一AxisServiceStub.java文件加入到srcws下 )

             

          2、新建TestWs.java,在test包下 主要代码如下

      

    //初始化Sub类
    AxisServiceStub stub = new AxisServiceStub();
    //传递AxisServiceStub.ShowName对象,相关参数在这边赋值。
    AxisServiceStub.ShowName command = new AxisServiceStub.ShowName();
    command.setName("Hello!");
    //取得返回值
    String name = stub.showName(command).get_return();
    System.out.println(name);

     调用成功后控制台输出:Hello!

    扩展学习---AXIS2调用REST Web Services

       使用http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest的方式访问刚才发布成功的WebService。从上面可以看出这个就是rest风格。

       Axis1.0是无法通过showName?name=rest来获取信息的。

      2、使用axis客户端调用

    public class TestRest {
     
        private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService";
       
        public static void main(String[] args) throws AxisFault {
            Options options = new Options();
            options.setTo(new EndpointReference(toEpr));
           
            //客户端REST方式调用服务跟普通服务的区别,REST调用必须加上下面这个代码。
            options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
            ServiceClient sender = new ServiceClient();
            //axis2-1.5.4不需要下面这句代码,否则会报错
            //sender.engageModule(new QName(Constants.MODULE_ADDRESSING));
            sender.setOptions(options);
            OMElement result = sender.sendReceive(getPayload());
            try {
                XMLStreamWriter writer = XMLOutputFactory.newInstance()
                        .createXMLStreamWriter(System.out);
                result.serialize(writer);
                writer.flush();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            } catch (FactoryConfigurationError e) {
                e.printStackTrace();
            }
        }
        private static OMElement getPayload() {
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace(
                    "http://ws", "example1");
            OMElement method = fac.createOMElement("showName", omNs);
            OMElement value = fac.createOMElement("name", omNs);
            value.addChild(fac.createOMText(value, "Rest"));
            method.addChild(value);
            return method;
        }

     说明:

           1、sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); axis2-1.5.4不需要下面这句代码,否则会报错

           2、客户端REST方式调用服务跟普通服务的区别,就是Rest有下面这个代码;

                 options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);

             两者返回的数据都是

    <ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>

     3、getPayload方法

         OMNamespace omNs = fac.createOMNamespace("http://ws", "example1"); 

        指定命名空间,如果没对的话会报如下错误namespace mismatch require http://ws found http://ws1 OMElement method = fac.createOMElement("showName", omNs);

        要传递的方法名为 "showName" OMElement value = fac.createOMElement("name", omNs);

       传递的参数为name value.addChild(fac.createOMText(value, "Rest"));

        传递参数name的值为Rest。

  • 相关阅读:
    servlet的之前与之后的基本使用
    java HashMap插入重复Key值问题
    ConcurrentHashMap底层实现原理(JDK1.7 & 1.8)
    spring cloud实现热加载
    spring cloud各个组件以及概念的解释和基本使用
    深入理解java 虚拟机 jvm高级特性与最佳实践目录
    【leetcode】1、两数之和
    【Java 基础领域】二维数组创建内存图
    【Java EE领域】com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'salary' in 'fi
    【JavaEE领域】com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mp.employee' doesn't exi
  • 原文地址:https://www.cnblogs.com/qingblog/p/5327848.html
Copyright © 2011-2022 走看看