代码人生的小狗窝

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_XML/SOAP

SOAP得Axis2的运用(一)

发布时间:2018-10-16浏览(2656)

    SOAP得Axis2的使用(一)
    SOAP得Axis2的使用(一)

    首页
    http://ws.apache.org/axis2/

    GuickStart Guide
    http://ws.apache.org/axis2/1_5_1/quickstartguide.html#introduction

    Axis2 Quick Start Guide
    It introduce many methods here, I just read them and find that axis2.war is useful. So I copy the code from
    F:\book\opensource\axis2\axis2-1.5.1-src\axis2-1.5.1\modules\webapp
    and build a project easyaixs to be a management system of my own axis2 webservice.

    my ivy.xml will be like this:
    <ivy-module version="2.0">
    <info organisation="sccl" module="swak" />
    <configurations>
    <conf name="release" />
    </configurations>
    <publications>
    <artifact name="common" />
    <artifact name="client" />
    </publications>
    <dependencies>
    <!-- commons -->
    <dependency org="commons-logging" name="commons-logging" rev="1.1.1" />
    <dependency org="commons-fileupload" name="commons-fileupload" rev="1.1.1" />
    <dependency org="commons-httpclient" name="commons-httpclient" rev="3.1" />
    <dependency org="commons-io" name="commons-io" rev="1.4" />
    <dependency org="commons-codec" name="commons-codec" rev="1.4" />
    <!--  axis2 -->
    <dependency org="org/apache/axis2" name="axis2-kernel" rev="1.5.1" />
    <dependency org="org/apache/axis2" name="axis2-adb" rev="1.5.1" />
    <dependency org="org/apache/axis2" name="axis2-jaxws" rev="1.5.1" />
    <dependency org="org/apache/axis2" name="axis2-transport-http" rev="1.5.1" />
    <dependency org="org/apache/axis2" name="axis2-transport-local" rev="1.5.1" />
    <dependency org="wsdl4j" name="wsdl4j" rev="1.6.2" />
    <dependency org="org/apache/ws/commons/schema" name="XmlSchema" rev="1.4.3" />
    <dependency org="org/apache/ws/commons/axiom" name="axiom-api" rev="1.2.8" />
    <dependency org="org/apache/ws/commons/axiom" name="axiom-impl" rev="1.2.8" />
    <dependency org="org/apache/neethi" name="neethi" rev="2.0.4" />
    <dependency org="javax/mail" name="mail" rev="1.4" />
    <dependency org="org/apache/woden" name="woden-api" rev="1.0M8" />
    <dependency org="org/apache/httpcomponents" name="httpcore" rev="4.0" />
    <dependency org="org/codehaus/woodstox" name="wstx-asl" rev="3.2.4" />
    </dependencies>
    </ivy-module>

    And the username and password of the admin console is configured in axis2.xml with location WEB-INF/conf/axis2.xml, and it will be admin/axis2 in default situation.

    After that I just follow the article
    http://ws.apache.org/axis2/1_5_1/pojoguide.html
    POJO Web Services using Apache Axis2                  
    to build a webservice example.

    撰写一个简单的POJO,Weather.java如下:
    package com.sillycat.axis2.weather.models;

    public class Weather {
    float temperature;
    String forecast;
    boolean rain;
    float howMuchRain;
    ...getter and setter snip...
    }

    一个简单的service服务类WeatherService.java如下:
    package com.sillycat.axis2.weather.services;

    import java.util.Date;

    import com.sillycat.axis2.weather.models.Weather;

    public class WeatherService {

    Weather weather;

    public void setWeather(Weather weather) {
    this.weather = weather;
    }

    public Weather getWeather() {
    return this.weather;
    }

    public void echoTheWeather() {
    System.out.println("Today=" + new Date() + " temperature="
    + weather.getTemperature());
    }

    }

    配置文件services.xml如下:
    <service name="WeatherService" scope="application">
    <description>
    Weather POJO Service
        </description>
    <messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>
    <parameter name="ServiceClass">com.sillycat.axis2.weather.services.WeatherService
    </parameter>
    </service>

    打包脚本也准备好了,build.properties如下:
    service.name=WeatherService
    service.version=1.0
    catalina.home=E:\\tool/apache-tomcat-6.0.20/
    ant.encoding=UTF-8
    java.level=1.5
    compile.debug=true
    compile.deprecation=true
    compile.optimize=true
    axis.home=E:\\work/easyaxis/

    build.xml脚本如下:
    <project name="${app.name}" default="all" xmlns:ivy="antlib:org.apache.ivy.ant">
    <!-- some variables used -->

    <property file="build.properties" />
    <property name="service.name" value="${service.name}" />
    <property name="src.dir" value="src" />
    <property name="conf.dir" value="conf" />
    <property name="build.dir" value="build" />
    <property name="dist.dir" value="dist" />
    <property name="web.dir" value="WebContent" />
    <property name="lib.dir" value="${web.dir}/WEB-INF/lib" />
    <property name="tomcat.dir" value="${catalina.home}" />
    <property name="ivy.install.version" value="2.0.0" />
    <property name="ivy.home" value="${user.home}/.ant" />
    <property name="ivy.jar.dir" value="${ivy.home}/lib" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />

    <property name="repository.path" value="${axis.home}/WebContent/WEB-INF/" />

    <!-- paths used for compilation and run  -->
    <path id="compile.path.id">
    <fileset dir="${tomcat.dir}\lib" />
    <fileset dir="${lib.dir}" />
    <path location="${build.dir}" />
    </path>

    <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpath="${ivy.jar.file}" />
    <target name="download-ivy">
    <mkdir dir="${ivy.jar.dir}" />
    <get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" dest="${ivy.jar.file}" usetimestamp="true" />
    </target>

    <!-- ================================= target: resolve ================================= -->
    <target name="resolve" depends="download-ivy" description="--> retreive dependencies with ivy">
    <ivy:retrieve pattern="${web.dir}/WEB-INF/lib/[artifact]-[revision].[ext]" />
    </target>

    <!-- ================================= target: clean-cache ================================= -->
    <target name="clean-cache-lib" description="--> clean the ivy cache">
    <delete dir="${ivy.home}/cache" />
    </target>

    <!-- ================================= target: clean ================================= -->
    <target name="clean" description="--> clean the project">
    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
    </target>

    <!-- ================================= target: prepare ================================= -->
    <target name="prepare" description="--> make-dir build , dist">
    <tstamp />
    <mkdir dir="${build.dir}" />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${build.dir}/${service.name}/classes" />
    <mkdir dir="${build.dir}/${service.name}/META-INF" />
    <echo message="built at ${DSTAMP}-${TSTAMP}" />
    <echo message="ant.version - ${ant.version}" />
    <echo message="ant.java.version - ${ant.java.version}" />
    <echo message="ivy.cache.dir - ${ivy.home}/cache" />
    </target>

    <target name="generate.service" depends="clean,prepare">
    <copy file="${conf.dir}/services.xml" tofile="${build.dir}/${service.name}/META-INF/services.xml" overwrite="true" />
    <javac srcdir="${src.dir}" destdir="${build.dir}/${service.name}/classes" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" encoding="${ant.encoding}" source="${java.level}">
    <classpath refid="compile.path.id" />
    </javac>
    <jar destfile="${dist.dir}/${service.name}-${service.version}.aar" basedir="${build.dir}/${service.name}/classes">
    <metainf dir="${build.dir}/${service.name}/META-INF/">
    <include name="services.xml" />
    </metainf>
    </jar>
    <copy file="${dist.dir}/${service.name}-${service.version}.aar" tofile="${repository.path}/services/${service.name}-${service.version}.aar" overwrite="true" />
    </target>

    <target name="all" depends="clean,generate.service" description="clean, build jar package, generate api-doc">
    </target>

    </project>

    打包后,直接将打好的Weather-1.0.saar拷贝到easyaxis下面的services目录了,启动easyaxis就发布了服务了。同时,我还参考示例写了个测试类WeatherServiceTest.java:
    package com.sillycat.axis2.weather;

    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;

    import com.sillycat.axis2.weather.models.Weather;

    public class WeatherServiceTest {
    @SuppressWarnings("unchecked")
    public static void main(String[] args1) throws AxisFault {
    RPCServiceClient serviceClient = new RPCServiceClient();
    Options options = serviceClient.getOptions();
    // EndpointReference targetEPR = new EndpointReference(
    // "http://localhost:8080/easyaxis/services/WeatherService");
    EndpointReference targetEPR = new EndpointReference(
    "http://192.168.1.127:8080/easyaxis/services/WeatherService");
    options.setTo(targetEPR);
    // Setting the weather
    QName opSetWeather = new QName(
    "http://services.weather.axis2.sillycat.com", "setWeather");
    Weather w = new Weather();
    w.setTemperature((float) 39.3);
    w.setForecast("Cloudy with showers");
    w.setRain(true);
    w.setHowMuchRain((float) 4.5);
    Object[] opSetWeatherArgs = new Object[] { w };
    serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);

    // Getting the weather
    QName opGetWeather = new QName(
    "http://services.weather.axis2.sillycat.com", "getWeather");
    Object[] opGetWeatherArgs = new Object[] {};
    Class[] returnTypes = new Class[] { Weather.class };
    Object[] response = serviceClient.invokeBlocking(opGetWeather,
    opGetWeatherArgs, returnTypes);

    Weather result = (Weather) response[0];

    if (result == null) {
    System.out.println("Weather didn't initialize!");
    return;
    }

    // Displaying the result
    System.out.println("Temperature               : "
    + result.getTemperature());
    System.out.println("Forecast                  : "
    + result.getForecast());
    System.out.println("Rain                      : " + result.isRain());
    System.out.println("How much rain (in inches) : "
    + result.getHowMuchRain());
    }
    }

    由于这个测试类,还必须导入写jar包才行,ivy.xml如下:
    <dependencies>
    <!-- commons -->
    <dependency org="commons-logging" name="commons-logging" rev="1.1.1" />
    <dependency org="commons-httpclient" name="commons-httpclient" rev="3.1" />
    <dependency org="commons-codec" name="commons-codec" rev="1.4" />
    <!--  axis2 -->
    <dependency org="org/apache/axis2" name="axis2-kernel" rev="1.5.1" />
    <dependency org="org/apache/axis2" name="axis2-adb" rev="1.5.1" />
    <dependency org="org/apache/ws/commons/axiom" name="axiom-api" rev="1.2.8" />
    <dependency org="org/apache/ws/commons/axiom" name="axiom-impl" rev="1.2.8" />
    <dependency org="wsdl4j" name="wsdl4j" rev="1.6.2" />
    <dependency org="org/apache/ws/commons/schema" name="XmlSchema" rev="1.4.3" />
    <dependency org="org/apache/neethi" name="neethi" rev="2.0.4" />
    <dependency org="org/apache/axis2" name="axis2-transport-local" rev="1.5.1" />
    <dependency org="org/apache/axis2" name="axis2-transport-http" rev="1.5.1" />
    <dependency org="javax/mail" name="mail" rev="1.4" />
    <dependency org="org/apache/httpcomponents" name="httpcore" rev="4.0" />
    </dependencies>

    这也是参考官方文档做了练习,以后要使用,可能还有很多细节要看。

    问题一:
    我用写的Client访问WS服务的时候。
    出错信息:
    信息: Unable to sendViaPost to url[http://localhost:8080/easyaxis/services/WeatherService]
    org.apache.axis2.AxisFault: Transport error: 404 Error: Not Found
    at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:310)
    解决方案:
    难道我写错了,命名wsdl文件都可以访问哒,看到404,怀疑是不是网络问题,修改为127.0.0.1还是不行,非要改成自己的IP才行。
    // EndpointReference targetEPR = new EndpointReference(
    // "http://localhost:8080/easyaxis/services/WeatherService");
    EndpointReference targetEPR = new EndpointReference(
    "http://192.168.1.127:8080/easyaxis/services/WeatherService");

    问题二:
    直接访问WSDL文件http://localhost:8080/easyaxis/services/Version?wsdl,成功,访问
    http://localhost:8080/easyaxis/services/Version 却报错。
    出错信息:
    严重: The endpoint reference (EPR) for the Operation not found is /easyaxis/services/Version and the WSA Action = null
    org.apache.axis2.AxisFault: The endpoint reference (EPR) for the Operation not found is /easyaxis/services/Version and the WSA Action = null
    at org.apache.axis2.engine.DispatchPhase.checkPostConditions(DispatchPhase.java:89)
    解决方案:
    最先以为是自己的问题,后来发现axis2给的例子都不得行,原来调用的时候必须加上方法名作为参数,比如要这么访问:
    http://localhost:8080/easyaxis/services/Version/getVersion
    1 楼 522374218 2012-09-14  
    说的很好。赞一下。