zoukankan      html  css  js  c++  java
  • java中用Axis开发webservice的简单实例

    本文主要记录Axis开发webservice简单实例的详细过程和步骤:
    Axis官方网站:http://ws.apache.org/axis/
    可以在官网下载最新1.4的包:axis-bin-1_4.zip
    将解压后的axis-1_4\webapps\下的axis目录考到%TOMCAT_HOME%/Webapps/目录下
    启动tomcat后在浏览器里输入http://localhost:8082/axis会看到下图所示(ps:本人的tomcat端口为8082)

    点击上图中的Validataion链接,页面上会提示已经有的包和缺少的包的信息,根据提示将必须的包下载全,将这些类包复制到 %tomcathome%/webapps/axis/WEB-INF/lib/目录下重新启动tomcat,直到Validation页面中看不到有Error与Warning的提示信息。
    如果提示缺少xmlsec.jar可以到http://santuario.apache.org/dist/java-library/下载.
    Axis支持三种web service的客户端访问方式,分别为:

    • Dynamic Invocation Interface ( DII)
    • Dynamic Proxy方式
    • Stubs方式

    PS:看到很多资料将上述方式列为Web Servcie的三种“部署和开发方法,个人觉得有些欠妥
    下面介绍axis部署和发布web service的方式:
    一、JWS
    JWS(Java Web Service)是最简单的一种方式。Axis允许把普通Java类的源文件的扩展名改为.jws,然后把它简单的copy到AXIS_HOME下。这 样,Axis 会自动编译.jws文件,并把它自动加入到Java Web Servie的服务中。具体过程如下
        1.用Eclipse或者文本编辑器编写一个java类SayHello.java(此类不含包名)
    Java代码

    • public class SayHello{   
    •     public String sayMsg(String name){   
    •         return "Hello,"+name;   
    •     }   
    • }  


    public class SayHello{        public String sayMsg(String name){                return "Hello,"+name;        }}
       2.将上面的类(SayHello.java)copy到%tomcat_home%/webapps/axis/目录下,只需要把类的源文件(不是class)到这个目录下,重命名为:SayHello.jws
       3.打开浏览器输入:http://localhost:8082/axis/SayHello.jws 会看到

    点击上图Click to see the WSDL 的链接,就可以看到生成的wsdl。
       4.客户端Dynamic Invocation Interface ( DII)方式 实现如下:
    Java代码

    • import org.apache.axis.client.Call;   
    • import org.apache.axis.client.Service;   
    •   
    • /**
    • * axis client
    • * @author Michael sun
    • */  
    • public class TestClient {   
    •   
    •     /**
    •      * @param args
    •      * @throws Exception
    •      */  
    •     public static void main(String[] args) throws Exception {   
    •         String wsdlUrl = "http://localhost:8082/axis/SayHello.jws";   
    •         // String wsdlUrl=”http://localhost:8080/axis/SayHello.jws?wsdl”   
    •         Service s = new Service();   
    •         Call call = (Call) s.createCall();   
    •         call.setOperationName("sayMsg");// 这里是要调用的方法名   
    •         call.setTargetEndpointAddress(wsdlUrl);// 设置调用的wsdl   
    •         String val = (String) call.invoke(new Object[] { "My Michael Sun" });   
    •         System.out.println("这是webservice服务器返回的信息:" + val);   
    •     }   
    • }  


    import org.apache.axis.client.Call;import org.apache.axis.client.Service;/** * axis client * @author Michael sun */public class TestClient {    /**     * @param args     * @throws Exception     */    public static void main(String[] args) throws Exception {        String wsdlUrl = "http://localhost:8082/axis/SayHello.jws";        // String wsdlUrl=”http://localhost:8080/axis/SayHello.jws?wsdl”        Service s = new Service();        Call call = (Call) s.createCall();        call.setOperationName("sayMsg");// 这里是要调用的方法名        call.setTargetEndpointAddress(wsdlUrl);// 设置调用的wsdl        String val = (String) call.invoke(new Object[] { "My Michael Sun" });        System.out.println("这是webservice服务器返回的信息:" + val);    }}

       5.客户端Dynamic Proxy方式 实现如下:
      Java代码

    • public interface SayHelloInterface extends java.rmi.Remote {   
    •   
    •     public String sayMsg(String name) throws java.rmi.RemoteException;   
    •   
    • }  


    public interface SayHelloInterface extends java.rmi.Remote {    public String sayMsg(String name) throws java.rmi.RemoteException;}
    Java代码

    • import java.net.URL;   
    •   
    • import javax.xml.namespace.QName;   
    • import javax.xml.rpc.Service;   
    • import javax.xml.rpc.ServiceFactory;   
    •   
    • /**
    • * test Dynamic Proxy client
    • * @author Michael sun
    • */  
    • public class TestProxyClient {   
    •   
    •     /**
    •      * @param args
    •      * @throws Exception
    •      */  
    •     public static void main(String[] args) throws Exception {   
    •         String wsdlname = "http://localhost:8088/axis/SayHello.jws?wsdl";   
    •         // 服务路径   
    •         String namespaceUrl = "http://localhost:8088/axis/SayHello.jws";   
    •         // 服务名   
    •         String serviceName = "SayHelloService";   
    •         // 服务   
    •         String portName = "SayHello";   
    •         // 创建代理对像   
    •         ServiceFactory service = ServiceFactory.newInstance();   
    •         // 创建远程服务   
    •         Service s = service.createService(new URL(wsdlname), new QName(   
    •                 namespaceUrl, serviceName));   
    •   
    •         SayHelloInterface proxy = (SayHelloInterface) s.getPort(new QName(   
    •                 namespaceUrl, portName), SayHelloInterface.class);   
    •         System.out.println(proxy.sayMsg("Blue boy!"));   
    •     }   
    • }  


    import java.net.URL;import javax.xml.namespace.QName;import javax.xml.rpc.Service;import javax.xml.rpc.ServiceFactory;/** * test Dynamic Proxy client * @author Michael sun */public class TestProxyClient {    /**     * @param args     * @throws Exception     */    public static void main(String[] args) throws Exception {        String wsdlname = "http://localhost:8088/axis/SayHello.jws?wsdl";        // 服务路径        String namespaceUrl = "http://localhost:8088/axis/SayHello.jws";        // 服务名        String serviceName = "SayHelloService";        // 服务        String portName = "SayHello";        // 创建代理对像        ServiceFactory service = ServiceFactory.newInstance();        // 创建远程服务        Service s = service.createService(new URL(wsdlname), new QName(                namespaceUrl, serviceName));        SayHelloInterface proxy = (SayHelloInterface) s.getPort(new QName(                namespaceUrl, portName), SayHelloInterface.class);        System.out.println(proxy.sayMsg("Blue boy!"));    }}

    二、WSDD(Web Service Deployment Descriptor)文件发布Web Service
        1.自定义参数bean和server的代码如下:
        Java代码

    • package wsaxis.bean;   
    •   
    • /**
    • * bean
    • * @author Michael sun
    • */  
    • public class UserBean {   
    •   
    •     private String userName;   
    •   
    •     private Integer age;   
    •   
    •     /**
    •      * @return the userName
    •      */  
    •     public String getUserName() {   
    •         return userName;   
    •     }   
    •   
    •     /**
    •      * @return the age
    •      */  
    •     public Integer getAge() {   
    •         return age;   
    •     }   
    •   
    •     /**
    •      * @param pUserName the userName to set
    •      */  
    •     public void setUserName(String pUserName) {   
    •         userName = pUserName;   
    •     }   
    •   
    •     /**
    •      * @param pAge the age to set
    •      */  
    •     public void setAge(Integer pAge) {   
    •         age = pAge;   
    •     }   
    •   
    • }  


    package wsaxis.bean;/** * bean * @author Michael sun */public class UserBean {    private String userName;    private Integer age;    /**     * @return the userName     */    public String getUserName() {        return userName;    }    /**     * @return the age     */    public Integer getAge() {        return age;    }    /**     * @param pUserName the userName to set     */    public void setUserName(String pUserName) {        userName = pUserName;    }    /**     * @param pAge the age to set     */    public void setAge(Integer pAge) {        age = pAge;    }}
    Java代码

    • package wsaxis;   
    •   
    • import wsaxis.bean.UserBean;   
    •   
    • /**
    • * axis server
    • * @author Michael sun
    • */  
    • public class MessageService {   
    •   
    •     /**
    •      * getBeanStr
    •      * @param bean
    •      * @return String
    •      */  
    •     public String getBeanStr(UserBean bean) {   
    •         return "You Name:" + bean.getUserName() + " , You Age:" + bean.getAge();   
    •     }   
    •   
    •     /**
    •      * checkUser
    •      * @param bean
    •      * @return String
    •      */  
    •     public String checkUser(UserBean bean) {   
    •         if ("Michael".equals(bean.getUserName())) {   
    •             return "Michael welcome to axis ";   
    •         } else {   
    •             return bean.getUserName() + " can't access this ws";   
    •         }   
    •   
    •     }   
    •   
    • }  


    package wsaxis;import wsaxis.bean.UserBean;/** * axis server * @author Michael sun */public class MessageService {    /**     * getBeanStr     * @param bean     * @return String     */    public String getBeanStr(UserBean bean) {        return "You Name:" + bean.getUserName() + " , You Age:" + bean.getAge();    }    /**     * checkUser     * @param bean     * @return String     */    public String checkUser(UserBean bean) {        if ("Michael".equals(bean.getUserName())) {            return "Michael welcome to axis ";        } else {            return bean.getUserName() + " can't access this ws";        }    }}
        2.deploy.wsdd和undeploy.wsdd文件的编写如下:
        WSDD文件描述可参见:http://www.oio.de/axis-wsdd/
    Xml代码

    • <deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"   
    •     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  
    •   <service name="MessageService" provider="java:RPC" style="rpc" use="encoded">  
    •     <parameter name="className" value="wsaxis.MessageService"/>  
    •     <parameter name="allowedMethods" value="*"/>  
    •         <typeMapping xmlns:ns1="http://wsaxis.michael.com" qname="ns1:userBean"   
    •             type="java:wsaxis.bean.UserBean"  
    •             serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"  
    •             deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"  
    •         encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"  
    •        />  
    •   </service>  
    • </deployment>  


    <deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/"     xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  <service name="MessageService" provider="java:RPC" style="rpc" use="encoded">    <parameter name="className" value="wsaxis.MessageService"/>    <parameter name="allowedMethods" value="*"/>                 <typeMapping xmlns:ns1="http://wsaxis.michael.com" qname="ns1:userBean"                          type="java:wsaxis.bean.UserBean"                         serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"                         deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"            encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"           />  </service></deployment>
    Xml代码

    • <undeployment  
    •     xmlns="http://xml.apache.org/axis/wsdd/">  
    •   <!-- Services from MessageService WSDL service -->  
    •   <service name="MessageService"/>  
    • </undeployment>  


    <undeployment    xmlns="http://xml.apache.org/axis/wsdd/">  <!-- Services from MessageService WSDL service -->  <service name="MessageService"/></undeployment>
       3.将上面写好的两个类的class文件复制到%tomcat_home%/axis/WEB-INF/class/目录下,完整的目录结构复制过来,然后在把两个wsdd文件复制到%tomcat_home%/axis/WEB-INF/目录下,打开控制台进入%tomcat_home%/axis/WEB-INF/目录下:
    >java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 8082 deploy.wsdd
        -s参数指定了AxisServlet所在的应用程序路径
    >java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 8082 -s /axis/servlet/AxisServlet deploy.wsdd
       -l参数指定目标应用的URL
    >java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient –lhttp://localhost:8082/axis/services/MessageService deploy.wsdd

    这个命令就是发布这个服务,发布成功后在控制台下会有提示:
    Processing file deploy.wsdd
    <Admin>Done processing</Admin>
    同时发布后会在%tomcat_home%/axis/目录下,多了一个server-config.wsdd文件.
    在浏览器输入:http://localhost:8082/axis/services/MessageService会看到下图

      5.client的生成方法:
      打开控制台进入%tomcat_home%/axis/WEB-INF/目录下:
    >java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java -p client http://localhost:8082/axis/services/MessageService?wsdl
    会在当前的目录下生成client文件夹,这个目录里文件就是客户端源码。

      6.通过WSDD文件卸载发布的webservice:
      打开控制台进入%tomcat_home%/axis/WEB-INF/目录下:
    >java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient undeploy.wsdd
    如果不是默认8080端口需要加上参数-p:
    >java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -p 8082 undeploy.wsdd

    PS:java -Djava.ext.dirs=lib 也可以用java -cp "lib\*"
      7.客户端Stubs方式实现如下:
    Java代码

    • import java.net.URL;   
    •   
    • import javax.xml.namespace.QName;   
    •   
    • import org.apache.axis.client.Call;   
    • import org.apache.axis.client.Service;   
    • import org.apache.axis.encoding.ser.BeanDeserializerFactory;   
    • import org.apache.axis.encoding.ser.BeanSerializerFactory;   
    •   
    • import stubclient.UserBean;   
    •   
    • /**
    • * test stub client
    • * @author Michael sun
    • */  
    • public class TestStubClient {   
    •   
    •     /**
    •      * @param args
    •      * @throws Exception
    •      */  
    •     public static void main(String[] args) throws Exception {   
    •         QName qname = new QName("http://wsaxis.michael.com", "user");   
    •         Service s = new Service();   
    •         Call call = (Call) s.createCall();   
    •         // 注册这个bean为可序列化的.传递参数   
    •         call.registerTypeMapping(UserBean.class, qname,   
    •                 new BeanSerializerFactory(UserBean.class, qname),   
    •                 new BeanDeserializerFactory(UserBean.class, qname));   
    •         // 设置一下调用方法名.   
    •         call.setOperationName("getBeanStr");   
    •   
    •         // 设置一下这个服务的绝对路径.   
    •         call.setTargetEndpointAddress(new URL(   
    •                 "http://localhost:8082/axis/services/MessageService?wsdl"));   
    •         // 实例化一个UserBean,这个UserBean是生成client的UserBean   
    •         UserBean u = new UserBean();   
    •         u.setAge(28);   
    •         u.setUserName("Michael");   
    •         // 通知方法,并返回结果   
    •         String str = (String) call.invoke(new Object[] { u });   
    •   
    •         System.out.println("web service 返回信息:" + str);   
    •   
    •     }   
    •   
    • }  


    原文:http://sjsky.iteye.com/blog/683916

    跟着做了一遍,感觉又学到很多东西,后面java客户端没做,改用c#程序来调用java服务,实现了不同平台间的相互通信。

    工欲善其事,必先利其器。
  • 相关阅读:
    java作业利用递归解决问题
    java课堂测试2(两种方式)
    java模拟验证码生成
    java选做猜数字
    java课堂动手测试
    java课堂作业,求多参数的和
    《大道至简》第一章伪代码形式读后感
    《大道至简》读后感
    关于《大道至简》第八章的收获
    [JLOI2012]树 倍增优化
  • 原文地址:https://www.cnblogs.com/zhangzhu/p/2495018.html
Copyright © 2011-2022 走看看