前两天下了XFire1.0和Axis2。一直拿不定在项目中使用Axis2还是XFire。那就两个分别作个example测试一番。
首先是XFire:
下载XFire库:
distribution和XFire-all,distribution是一些依赖包,例子及文档,XFire-all是XFire的核心包.
然后是根据文档中的Quick Start来实现一个Web Service,Quick Start中举的例子在distibution目录下的example/book下.根据Quick Start来实现一个Web Service的过程是非常简单的,文档上这么讲而且事实也是非常简单的,如果在Eclipse中也就是包括以下几个步骤:
- 设置目录结构,设置类路径.
- 编写一个要作为Web Service发布的java类.
- 创建XFire配置.
- 修改web.xml,为XFire配置Servlet.
- 因为Lomboz会自动部署,那么剩下的就是测试Web Service部署是否成功了.
下面就开始在Eclipse中开始以上列出的步骤:
JDK:JSDK 1.4
IDE:
Eclipse3.1.2
Plugins:
lomboz-wtp-emf-gef-jem(其中已经包含有
wtp-sdk-R-1.0.1-200602171228 ,
emf-sdo-xsd-SDK-2.1.2,
GEF-SDK-3.1.1 ,
JEM-SDK-1.1.0.1)
Container:
Tomcat5.0
1.新建一个dynamic Web Project 名称为testXFire,添加XFire库(包括XFire-distibution中lib中所有的.jar文件和xfire-all-1.0.jar)到工程中,做完上述工作后整个工程结构如下图:
2.编写一个需要作为Web Service发布的java类:
a)Service类
package com.bws.XFireDemo;
public class BookService
{
private Book onlyBook;
public BookService()
{
onlyBook = new Book();
onlyBook.setAuthor("Dan Diephouse");
onlyBook.setTitle("Using XFire");
onlyBook.setIsbn("0123456789");
}
public Book[] getBooks()
{
return new Book[] { onlyBook };
}
public Book findBook(String isbn)
{
if (isbn.equals(onlyBook.getIsbn()))
return onlyBook;
return null;
}
}
public class BookService
{
private Book onlyBook;
public BookService()
{
onlyBook = new Book();
onlyBook.setAuthor("Dan Diephouse");
onlyBook.setTitle("Using XFire");
onlyBook.setIsbn("0123456789");
}
public Book[] getBooks()
{
return new Book[] { onlyBook };
}
public Book findBook(String isbn)
{
if (isbn.equals(onlyBook.getIsbn()))
return onlyBook;
return null;
}
}
b)对应的Book.java
package com.bws.XFireDemo;
public class Book {
private String title;
private String isbn;
private String author;
public String getIsbn()
{
return isbn;
}
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
}
public class Book {
private String title;
private String isbn;
private String author;
public String getIsbn()
{
return isbn;
}
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
}
3.创建XFire配置:
a)在源文件夹中创建META-INF/xfire文件夹.
b)在该文件夹中创建一个service.xml文件,这个文件就是描述需要部署的Web Services.内容如下:
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>BookService</name>
<namespace>http://com.bws.XFireDemo/BookService</namespace>
<serviceClass>test.BookService</serviceClass>
</service>
</beans>
<service>
<name>BookService</name>
<namespace>http://com.bws.XFireDemo/BookService</namespace>
<serviceClass>test.BookService</serviceClass>
</service>
</beans>
name:Web Service名称
namespace:命名空间
serviceClass:作为Web Service发布的类名
完成后目录结构如下图:
4.修改web.xml,为XFire配置Servlet.
<servlet>
<servlet-name>XFireServlet</servlet-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>
<servlet-name>XFireServlet</servlet-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>
5.测试Web Service
a)将XFire所需库拷贝至WebContent/WEB-INF/lib下:
b)运行工程于服务器上:
c)访问
http://localhost:8080/testXFire/services/BookService?wsdl将会显示BookService Web Service的描述文件,其中不久又对服务的描述,并且会自动生成服务引用类的typeMapping即complexType.
<?xml version="1.0" encoding="UTF-8" ?>
-
<
wsdl:definitions
xmlns:wsdl
="
http://schemas.xmlsoap.org/wsdl/
"
xmlns:ns1
="
http://XFireDemo.bws.com
"
xmlns:soap11
="
http://schemas.xmlsoap.org/soap/envelope/
"
xmlns:soap12
="
http://www.w3.org/2003/05/soap-envelope
"
xmlns:soapenc11
="
http://schemas.xmlsoap.org/soap/encoding/
"
xmlns:soapenc12
="
http://www.w3.org/2003/05/soap-encoding
"
xmlns:tns
="
http://com.bws.XFireDemo/BookService
"
xmlns:wsdlsoap
="
http://schemas.xmlsoap.org/wsdl/soap/
"
xmlns:xsd
="
http://www.w3.org/2001/XMLSchema
"
targetNamespace
="
http://com.bws.XFireDemo/BookService
">
-
<
xsd:schema targetNamespace
="
http://com.bws.XFireDemo/BookService
"
elementFormDefault
="
qualified
"
attributeFormDefault
="
qualified
">
<
xsd:complexType
/>
</
xsd:element
>
</
xsd:schema
>
-
<
xsd:schema targetNamespace
="
http://XFireDemo.bws.com
"
elementFormDefault
="
qualified
"
attributeFormDefault
="
qualified
">
<
xsd:element
name
="
Book
"
type
="
ns1:Book
"
nillable
="
true
"
minOccurs
="
0
"
maxOccurs
="
unbounded
" />
</
xsd:sequence
>
</
xsd:complexType
>
<
xsd:element
name
="
author
"
type
="
xsd:string
"
minOccurs
="
0
"
nillable
="
true
" />
<
xsd:element
name
="
isbn
"
type
="
xsd:string
"
minOccurs
="
0
"
nillable
="
true
" />
<
xsd:element
name
="
title
"
type
="
xsd:string
"
minOccurs
="
0
"
nillable
="
true
" />
</
xsd:sequence
>
</
xsd:complexType
>
</
xsd:schema
>
</
wsdl:types
>
<
wsdl:part
element
="
tns:findBook
"
name
="
parameters
" />
</
wsdl:message
>
<
wsdl:part
element
="
tns:getBooksResponse
"
name
="
parameters
" />
</
wsdl:message
>
<
wsdl:part
element
="
tns:findBookResponse
"
name
="
parameters
" />
</
wsdl:message
>
<
wsdl:part
element
="
tns:getBooks
"
name
="
parameters
" />
</
wsdl:message
>
<
wsdl:input
message
="
tns:getBooksRequest
"
name
="
getBooksRequest
" />
<
wsdl:output
message
="
tns:getBooksResponse
"
name
="
getBooksResponse
" />
</
wsdl:operation
>
<
wsdl:input
message
="
tns:findBookRequest
"
name
="
findBookRequest
" />
<
wsdl:output
message
="
tns:findBookResponse
"
name
="
findBookResponse
" />
</
wsdl:operation
>
</
wsdl:portType
>
<
wsdlsoap:binding
style
="
document
"
transport
="
http://schemas.xmlsoap.org/soap/http
" />
</
wsdl:binding
>
<
wsdlsoap:address
location
="
http://localhost:8080/testXFire/services/BookService
" />
</
wsdl:port
>
</
wsdl:service
>
</
wsdl:definitions
>
至此Web Service的发布过程就完成了,剩下的就是进行Client的测试了.