下載XFrie
首先,去http://xfire.codehaus.org下載最新版本的XFire
搭建webservice工程環(huán)境
在eclipse里創(chuàng)建一個(gè)叫webservice的java工程,然后依次添加src-service、src-conf、src-test和src-util這幾個(gè)Source Folder以及web這個(gè)Folder
目錄結(jié)構(gòu)及文件如下:
代碼
- webservice
- src-service
- cn.hidetoishandsome.xfire.model
- Book.java
- cn.hidetoishandsome.xfire.service
- BookService.java
- cn.hidetoishandsome.xfire.service.impl
- BookServiceImpl.java
- src-conf
- META-INF
- xfire
- services.xml
- src-test
- cn.hidetoishandsome.xfire.test
- BookServiceTest.java
- src-util
- cn.hidetoishandsome.xfire.util
- XfireClientFactory.java
- web
- WEB-INF
- lib
- web.xml
- index.html
然后將解壓后的xfire的lib目錄下所有jar包和xfire-all-1.*.jar復(fù)制到WEB-INF/lib目錄
web.xml內(nèi)容如下:
代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>xfire</servlet-name>
- <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>xfire</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
寫一個(gè)BookService
我們將創(chuàng)建一個(gè)從ISBM號(hào)得到Book的Title的簡(jiǎn)單查詢Web服務(wù)
首先創(chuàng)建Book.java
代碼
- package cn.hidetoishandsome.xfire.model;
- public class Book {
- private String title;
- private String isbn;
- 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;
- }
- }
然后寫一個(gè)BookService接口BookService.java
代碼
- package cn.hidetoishandsome.xfire.service;
- import cn.hidetoishandsome.xfire.model.Book;
- public interface BookService {
- Book findBookByISBN(String isbn);
- }
然后是BookService的實(shí)現(xiàn)BookServiceImpl.java
代碼
- package cn.hidetoishandsome.xfire.service.impl;
- import cn.hidetoishandsome.xfire.model.Book;
- import cn.hidetoishandsome.xfire.service.BookService;
- public class BookServiceImpl implements BookService {
- private Book book;
- public BookServiceImpl() {
- book = new Book();
- book.setTitle("XFire Quick Start");
- book.setIsbn("123456");
- }
- public Book findBookByISBN(String isbn) {
- if (isbn.equals(book.getIsbn()))
- return book;
- throw new RuntimeException("Can't find book");
- }
- }
在services.xml中配置要發(fā)布的服務(wù)
在src-conf的META-INF/xfire目錄創(chuàng)建services.xml
代碼
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>BookService</name>
- <namespace>http://localhost:8080/xfire/services/BookService</namespace>
- <serviceClass>cn.hidetoishandsome.xfire.service.BookService</serviceClass>
- <implementationClass>cn.hidetoishandsome.xfire.service.impl.BookServiceImpl</implementationClass>
- </service>
- </beans>
其中name標(biāo)簽決定了我們創(chuàng)建的該服務(wù)的WSDL的URL為http://xx.xx.xx/xx/xx/BookService?wsdl
在Tomcat中發(fā)布
可以簡(jiǎn)單的修改Tomcat的server.xml來發(fā)布該Web服務(wù),在<Host>標(biāo)簽中添加以下內(nèi)容:
代碼
- Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>
現(xiàn)在打開瀏覽器訪問http://localhost:8080/webservice/services/BookService?wsdl來看看生成的WSDL文檔
客戶端調(diào)用測(cè)試
我們將使用一個(gè)XfireClientFactory.java工具類來幫我們調(diào)用該Web服務(wù):
代碼
- package cn.hidetoishandsome.xfire.util;
- import java.net.MalformedURLException;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
- import org.springframework.util.Assert;
- public class XfireClientFactory {
- private static XFireProxyFactory serviceFactory = new XFireProxyFactory();
- private static final Log log = LogFactory.getLog(XfireClientFactory.class);
- private XfireClientFactory() {
- }
- public static <T> T getClient(String serviceURL, Class<T> serviceClass) {
- Assert.notNull(serviceURL);
- Assert.notNull(serviceClass);
- Service serviceModel = new ObjectServiceFactory().create(serviceClass);
- try {
- return (T) serviceFactory.create(serviceModel, serviceURL);
- } catch (MalformedURLException e) {
- log.error(e.getMessage(), e);
- return null;
- }
- }
- }
然后編寫一個(gè)BookServiceTest.java來調(diào)用我們剛才發(fā)布的Web服務(wù):
代碼
- package cn.hidetoishandsome.xfire.test;
- import cn.hidetoishandsome.xfire.service.BookService;
- import cn.hidetoishandsome.xfire.util.XfireClientFactory;
- public class BookServieTest {
- public static void main(String[] args) {
- String serviceURL = "http://localhost:8080/webservice/services/BookService";
- try {
- BookService service = XfireClientFactory.getClient(serviceURL, BookService.class);
- System.out.println("Book with ISBN '123456': 《" + service.findBookByISBN("123456").getTitle() + "》");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
服務(wù)調(diào)用成功,Console打印內(nèi)容如下:
代碼
- Book with ISBN '123456': 《XFire Quick Start》