axis2 webservice 简单例子

    概念的东西我就不讲了,当你们都会了.(其实是我自己也是半桶水,就不误人子弟了)
    环境大家自己搞定吧,我们直接进入主题吧.
    我用的编译器是ECLIPSE.
    我们来做个简单的登录验证的接口: authenticate传入参数为CredentialsMessage传出参数为String
public class yskyWebService {
    public String authenticate(CredentialsMessage msg) throws Exception{
        return msg.execute();
    }
}

    public class CredentialsMessage implements Serializable {
    private static final long serialVersionUID = -7856385396635295875L;
    private String username;
    private String password;
    public CredentialsMessage() {
    }
    public CredentialsMessage(String username, String password){
        this.username = username;
        this.password = password;
    }
    public String execute() throws Exception{
        return this.username + this.username;
    }
    
    在CONFIG目录下包含一个resources目录下面再包含META_INF目录现包含services.xml和*.WSDL(名字在编译这个文件时自己定义,我们这用yskyAxis2Service.wsdl在下面的ANT脚本中可以看到)
    services.xml中内容如下:

<service name="yskyAxis2Service">  
    <!-- name 要和WSDL文件中的 <wsdl:service name="*"> 的 name一致 -->
    <!-- 也可以自己在services.xml定义WSDL部分的内容,这样就可以不用产生一个和services.xml对应的WSDL文件了-->
    
    <description> The description of the ysky's service  </description>

    <parameter name="ServiceClass"     locked="xsd:false">com.ysky.service.yskyWebService</parameter>
    
    <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> 
</service>

    
    webservice就算写好了,下面我们要把它部署到Tomcat中去:
    我在这用ANT来部署,也可以直接在COMMAND模式下用命令来实现.(ANT中用到的变量请大家自己定义,其实多数都是些路经)
    产生axis2包:
    <target name="creat.axis2.war" description="Creat Axis2.war">
        <ant antfile="${axis2.dir}/webapp/build.xml" target="create.war" dir="${axis2.dir}" />
    </target>
    
    编译webservice
    <target name="compile.service" >
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.dir}/classes" />
        <!--First let's compile the classes-->
        <javac srcdir="${src.dir}"
            destdir="${classes.dir}"
            source="1.5"
            target="1.5"
            debug="true"
            debuglevel="lines"
            classpathref="axis2.classpath"/>
    </target>

    产生webservice.aar
    <target name="generate.service" depends="compile.service, build.wsdl" description="generate.service">
        <!--aar them up -->
        <copy toDir="${build.dir}/classes" failοnerrοr="false">
            <fileset dir="${basedir}/config/resources">
                <include name="**/*.xml"/>
                <include name="**/*.wsdl"/>
            </fileset>
            <fileset dir="${basedir}/config">
                <include name="**/*.*"/>
            </fileset>
        </copy>
    
        <jar destfile="${aar.file}">
            <fileset excludes="**/Test.class" dir="${build.dir}/classes" />
        </jar>
    </target>

部署AXIS2到TOMCAT
    <target name="deploy.axis2.to.tomcat" depends="creat.axis2.war">
        <webapp.deploy url="${tomcat.manager.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${axis2.name}" war="${axis2.dir}/dist/axis2.war" update="true" failοnerrοr="true" />
    </target>

    COPY ARR文件到AXIS2的SERVICE目录下,发布WEBservice
    <target name="deploy" depends="deploy.axis2.to.tomcat , generate.service" description="deploy service to tomcat">
        <!--deploy the test service to Axis2'dir-->
        <copy toDir="${service.dir}" failοnerrοr="false">
            <fileset dir="${build.dir}">
                <include name="**/${ant.project.name}.aar" />
            </fileset>
        </copy>
        
        <!-- restart.tomcat-->
        <ant antfile="${tomcat.home}/build.xml" dir="${tomcat.home}" target="restart.tomcat" inheritall="false" />
    </target>
    
    产生WSDL定义(也可以通过编写services.xml来实现
    <taskdef name="java2WSDL" classname="org.apache.ws.java2wsdl.Java2WSDLTask" classpath="${classes.dir}" classpathref="axis2.classpath"/>
    
    <!-- with all possible options -->
    <target name="build.wsdl" description="build wsdl">
        <java2WSDL className="com.ysky.service.yskyWebService"
            outputLocation="${basedir}/config/resources/META-INF"
            targetNamespace="http://ysky/ts/"
            schemaTargetNamespace="http://ysky/stn/"
            serviceName="yskyAxis2Service"
            outputFileName="yskyAxis2Service.wsdl" >
        </java2WSDL>
    </target>
    
至此,WEBSERVICE部份就算完成了,试一下 http://localhost:8080/Axis2Test/   看看有没有你的SERVICE?

现在我们来看看客户端吧!
客户端简单多了,用ANT调用AXIS2包下的org.apache.axis2.wsdl.WSDL2Java 如下:

<target name="wsdl.to.java" description="wsdl.to.java">
        <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true">
            <arg line="-uri http://localhost:8080/Axis2Test/services/yskyAxis2Service?wsdl
                -p com.ysky.axis2
                -o ${basedir}
                -u" />
            <classpath refid="axis2.classpath" />
        </java>
    </target>
    
    就帮你产生了客户端代码了(注:AXIS网站上说有很多种方法,但我没细看,如果大家有什么心得请告诉我)
    
    写个测试看看能连上WEBSERVICE么?
    public void testAuthenticatePass() throws Exception {
        YskyAxis2ServiceStub serviceClient = new YskyAxis2ServiceStub();
        CredentialsMessage msg = new CredentialsMessage();
        msg.setPassword("test");
        msg.setUsername("test");

        Authenticate ath = new Authenticate();
        ath.setParam0(msg);
        assertEquals("testtest", serviceClient.authenticate(ath).get_return());
    }

因本人是第一次写技术文章,肯定有很多不足的地方,如果你有什么意见或建议请在评论中给我回复,本人将不胜感激.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值