【转】Java WebServices教程(1)-使用XFire
2012-12-12 11:43
405 查看
原地址:http://cnjava.blog.51cto.com/1208887/335070
accp的课程的webService的框架是xfire。
以下是Ibm dw中对Xfire的理论介绍。
---------------------------
XFire 概述
XFire 是 codeHaus 组织提供的一个开源框架,它构建了 POJO 和 SOA 之间的桥梁,主要特性就是支持将 POJO 通过非常简单的方式发布成 Web 服务,这种处理方式不仅充分发挥了 POJO 的作用,简化了 Java 应用转化为 Web 服务的步骤和过程,也直接降低了 SOA 的实现难度,为企业转向 SOA 架构提供了一种简单可行的方式。
XFire 目前的稳定版本是 1.2.6,(在myEclipse6.01中集成了Xfire1.2.6.)
目前支持的特性主要包括:支持将 Web 服务绑定到 POJO、XMLBeans、JAXB1.1、JAXB2.0 和 Castor;
支持基于 HTTP、JMS、XMPP 等多种协议访问 Web 服务;
支持多种 Web 服务业界重要标准如 SOAP、WSDL、Web 服务寻址(WS-Addressing)、Web 服务安全(WS-Security)等;
支持 JSR181,可以通过 JDK5 配置 Web 服务;
高性能的 SOAP 实现;
服务器端、客户端代码辅助生成;
对 Spring、Pico、Plexus 等项目的支持等。
-------------------------------------------------------------------------------
XFire 安装包
XFire 框架目前的最新版本是 1.2.6,可以访问 xfire.codehaus.org 下载 XFire 框架的安装包,下载时请选择“全部二进制发布包(Binary Distribution in zip package)”,而不仅仅是“XFire jar 文件(Jar of all XFire modules)”。
下载完成后,我们可以将下载的 .zip 文件解压缩到任意的文件夹中(后面的章节中使用 % XFIRE_HOME % 表示 XFire 框架的安装目录),解压缩后形成的文件目录结构如下:
api(目录)
api 目录中是 XFire 框架中所有类(class)对应的 API 文档,为开发者使用 XFire 完成应用开发提供帮助。
examples(目录)
examples 目录中包含了所有随 XFire 二进制包发布的实例,包括这些实例的源代码和相关 Web 应用配置内容。
lib(目录)
lib 目录中包含 XFire 运行所需要的外部支持类包(.jar文件),可以根据不同项目所需的 XFire 特性选择所需要的支持类包。保守的方法是在 Web 项目中包含所有的外部支持类包(.jar文件)。
manual(目录)
manual 目录中包含有 XFire 框架的帮助文档,开发者可以从这些帮助文档中学习更多运用 XFire 框架实现 SOA 的知识和技巧。
modules(目录)
modules 目录中包含了 XFire 框架根据不同特性分别编译的二进制包文件。发布基于 XFire 框架的 Web 项目时,可以选择使用该目录下的所有 .jar 文件,也可以选择 XFire-all-1.2.6.jar 文件。
XFire-all-1.2.6.jar
XFire 框架的二进制包文件,包含了全部的模块(modules)。
LICENSE.txt
LICENSE.txt 文件中包含了 XFire 框架的授权协议。
NOTICE.txt
README.txt
这两个文件中包含了 XFire 发布时的一些有用的信息。
---------------------------------------
下面我们使用IDE开发一个简单的webServices.
一)使用MyEclipse6.01来开发一个webService服务端。
使用MyEclipse来开发webService的服务器端比较简单。主要经过如果几个步骤
1)首先建立一个WebServices工程
如果
其中myEclipse会自动配置好XFire的Servlet。
以下为Web.xml文件的配置内容
2)定义WebService的接口
view plaincopy to clipboardprint?
3)定义接口的实现类
4)创建 XFire 框架的服务发布文件 services.xml
按Xfire的开发指南上说是要在
WEB-INF\classes\META-INF\xfire\services.xml
的位置。但是MyEclipse已经在工程的下面建立了一个WebService/services.xml的文件。
其实是myclipse帮助我们映射好了位置而已。
其中各元素的功能如下:
service
service 标签和它所包含的 xml 内容为发布成 Web 服务的 POJO 提供完整的描述。
name
Web 服务被发布时所采用的唯一名称。
namespace
Web 服务发布时所使用的命名空间。
serviceClass
Web 服务接口类的全名,包括包名和类名。
implemetationClass
Web 服务实现类的全名,包括包名和类名。
下面我们就可以部署这个工程,顺便提一下,如果正常的话你可以在tomcat启动的过程中
看到如下的信息信息:
2010-6-18 14:38:26
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
........
Exposing service with name {www.pengzj.com.cn/hellows}hellows
就可以知道xfire使用了spring。
现在我们可以在ie中输入如下的地址访问它
http://127.0.0.1:8080/xfire_hello_service/services/
会出现如下的可用服务列表:
点击wsdl的超链接出现这个wsdl文件的内容大致如下:
ok.这就表示服务端大功告成了。
接下来就是在客户端要如何调用WebServices了。
accp的课程的webService的框架是xfire。
以下是Ibm dw中对Xfire的理论介绍。
---------------------------
XFire 概述
XFire 是 codeHaus 组织提供的一个开源框架,它构建了 POJO 和 SOA 之间的桥梁,主要特性就是支持将 POJO 通过非常简单的方式发布成 Web 服务,这种处理方式不仅充分发挥了 POJO 的作用,简化了 Java 应用转化为 Web 服务的步骤和过程,也直接降低了 SOA 的实现难度,为企业转向 SOA 架构提供了一种简单可行的方式。
XFire 目前的稳定版本是 1.2.6,(在myEclipse6.01中集成了Xfire1.2.6.)
目前支持的特性主要包括:支持将 Web 服务绑定到 POJO、XMLBeans、JAXB1.1、JAXB2.0 和 Castor;
支持基于 HTTP、JMS、XMPP 等多种协议访问 Web 服务;
支持多种 Web 服务业界重要标准如 SOAP、WSDL、Web 服务寻址(WS-Addressing)、Web 服务安全(WS-Security)等;
支持 JSR181,可以通过 JDK5 配置 Web 服务;
高性能的 SOAP 实现;
服务器端、客户端代码辅助生成;
对 Spring、Pico、Plexus 等项目的支持等。
-------------------------------------------------------------------------------
XFire 安装包
XFire 框架目前的最新版本是 1.2.6,可以访问 xfire.codehaus.org 下载 XFire 框架的安装包,下载时请选择“全部二进制发布包(Binary Distribution in zip package)”,而不仅仅是“XFire jar 文件(Jar of all XFire modules)”。
下载完成后,我们可以将下载的 .zip 文件解压缩到任意的文件夹中(后面的章节中使用 % XFIRE_HOME % 表示 XFire 框架的安装目录),解压缩后形成的文件目录结构如下:
api(目录)
api 目录中是 XFire 框架中所有类(class)对应的 API 文档,为开发者使用 XFire 完成应用开发提供帮助。
examples(目录)
examples 目录中包含了所有随 XFire 二进制包发布的实例,包括这些实例的源代码和相关 Web 应用配置内容。
lib(目录)
lib 目录中包含 XFire 运行所需要的外部支持类包(.jar文件),可以根据不同项目所需的 XFire 特性选择所需要的支持类包。保守的方法是在 Web 项目中包含所有的外部支持类包(.jar文件)。
manual(目录)
manual 目录中包含有 XFire 框架的帮助文档,开发者可以从这些帮助文档中学习更多运用 XFire 框架实现 SOA 的知识和技巧。
modules(目录)
modules 目录中包含了 XFire 框架根据不同特性分别编译的二进制包文件。发布基于 XFire 框架的 Web 项目时,可以选择使用该目录下的所有 .jar 文件,也可以选择 XFire-all-1.2.6.jar 文件。
XFire-all-1.2.6.jar
XFire 框架的二进制包文件,包含了全部的模块(modules)。
LICENSE.txt
LICENSE.txt 文件中包含了 XFire 框架的授权协议。
NOTICE.txt
README.txt
这两个文件中包含了 XFire 发布时的一些有用的信息。
---------------------------------------
下面我们使用IDE开发一个简单的webServices.
一)使用MyEclipse6.01来开发一个webService服务端。
使用MyEclipse来开发webService的服务器端比较简单。主要经过如果几个步骤
1)首先建立一个WebServices工程
如果
其中myEclipse会自动配置好XFire的Servlet。
以下为Web.xml文件的配置内容
<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>
2)定义WebService的接口
view plaincopy to clipboardprint?
public interface IHelloService { public String sayHi(String uname); } public interface IHelloService { public String sayHi(String uname); }
3)定义接口的实现类
public class IHelloServiceImpl implements IHelloService{ @Override public String sayHi(String uname) { return "Hello "+uname; } } public class IHelloServiceImpl implements IHelloService{ @Override public String sayHi(String uname) { return "Hello "+uname; } }
4)创建 XFire 框架的服务发布文件 services.xml
按Xfire的开发指南上说是要在
WEB-INF\classes\META-INF\xfire\services.xml
的位置。但是MyEclipse已经在工程的下面建立了一个WebService/services.xml的文件。
其实是myclipse帮助我们映射好了位置而已。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>hellows</name> <namespace>www.pengzj.com.cn/hellows</namespace> <serviceClass> com.pengzj.service.IHelloService </serviceClass> <implementationClass> com.pengzj.service.impl.IHelloServiceImpl </implementationClass> </service> </beans>
其中各元素的功能如下:
service
service 标签和它所包含的 xml 内容为发布成 Web 服务的 POJO 提供完整的描述。
name
Web 服务被发布时所采用的唯一名称。
namespace
Web 服务发布时所使用的命名空间。
serviceClass
Web 服务接口类的全名,包括包名和类名。
implemetationClass
Web 服务实现类的全名,包括包名和类名。
下面我们就可以部署这个工程,顺便提一下,如果正常的话你可以在tomcat启动的过程中
看到如下的信息信息:
2010-6-18 14:38:26
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
........
Exposing service with name {www.pengzj.com.cn/hellows}hellows
就可以知道xfire使用了spring。
现在我们可以在ie中输入如下的地址访问它
http://127.0.0.1:8080/xfire_hello_service/services/
会出现如下的可用服务列表:
点击wsdl的超链接出现这个wsdl文件的内容大致如下:
<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions targetNamespace="www.pengzj.com.cn/hellows" xmlns:ns1="http://service.pengzj.com" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="www.pengzj.com.cn/hellows" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> + <wsdl:types> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="www.pengzj.com.cn/hellows"> - <xsd:element name="getAllUser"> <xsd:complexType /> </xsd:element> - <xsd:element name="getAllUserResponse"> - <xsd:complexType> - <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="ns1:ArrayOfUser" /> </xsd:sequence> </xsd:complexType> </xsd:element> - <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> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.pengzj.com"> - <xsd:complexType name="ArrayOfUser"> - <xsd:sequence> <xsd:element maxOccurs="unbounded" minOccurs="0" name="User" nillable="true" type="ns1:User" /> </xsd:sequence> </xsd:complexType> - <xsd:complexType name="User"> - <xsd:sequence> <xsd:element minOccurs="0" name="age" type="xsd:int" /> <xsd:element minOccurs="0" name="uname" nillable="true" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> + <wsdl:message name="sayHiRequest"> <wsdl:part name="parameters" element="tns:sayHi" /> </wsdl:message> + <wsdl:message name="getAllUserResponse"> <wsdl:part name="parameters" element="tns:getAllUserResponse" /> </wsdl:message> + <wsdl:message name="getAllUserRequest"> <wsdl:part name="parameters" element="tns:getAllUser" /> </wsdl:message> + <wsdl:message name="sayHiResponse"> <wsdl:part name="parameters" element="tns:sayHiResponse" /> </wsdl:message> + <wsdl:portType name="hellowsPortType"> - <wsdl:operation name="getAllUser"> <wsdl:input name="getAllUserRequest" message="tns:getAllUserRequest" /> <wsdl:output name="getAllUserResponse" message="tns:getAllUserResponse" /> </wsdl:operation> - <wsdl:operation name="sayHi"> <wsdl:input name="sayHiRequest" message="tns:sayHiRequest" /> <wsdl:output name="sayHiResponse" message="tns:sayHiResponse" /> </wsdl:operation> </wsdl:portType> + <wsdl:binding name="hellowsHttpBinding" type="tns:hellowsPortType"> <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="getAllUser"> <wsdlsoap:operation soapAction="" /> - <wsdl:input name="getAllUserRequest"> <wsdlsoap:body use="literal" /> </wsdl:input> - <wsdl:output name="getAllUserResponse"> <wsdlsoap:body use="literal" /> </wsdl:output> </wsdl:operation> - <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" /> Technorati 标签: java,j2ee,webservices,xfire </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="hellows"> - <wsdl:port name="hellowsHttpPort" binding="tns:hellowsHttpBinding"> <wsdlsoap:address location="http://127.0.0.1:8080/xfire_hello_service/services/hellows" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
ok.这就表示服务端大功告成了。
接下来就是在客户端要如何调用WebServices了。
相关文章推荐
- Java WebServices教程(1)-使用XFire
- Java WebServices教程(1)-使用XFire
- Java WebServices教程(1)-使用XFire
- jax-rs(Java API for RESTful Web Services)实践教程 之二 —— get/post/put/delete/head
- Java Web基础教程四 ---------配置篇三mysql数据库的安装配置使用
- Use xfire to done your web services in java
- 使用Eclipse+Maven+Jetty构建Java Web开发环境(几个教程综合集成2014发行)
- 【Java】HTTP Web调试工具Fiddler介绍和使用教程
- 使用Java进行FreeMarker的web模板开发的基础教程
- Web图表控件ChartDirector使用教程:轻松创建Java图表应用程序
- jax-rs(Java API for RESTful Web Services) 实践教程 之五 —— 注入全局变量 和 rest的生命周期
- 如何与他人共享你的JavaWeb项目(一)花生壳内网穿透使用教程(新手上路)
- Java WebService总结(使用xfire)
- JavaWEB之Markdown 编辑器Editor.md集成使用教程
- 使用Java进行FreeMarker的web模板开发的基础教程
- jax-ws(Java API for XML Web Services)实践教程
- Java WebService使用简单教程
- 使用WebDriver + Java + Junit做自动化测试教程
- Java WebService 使用实例(xfire)
- ImportNew 首页所有文章资讯Web架构基础技术书籍教程我要投稿更多频道 » 如何在 Java 中正确使用 wait, notify 和 notifyAll – 以生产者消费者模型为例