zoukankan      html  css  js  c++  java
  • [转]Webservice,基于Axis的最佳实践

    转自:http://blog.csdn.net/programeyonger/archive/2008/11/04/3218088.aspx

    Axis原理探讨与实战演练

    Axis是支持Webservicejava平台的框架,和Xfire是一样的。不过使用起来好像比Xfire要麻烦很多。

    它有三种方式支持Webservice

    1.  Dynamic Invocation Interface ( DII)  

    2.  Dynamic Proxy,

    3.  Stubs,

     

    三种方式的介绍:

     

    <!--[if !supportLists]-->1.  <!--[endif]--> DII是最简单的方式,编写一个java文件比如说MyService,记住不需要编译成class文件,因为Axis会帮您编译,你需要做的是拷贝这个java文件到一个目录下,我们是拷贝MyServic.javaaxis_example/jws目录下,更改文件名为MyService.jws,然后编写一个Client程序,直接访问之,

    import org.apache.axis.client.Call;  

    import org.apache.axis.client.Service;  

    import javax.xml.namespace.QName;  

    import javax.xml.rpc.ServiceFactory;  

    import java.net.URL;  

    public class Client {  

        public static void main(String [] args) throws Exception {  

            // 指出service所在URL  

            String endpoint = "http://localhost:8080/axis_example/jws/MyService.jws";  

            // 创建一个服务(service)调用(call)  

            Service service = new Service();  

            Call call = (Call) service.createCall();// 通过service创建call对象

            // 设置service所在URL  

            call.setTargetEndpointAddress(new java.net.URL(endpoint));  

            // 方法名(processService)MyService.java方法名保持一致  

            call.setOperationName("processService");  

            // Object 数组封装了参数,参数为"This is Test!",调用processService(String arg)  

            String ret = (String) call.invoke(new Object[]{"This is Test!"});  

            System.out.println(ret);  

        }  

    }  

     

    axis_example 工程放入tomcat/webapps启动tomcat  

    编译Client.java,运行其中的main方法进行测试,可以看到屏幕打印出:"This is Test!",可以看到axis_example/WEB-INF目录下生jwsClasses/jws/MyService.class文件——axis会根据你访问时的endpoint,自动编译其中的*.jws文件,并置于生成的jwsClasses相应目录下。

     

    1 在上面的 new Object[]{"This is Test!"} 语句中,只传递了一个参数。如果MyServic.java  

    processService(String arg) 改写为  

    processService(String arg,String arg2)  

    你可以通过new Object[]{"test","test2"}传递多个参数。  

    2 启动tomcat 后控制台出现下面警告:  

    - Unable to find required classes (javax.activation.DataHandler and javax.mail.i  nternet.MimeMultipart). Attachment support is disabled.  

    这是因为缺少activation.jarmail.jar(本文中的实例可以忽略此警告)。  

    activation.jar (目前版本为1.1)下载地址  

    http://java.sun.com/products/javabeans/jaf/downloads/index.html  

    mail.jar (目前版本为1.4)下载地址  

    http://java.sun.com/products/javamail/downloads/  

     

    <!--[if !supportLists]-->2.  <!--[endif]-->Dynamic Proxy)动态代理

      axis_example /src 新建一MyServiceInterface.java文件,内容为:  

    import java.rmi.Remote;  

    import java.rmi.RemoteException;  

    public interface MyServiceInterface extends Remote {  

        public String processService(String arg) throws RemoteException;  

    }
    修改axis_example /src MyServic.java文件,把类声明  

    public class MyService  

    改为  

    public class MyService implements MyServiceInterface  

    无需编译,拷贝MyServic.javaaxis_example/jws目录下,更改文件名为MyService.jws



    客户端的开发, 更改axis_example/src/Client.java中的main方法,内容为:  

        public static void main(String [] args) throws Exception {  

            String wsdlUrl = "http://localhost:8080/axis_example/jws/MyService.jws?wsdl";  

            String nameSpaceUri = "http://localhost:8080/axis_example/jws/MyService.jws";  

            String serviceName = "MyServiceService";  

            ServiceFactory serviceFactory = ServiceFactory.newInstance();  

            javax.xml.rpc.Service service = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName));  

            MyServiceInterface proxy = (MyServiceInterface)  

                    service.getPort(new QName(nameSpaceUri, portName), MyServiceInterface.class);  

            System.out.println("This is " + proxy.processService("Dynamic Proxy test!"));  

    }
    <!--[if !supportLineBreakNewLine]-->
    <!--[endif]-->

    axis_example/src工程下的放入tomcat/webapps下面,启动tomcat

    编译Client,运行其中的main方法,你会看到程序执行的结果。

     

    Stub方式(推荐的方式)。

     

    axis_example/src下新建一MyServic.java文件,内容为:  

    public class MyService {  

        public String processService(String arg){  

            return arg;  

        }  

    }  

     

    编译 MyServic.java

    在新建一deploy.wsdd(可参考 axis-bin-1_4.zip \axis-1_4\samples 中的deploy.wsdd----只要保证在classpath下就可以了)文件,内容为:  

    <deployment xmlns="http://xml.apache.org/axis/wsdd/"  

                xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  

     <service name=" MyService" provider="java:RPC">  

      <parameter name="className" value=" com.cjl.test.MyService"/>  

      <parameter name="allowedMethods" value="processService"/>  

     </service>  

    </deployment>  

     

    启动tomcat

    采用这种方式,先要保证axis所需要的jar在你的环境的classpath下。我是采用把所有的jarclasspath = D:\eCustoms\j2sdk1.4.2_07\lib;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\activation.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\axis.jar;axis-ant.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\commons-discovery-0.2.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\jaxrpc.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\saaj.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\tools.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\wsdl4j-1.5.1.jar;D:\Alex\workspace\mywebservice\WebRoot\WEB-INF\lib\commons-logging-1.0.4.jar

    axis_example/WEB-INF目录下执行:

    java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis_example/servlet/AxisServlet deploy.wsdd

    执行后可看到在axis_example/WEB-INF目录下生成server-config.wsdd文件。

     

    从文件名称上就能看到大概的意思,是一个配置文件,这个文件就是一个Webservice工程的核心。

     

    启动tomcatAxis框架会自动加载这个server-config.wsdd文件(相当于struts-config.xml,假如你使用struts框架),然后你就可以通过在浏览器的地址栏输入:

    http://localhost:8080/axis_example/services/MyService?wsdl,

     

    假如能看到xml文件了,表示这个Webservice的服务器已经部署完成,可以接受它的客户端的访问咯。

    你可以编写客户端程序来访问这个Webservice了。

    String endpoint = "http://localhost:8080/mywebservice/services/MyService"

            // 创建一个服务(service)调用(call) 

            Service service = new Service(); 

            Call call = (Call) service.createCall();// 通过service创建call对象 

            // 设置service所在URL 

            call.setTargetEndpointAddress(new java.net.URL(endpoint)); 

            // 方法名(processService)MyService.java方法名保持一致 

          

     

            call.setOperationName("processService"); 

            // Object 数组封装了参数,参数为"This is Test!",调用processService(String arg) 

            String ret = (String) call.invoke(new Object[]{"This is Test!"}); 

            System.out.println(ret); 

    假如你的service传递的方法是用户自定义的对象类型的话,需要增加一个映射配置

    1.客户端程序里。

    QName qn = new QName(endpoint,"MessageInfo");

           call.registerTypeMapping(MessageInfo.class, qn,

               new BeanSerializerFactory(MessageInfo.class, qn),

               new BeanDeserializerFactory(MessageInfo.class, qn));

     

    2 服务器端:

    修改Service-config.wsdd文件:

    <service name="MyService" provider="java:RPC">

      <parameter name="allowedMethods" value="processService"/>

      <parameter name="className" value="com.eci.ciq.webservice.test.MyService"/>

      <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns1:MessageInfo" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.eci.ciq.webservice.test.MessageInfo" xmlns:ns1="urn:MyService"/>

     </service>

    表色加粗的部分就是添加项。

    那样就可以。

  • 相关阅读:
    团队冲刺2---个人工作总结一(5.25)
    第十二周学习进度
    课堂作业——找水王
    个人冲刺07
    第十五周学习进度情况
    构建之法阅读笔记06
    构建之法阅读笔记05
    第十四周学习进度情况
    个人冲刺06
    个人冲刺05
  • 原文地址:https://www.cnblogs.com/hanxianlong/p/1326817.html
Copyright © 2011-2022 走看看