Web Service实践之——开始XFire

一、Axis与XFire的比较
XFire是与Axis2 并列的新一代WebService平台。之所以并称为新一代,因为它:
1、支持一系列Web Service的新标准--JSR181、WSDL2.0 、JAXB2、WS-Security等;
2、使用Stax解释XML,性能有了质的提高。XFire采用Woodstox 作Stax实现;
3、容易上手,可以方便快速地从pojo发布服务;
4、Spring的结合;
5、灵活的Binding机制,包括默认的Acegis,xmlbeans,jaxb2,castor。

XFire与Axis1性能的比较
1、XFire比Axis1.3快2-6倍
2、XFire的响应时间是Axis1.3的1/2到1/5

XFire与Axis2的比较
虽然XFire与Axis2都是新一代的WebService平台,但是Axis2的开发者太急于推出1.0版本,所以1.0还不是一个稳定的版本,它的开发者宣称1.1版本即将推出,希望1.1版本会是个稳定的版本。在XFire捐献给apache后有人认为Axis2将会灭亡。其实在很多人眼里,Axis2并不是pojo形式,现在也好象XFire比Axis更有市场,也有很多人开始从Axis转向XFire。
据说,XFire确实比Axis2简单很多

通过一个比较矩阵来比较Axis2和CXF变得有现实的意义。这两个项目都开发不够成熟,但是最主要的区别在以下几个方面:
1. CXF支持 WS-Addressing,WS-Policy, WS-RM, WS-Security和WS-I Basic Profile。Axis2不支持WS-Policy,但是承诺在下面的版本支持。
2. CXF可以很好支持Spring。Axis2不能
3. AXIS2支持更广泛的数据并对,如XMLBeans,JiBX,JaxMe和JaxBRI和它自定义的数据绑定ADB。注意JaxME和JaxBRI都还是试验性的。CXF只支持JAXB和Aegis。在CXF2.1
4. Axis2支持多语言-除了Java,他还支持C/C++版本。

[color=red][b]二.开始XFire之旅:[/b][/color]
[b]1.配置XFire运行环境[/b]:
在Tomcat下新建一个Web Applications,命名为stove,然后在其WEB-INF目录下新建一个web.xml文件,文件中输入:

<?xml version="1.0" encoding="GB2312">
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>XFireServlet</servlet-name>
<display-name>XFire Servlet</display-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

其中主要就是引入了XFireServlet,用以处理Web Service请求,并且负责提供Web Service的WSDL,如果你发布了一个名为BookService的WebService,则可以通过网址:
[b][color=red]http://<服务器>[:端口]/<webapp名>/services/BookService来访问这个WebService,并且通过地址:
http://<服务器>[:端口]/<webapp名>/services/BookService?WSDL 来得到这个WebService的WSDL信息。[/color][/b]

[b][color=red]2、开发最简单的WebService[/color][/b]
在WEB-INF目录下新建src文件夹,然后再建一个package:
cn.com.pansky.webservice.xfire.study
,在这个包下面新建一个接口:

package cn.com.pansky.webservice.xfire.study;
public interface SayHiService{
public String sayHi(String name);
}

[color=indigo]这个接口是告诉服务器你的WebService哪些方法可以被用户调用的。[/color]下面我们再来写一个SayHiService的实现类,以完成业务逻辑:

package cn.com.pansky.webservice.xfire.study;
public class SayHiServiceImpl implements SayHiService{
public String sayHi(String name){
if(name==null){
return "连名字也不肯告诉我吗?";
}
return name+", 你吃了吗?没吃回家吃去吧。";
}
public String 不告诉你(){
return "我的名字不告诉你!";
}
}


这个类实现了sayHi方法,该方法是可以通过WebService调用访问到的。另外还实现了一个方法“不告诉你”,该方法因为没有在接口SayHiService中定义,所以不能被WebService调用到。
这个例子足够简单吧,就跟我们刚学Java时写的"Hello world"没什么两样。
到这里为止,我们做的跟平时的Java开发没啥区别,该如何来发布WebService呢?
[color=red][b]3、把JAVA类发布为WebService:[/b][/color]
在WEB-INF/classes目录下新建文件夹:META-INF/xfire,然后在该文件夹下新建一个XML文件:services.xml,文件内容如下:

<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>SayHiService</name>
<namespace>http://cn.com.pansky/SayHiService</namespace>
<serviceClass>cn.com.pansky.webservice.xfire.study.SayHiService</serviceClass>
<implementationClass>cn.com.pansky.webservice.xfire.study.SayHiServiceImpl</implementationClass>
</service>
</beans>

这个文件定义一个WebService: SayHiService,并同时定义了接口和实现类。好了,该建的文件基本建完了.
[color=red][b]4、启动Tomcat,测试WebService[/b][/color]
如果Tomcat还没配置好,抽两分钟再配一下。再把Tomcat启动起来。
再打开浏览器,输入:http://localhost/stove/services
,服务器返回的结果如下:
Available Services:
* SayHiService [wsdl]
Generated by XFire ( http://xfire.codehaus.org )
我们看到我们的WebService已经布署成功了,我们再看看它的WSDL信息:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://cn.com.pansky/SayHiService" xmlns:tns="http://cn.com.pansky/SayHiService" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://cn.com.pansky/SayHiService">
<xsd:element name="sayHi">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHiResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHiRequest">
<wsdl:part name="parameters" element="tns:sayHi">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHiResponse">
<wsdl:part name="parameters" element="tns:sayHiResponse">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="SayHiServicePortType">
<wsdl:operation name="sayHi">
<wsdl:input name="sayHiRequest" message="tns:sayHiRequest">
</wsdl:input>
<wsdl:output name="sayHiResponse" message="tns:sayHiResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SayHiServiceHttpBinding" type="tns:SayHiServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHi">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHiRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHiResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SayHiService">
<wsdl:port name="SayHiServiceHttpPort" binding="tns:SayHiServiceHttpBinding">
<wsdlsoap:address location="http://localhost/stove/services/SayHiService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这个文件跟我们用Axis生成的基本是一样的。

[b][color=red]5、享受美味的时刻[/color][/b]
我们写一个WebService 客户端:

package cn.com.pansky.webservice.xfire.study;

import java.net.MalformedURLException;
import java.util.Map;

import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

public class SayHiClient{
public static void main(String args[]) {
String serviceURL = "http://localhost/stove/services/SayHiService";
Service serviceModel = new ObjectServiceFactory().create(SayHiService.class,null,"http://cn.com.pansky/SayHiService",null);

XFireProxyFactory serviceFactory = new XFireProxyFactory();

try{
SayHiService service = (SayHiService) serviceFactory.create(serviceModel, serviceURL);
Client client = Client.getInstance(service);
//client.addOutHandler(new OutHeaderHandler());

// disable timeout
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "1");

String hello = service.sayHi("kk");
System.out.println("服务器对[kk] 的回答是:" + hello );

hello = service.sayHi(null);
System.out.println("服务器胡言乱语说:" + hello );
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}


编译这个类,再执行一下,服务器服务的结果是:

2011-5-22 17:39:17 org.apache.commons.httpclient.HttpMethodBase writeRequest
信息: 100 (continue) read timeout. Resume sending the request
2011-5-22 17:39:17 org.apache.commons.httpclient.HttpMethodBase readResponse
信息: Discarding unexpected response: HTTP/1.1 100 Continue
服务器对[kk] 的回答是:kk, 你吃了吗?没吃回家吃去吧。
2011-5-22 17:39:20 org.apache.commons.httpclient.HttpMethodBase writeRequest
信息: 100 (continue) read timeout. Resume sending the request
2011-5-22 17:39:20 org.apache.commons.httpclient.HttpMethodBase readResponse
信息: Discarding unexpected response: HTTP/1.1 100 Continue
服务器胡言乱语说:连名字也不肯告诉我吗?

好了,WebService布署成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值