• axis2 webService开发指南(2)


    1  Axis2的简单WebService示例

    1.1 新建一个web工程,创建一个类Greeting,用于当作webservice服务

    代码如下:

    package amyservices;
    
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    
    import Com.WebService.Model.Person;
    
    public class Greeting {
        public String getGreeting(String name){
            return "你好:" + name;
        }
        
        public int getPrice()
        {
            return new java.util.Random().nextInt(1000);
        }
        
        public List<Person> GetPersonList()
        {
            List<Person> list = new ArrayList<Person>();
            Person model = new Person();
            model.setKeyID("1214324234234234324");
            model.setName("張三");
            Calendar birth = Calendar.getInstance();
            birth.set(1990, 11, 22);
            model.setBirth(birth);
            list.add(model);
            model = new Person();
            model.setKeyID("1214324234234234324");
            model.setName("李四");
            birth = Calendar.getInstance();
            birth.set(1990, 11, 22);
            model.setBirth(birth);
            System.out.println(birth.toString());
            list.add(model);
            for(Person item : list)
            {
                System.out.println(item.getName());
            }
            
            return list;
        }
        
        public String PrintNow()
        {
            Calendar cale = Calendar.getInstance();
            Date tasktime=cale.getTime();  
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            System.out.println(df.format(tasktime));
            return  df.format(tasktime);
        }
    }
    View Code

    .classpath文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
            <attributes>
                <attribute name="owner.project.facets" value="java"/>
            </attributes>
        </classpathentry>
        <classpathentry kind="con" path="com.genuitec.runtime.library/com.genuitec.generic_7.0">
            <attributes>
                <attribute name="owner.project.facets" value="jst.web"/>
            </attributes>
        </classpathentry>
        <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
        <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
        <classpathentry kind="con" path="com.genuitec.runtime.library/com.genuitec.jstl_1.2.2">
            <attributes>
                <attribute name="org.eclipse.jst.component.dependency" value="WEB-INF/lib"/>
                <attribute name="owner.project.facets" value="jst.web.jstl"/>
            </attributes>
        </classpathentry>
        <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
    </classpath>
    View Code

    1.2 点击File-->New-->other,选择Axis2 Service Archiver

    配置Class File Location,配置路径到工程的WEB-INFclassess目录下,同时将下图的复选框勾选去掉,继续根据向导完成创建。

    创建成功,在axis2的工程目录下,多了我们刚配置好的文件

    输入网址http://localhost:8080/axis2/services/listServices,会发现增加了我们刚创建的服务。

    2 创建客户端

    2.1 创建一个java项目

    2.2 新建一个WsClient测试类

    代码如下:

    package com.amy.client;
    
    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;
    
    public class WsClient {
        private RPCServiceClient serviceClient;  
        private Options options;
        private EndpointReference targetERP;
        
        public WsClient(String endpoint) throws AxisFault{
            this.serviceClient = new RPCServiceClient();
            this.options = this.serviceClient.getOptions();
            this.targetERP = new EndpointReference(endpoint);
            this.options.setTo(this.targetERP);
        }
        
        public Object[] invokeOp(String targetNamespace, String opName,Object[] opArgs, Class<?>[]opReturnType) throws AxisFault, ClassNotFoundException{
            QName opQName = new QName(targetNamespace, opName);
            return this.serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
        }
        
        public static void main(String args[])
        {
            final String endpointReference = "http://localhost:8080/axis2/services/WebServiceTest1";
            final String targetNamesPace = "http://amyservices";
            try {
                WsClient client = new WsClient(endpointReference);
                String opName = "GetPersonList";
                Object[] opArgs = new Object[]{};
                Class<?>[] opReturnType = new Class[]{Person.class};
                Object[] response = client.invokeOp(targetNamesPace, opName, opArgs, opReturnType);
                if (response != null && response.length > 0)
                {
                    System.out.println(response.length);
                    for(Object obj : response)
                    {
                        Person model = (Person)obj;
                        System.out.println(model.getName());
                    }
                }
                 
            } catch (AxisFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             
        }
    }
    View Code

    2.3 要引用的jar包有

      .classpath文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
        <classpathentry kind="lib" path="lib/axiom-impl-1.2.13.jar"/>
        <classpathentry kind="lib" path="lib/axis2-adb-1.6.2.jar"/>
        <classpathentry kind="lib" path="lib/axis2-kernel-1.6.2.jar" sourcepath="C:/Users/zhujinrong/.m2/repository/org/apache/axis2/axis2-kernel/1.6.2/axis2-kernel-1.6.2-sources.jar"/>
        <classpathentry kind="lib" path="lib/axis2-transport-http-1.6.2.jar"/>
        <classpathentry kind="lib" path="lib/axis2-transport-local-1.6.2.jar"/>
        <classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
        <classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
        <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
        <classpathentry kind="lib" path="lib/httpcore-4.0.jar"/>
        <classpathentry kind="lib" path="lib/neethi-3.0.2.jar"/>
        <classpathentry kind="lib" path="lib/wsdl4j-1.6.2.jar"/>
        <classpathentry kind="lib" path="lib/XmlSchema-1.4.7.jar"/>
        <classpathentry kind="lib" path="lib/axiom-api-1.2.13.jar"/>
        <classpathentry kind="lib" path="lib/activation-1.1.jar"/>
        <classpathentry kind="lib" path="lib/axis2-adb-codegen-1.6.2.jar"/>
        <classpathentry kind="lib" path="lib/axis2-java2wsdl-1.6.2.jar"/>
        <classpathentry kind="lib" path="lib/mail-1.4.jar"/>
        <classpathentry kind="lib" path="lib/woden-api-1.0M9.jar"/>
        <classpathentry kind="lib" path="lib/woden-impl-commons-1.0M9.jar"/>
        <classpathentry kind="lib" path="lib/woden-impl-dom-1.0M9.jar"/>
        <classpathentry kind="lib" path="lib/wstx-asl-3.2.9.jar"/>
        <classpathentry kind="lib" path="lib/xmlbeans-2.3.0.jar"/>
        <classpathentry kind="output" path="bin"/>
    </classpath>
    View Code

    3 执行结果

    3.1 查看服务器启动

    3.2 客户端

    3.3 服务端情况

    3.4 直接查询

    作者:BestNow
    出处:http://www.cnblogs.com/BestNow/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    speeding up your web site 前端性能优化
    数据结构与算法学习链表
    SQL Server 无法生成 FRunCM 线程
    NoSql的优缺点
    Vs2012中开发Node.js应用
    利用GNUstep在windows下编写objectc
    Hadoop HDFS学习总结
    HBase Thrift 接口的一些使用问题及相关注意事项
    .NET深入解析LINQ框架
    【翻译】ASP.NET Web API是什么?
  • 原文地址:https://www.cnblogs.com/tianxue/p/4135979.html
Copyright © 2020-2023  润新知