XFire WebService开发快速起步

news/2024/7/7 21:36:49
 
环境:
XFire-1.2.6
JDK1.5
MyEclipse 6.5
Tomcat-5.5.27
Windows XP Professional简体中文版
 
软件下载地址:
[url]http://repository.codehaus.org/org/codehaus/xfire/xfire-distribution/1.2.6/xfire-distribution-1.2.6.zip[/url]
[url]http://apache.mirror.phpchina.com/tomcat/tomcat-5/v5.5.27/bin/apache-tomcat-5.5.27.zip[/url]
 
有关WebService的概念、原理、数据发现、描述、绑定等过程、方式也不说了。这里就只关注如何快速开发出来一个通用的、易懂的Hello World例子。
 
以下是开发步骤:
 
1、创建工程
 
打开MyEclipse 6.5,新建一个WebService工程。如下图
 
 
然后一路next,直到完成。
 
创建完成后,打开生成的web.xml文件,可以看到,XFire已经配置好了。
<? xml version ="1.0" encoding ="UTF-8" ?>
< web-app xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" version ="2.5" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee     [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]" >
     < servlet >
         < servlet-name >XFireServlet </ servlet-name >
         < servlet-class >org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >
         < load-on-startup >0 </ load-on-startup >
     </ servlet >
     < servlet-mapping >
         < servlet-name >XFireServlet </ servlet-name >
         < url-pattern >/services/* </ url-pattern >
     </ servlet-mapping >
     < welcome-file-list >
         < welcome-file >index.jsp </ welcome-file >
     </ welcome-file-list >
</ web-app >
 
2、创建WebService服务
 
创建两个个包“wstest.server”和“wstest.client”,用来保存服务端和客户端程序。然后开始创建服务端程序,如下图
 
 
 
完成后,生成了一个Service的配置services.xml:
 
<? xml version ="1.0" encoding ="UTF-8" ?>
< beans xmlns ="http://xfire.codehaus.org/config/1.0" >

   < service >
     < name >MyService </ name >
     < serviceClass >wstest.server.IMyService </ serviceClass >
     < implementationClass >
      wstest.server.MyServiceImpl
     </ implementationClass >
     < style >wrapped </ style >
     < use >literal </ use >
     < scope >application </ scope >
   </ service >
</ beans >
 
也生成了接口和默认实现,改写后如下:
package wstest.server;
//Generated by MyEclipse

public interface IMyService {
    
   public String sayHello(String user);
    
}
 
package wstest.server;
//Generated by MyEclipse

public class MyServiceImpl implements IMyService {
    
   public String sayHello(String user) {
     return "您好,"+user;
  }
    
}
 
至此,服务端代码已经完成。
 
3、测试服务端代码
 
测试依赖与Servlet容器Tomcat,需要将做好的服务端打包部署到tomcat上,然后启动。才可以进行测试。假设你已经配置了Tomcat服务器,并完成了WebService服务端的部署。那么,现在就启动Tomcat,然后:
 
 
 
输入访问地址: [url]http://localhost:8080/xfire126Demo/services/MyService?wsdl[/url]  ,然后go一把!
 
 
 
这样,出现上上面的结果,表明测试成功了。
 
4、生成客户端代码
 
 
 
 
 
很郁闷,这个生成的客户端代码一部分跑到服务端的包里面了。真是垃圾,rubbish!!!
 
但是,这就是MyEclipse的功能,我改变不了。
 
5、客户端测试
 
下面就耐心看怎么用这个客户端代码。
打开生成的代码如下:

package wstest.client;

import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;

public class MyServiceClient {

         private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
         private HashMap endpoints = new HashMap();
         private Service service0;

         public MyServiceClient() {
                create0();
                Endpoint MyServicePortTypeLocalEndpointEP = service0 .addEndpoint( new QName( "http://server.wstest", "MyServicePortTypeLocalEndpoint"), new QName("http://server.wstest", "MyServicePortTypeLocalBinding"), "xfire.local://MyService");
                endpoints.put(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint"), MyServicePortTypeLocalEndpointEP);
                Endpoint MyServiceHttpPortEP = service0 .addEndpoint(new QName("http://server.wstest", "MyServiceHttpPort"), new QName("http://server.wstest", "MyServiceHttpBinding"), "http://localhost:8080/xfire126Demo/services/MyService");
                endpoints.put(new QName("http://server.wstest", "MyServiceHttpPort"), MyServiceHttpPortEP);
        }

        public Object getEndpoint(Endpoint endpoint) {
                try {
                        return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
                } catch (MalformedURLException e) {
                        throw new XFireRuntimeException("Invalid URL", e);
                }
        }

        public Object getEndpoint(QName name) {
                Endpoint endpoint = ((Endpoint) endpoints.get((name)));
                if ((endpoint) == null) {
                        throw new IllegalStateException("No such endpoint!");
                }
                return getEndpoint((endpoint));
        }

        public Collection getEndpoints() {
                return endpoints.values();
        }

        private void create0() {
                TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
                HashMap props = new HashMap();
                props.put("annotations.allow.interface", true);
                AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
                asf.setBindingCreationEnabled(false);
                service0 = asf.create((wstest.client.MyServicePortType.class), props);
                {
                        AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server.wstest", "MyServiceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
                }
                {
                        AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server.wstest", "MyServicePortTypeLocalBinding"), "urn:xfire:transport:local");
                }
        }

        public MyServicePortType getMyServicePortTypeLocalEndpoint() {
                return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint")));
        }

        public MyServicePortType getMyServicePortTypeLocalEndpoint(String url) {
                MyServicePortType var = getMyServicePortTypeLocalEndpoint();
                org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
                return var;
        }

        public MyServicePortType getMyServiceHttpPort() {
                return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest", "MyServiceHttpPort")));
        }

        public MyServicePortType getMyServiceHttpPort(String url) {
                MyServicePortType var = getMyServiceHttpPort();
                org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
                return var;
        }

        public static void main(String[] args) {
                

                MyServiceClient client = new MyServiceClient();
                
    //create a default service endpoint
                MyServicePortType service = client.getMyServiceHttpPort();
                
    //TODO: Add custom client code here
                    //
                    //service.yourServiceOperationHere();
                
    System.out.println("test client completed");
                    System.exit(0);
        }

}
 
看得很晕,不知道啥意思,但是从“TODO”标记处,我知道了:
     //TODO: Add custom client code here
                     //
                     //service.yourServiceOperationHere();
 
现在就在这里添加测试代码吧:
     //TODO: Add custom client code here
                     //
                     //service.yourServiceOperationHere();
                String helloString = service.sayHello( "熔岩");
                System.out.println(helloString);
 
添加了很傻蛋的两行代码后,就可以运行起来看看测试代码了。
 
运行结果如下:
您好,熔岩
test client completed
 
终于可以松一口气了。完整的例子跑起来了。
 
6、总结
 
总感觉这个开发过程不爽,其实有更好的工具和开发方式:
 
WebService的编写,比较麻烦的是客户端代码,客户端代码依靠人工去写基本上是不可能的,除非你愿意付出惊人的时间和精力,既便如此也得不偿失。
 
MyEclipse的客户端开发太差,主要是生成的客户端代码混乱,解决办法是将客户端的编写放到单独一个工程里面来做。
 
其实,只要服务端编写好了,就完全可以用别的方式根据wsdl的url去生成客户端代码,在这里不得不将一个强大的工具IDEA8推荐出来,IDEA8自带WebService开发工具,插件非常强大,易用。在后面的篇幅中,我会做专门介绍,敬请关注。
 
当然,MyEclipse也并非一无是处,MyEclipse的服务端调试工具就很不错,很喜欢。提高了开发效率,这也是MyEclipse的过人之处。

http://www.niftyadmin.cn/n/3484854.html

相关文章

python如何求一个列表里所有数字的和_在Python中汇总一个数字列表

我有一个数字列表&#xff0c;如[1,2,3,4,5...] &#xff0c;我想计算(12)/2和第二&#xff0c; (23)/2和第三&#xff0c; (34)/2 &#xff0c;依此类推。 我怎样才能做到这一点&#xff1f;我想将第一个数字与第二个数字相加并除以2&#xff0c;然后将第二个数字与第三个相加…

vue知识掌握(一)

vue项目的搭建 本文章主要讲述vue项目的搭建&#xff0c;在搭建vue项目前需要准备一些材料。 1.node安装 npm&#xff1a;在你安装node的时候一般node已经自带了npm&#xff0c;所以忽略。 webpack&#xff1a;npm install webpack -g 检验安装是否成功的办法&#xff1a; 检验…

java 包装类方法总结_Java基础教程:Java包装类概念及应用

概述Java提供了两个类型系统&#xff0c;基本类型与引用类型&#xff0c;使用基本类型在于效率&#xff0c;然而很多情况&#xff0c;会创建对象使用&#xff0c;因为对象可以做更多的功能&#xff0c;如果想要我们的基本类型像对象一样操作&#xff0c;就可以使用基本类型对应…

Spring Bean 初始化过程

Life类 Java代码 package com.open.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory…

java处理分析经济学_什么?程序员还要了解经济学?!

【编者按】本文作者为常年混迹于 SitePoint JavaScript 论坛的 Web 开发者 James Hibbard&#xff0c;主要介绍针对开发新手提出了一些学习建议。文章系国内 ITOM 管理平台 OneAPM 编译呈现。在近期的一次采访中&#xff0c; StackOverflow联合创始人Joel Spolsky建议&#xff…

密码学在区块链中能做什么

2019独角兽企业重金招聘Python工程师标准>>> 介绍 本篇会讲述密码学在区块链中的具体的使用&#xff0c;从而理解为什么区块链可以防止消息被篡改&#xff0c;怎么进行数字身份认证。比特币中是如何通过多重签名实现多个人共同管理某个账户的比特币交易。 布隆过滤器…

php隐藏域,php隐藏域是什么

php隐藏域是用来收集或发送信息的不可见元素&#xff0c;对于页面的访问者来说&#xff0c;隐藏域是不可见的。当表单被提交时&#xff0c;隐藏域便会将信息用设置时定义的名称和值发送到服务器上。隐藏域&#xff1a;隐藏域是用来收集或发送信息的不可见元素&#xff0c;对于网…

SpringMVC+FreeMarker实现半自动静态化

感谢imyourgod的原贴http://topic.csdn.net/u/20090613/12/30302a5d-33c0-4ce4-b9e3-cd503dbafac1.html这里对freemarker的代码进行了修改&#xff0c;效果:1,请求.do的URL时直接生成对应的.htm文件,并将请求转发到该htm文件2,自由控制某个页面是否需要静态化原理&#xff1a;对…