MYECLIPSE 开发Web Service之详细讲解--第二节

Posted tropica

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYECLIPSE 开发Web Service之详细讲解--第二节相关的知识,希望对你有一定的参考价值。

 第一节中说明了怎么开发webservice,这节主要描述怎么测验和应该注意的内容

一:开发JAVA客户端

上面一节说了,如果你发现下面的内容,那就是发布成功了;

And now... Some Services

CallSoapImpl (wsdl) 这个就是新增加的节点,点wsdl链接,出现XML相关的文件,然后您要做的是点右键,查看源代码。讲内容保存下来,命名CallSoapImpl.wsdl(名称是随便的),这个文件就是我们下面生成客户端需要的。

生成客户端
java org.apache.axis.wsdl.WSDL2Java -p com.xuzr  CallSoapImpl.wsdl

看到了吧,上面就是执行客户端的脚本,com.xuzr  是生成的文件包

然后你到执行脚本的目录下看,会发现多了文件夹,细点,发现,会出现JAVA文件,分别为:

CallSoap.java 

CallSoapImpl_PortType.java

CallSoapImplSoapBindingStub.java

CallSoapLocator.java

注意看CallSoapLocator.java文件,会发现里面有相应的servlet地址,

 public java.util.Iterator getPorts() {
        if (ports == null) {
            ports = new java.util.HashSet();
            //ports.add(new javax.xml.namespace.QName("http://localhost:7001/CallSmsService/services/CallSoapImpl", "CallSoapImpl"));
            ports.add(new javax.xml.namespace.QName("http://10.168.6.68:8383/CallSmsService/services/CallSoapImpl", "CallSoapImpl"));
           
        }

呵呵,这就是我们客户端需要用到的。

二:调用web service

上面的步骤都完成的情况下,那么就需要我们来调用咯,呵呵,其实客户端代码有很多种,比如

        org.codehaus.xfire.service.Service srvcModel = new ObjectServiceFactory().create(CallSoap.class);
        XFireProxyFactory factory =
        new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//     String helloWorldURL =
//        "http://localhost:7001/HelloWorld/services/HelloWorldService";
        String helloWorldURL =
         "http://localhost:7001/CallSoap/services/CallSoapImpl";
     try {
      CallSoap srvc = (CallSoap)factory.create(srvcModel, helloWorldURL);
        String result = srvc.getCallSoapImpl().example(" 中文测验hello world..11");
        System.out.print(result);
     } catch (MalformedURLException e){
           e.printStackTrace();
     }
 }

但是我们为了让初学的人更容易理解,我建议大家用下面的测验代码:

    CallSoap service = new CallSoapLocator();
  try {
      System.out.println(service.getCallSoapImpl().example(bufferdemo.toString()));
   
   
  } catch (ServiceException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

哈哈,简单吧,如果成功,会出现下面的情况,当然,我的调用很简单,就是返回OK,所以就不需要写了,在调用的过程中有N多日志,你不要管,那都是正常的。

15:17:38,313 DEBUG [SOAPPart] Exit: SOAPPart ctor()
15:17:38,325 DEBUG [SOAPPart] Enter: SOAPPart::getAsSOAPEnvelope()
15:17:38,326 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(currForm)
15:17:38,326 DEBUG [SOAPPart] current form is FORM_SOAPENVELOPE
15:17:38,326 DEBUG [RPCProvider] Enter: RPCProvider.processMessage()
15:17:38,326 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(bodyElems00)
15:17:38,326 DEBUG [RPCProvider] There are 1 body elements.
15:17:38,336 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(register00)
15:17:38,337 DEBUG [SerializationContext] register 'ns1' - 'http://soap.linkage.com'
15:17:38,337 DEBUG [NSStack] NSPush (32)
15:17:38,337 DEBUG [NSStack] NSPush (32)
15:17:38,337 DEBUG [SAXOutputter] SAXOutputter.startElement ['http://soap.linkage.com' example]
15:17:38,337 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(startElem00)
15:17:38,337 DEBUG [SerializationContext] Start element [http://soap.linkage.com]:example
15:17:38,347 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(register00)
15:17:38,347 DEBUG [SerializationContext] register 'soapenv' - 'http://schemas.xmlsoap.org/soap/envelope/'
15:17:38,348 DEBUG [NSStack] NSPush (32)
15:17:38,348 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(register00)
15:17:38,348 DEBUG [SerializationContext] register 'soapenc' - 'http://schemas.xmlsoap.org/soap/encoding/'
15:17:38,348 DEBUG [NSStack] NSPush (32)
15:17:38,349 DEBUG [NSStack] NSPush (32)
15:17:38,349 DEBUG [SAXOutputter] SAXOutputter.startElement ['' message]
15:17:38,349 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(startElem00)
15:17:38,349 DEBUG [SerializationContext] Start element []:message
15:17:38,349 DEBUG [ProjectResourceBundle] org.apache.axis.i18n.resource::handleGetObject(register00)
15:17:38,349 DEBUG [SerializationContext] register 'xsi' - 'http://www.w3.org/2001/XMLSchema-instance'
15:17:38,349 DEBUG [NSStack] NSPush (32)

好了,关于用Myeclipse开发WEB Service的就讲到这里咯,我相信大家应该很容易上手的,毕竟需要我们写的代码真的不多,恩,下一节我讲下一个开发Demo,EJB的开发和调试,大家等着吧,欢迎和我交流:tropica@163.com

 

以上是关于MYECLIPSE 开发Web Service之详细讲解--第二节的主要内容,如果未能解决你的问题,请参考以下文章

MYECLIPSE 开发Web Service之详细讲解--第三节

MYECLIPSE 开发Web Service之详细讲解--第二节

在Myeclipse下使用CXF和Spring开发Web Service

MYECLIPSE 开发Web Service之详细讲解--第一节

MyEclipse中创建Web Service项目

myeclipse新建web工程就有个红叉