XFire快速上手

关键字: XFire WebService

下载XFrie
首先,去 http://xfire.codehaus.org/下载最新版本的XFire

搭建webservice工程环境
在eclipse里创建一个叫webservice的java工程,然后依次添加src-service、src-conf、src-test和src-util这几个Source Folder以及web这个Folder
目录结构及文件如下:
Java代码 复制代码
  1. webservice   
  2.     src-service   
  3.         cn.hidetoishandsome.xfire.model   
  4.             Book.java   
  5.         cn.hidetoishandsome.xfire.service   
  6.             BookService.java   
  7.         cn.hidetoishandsome.xfire.service.impl   
  8.             BookServiceImpl.java   
  9.     src-conf   
  10.         META-INF   
  11.             xfire   
  12.                 services.xml   
  13.     src-test   
  14.         cn.hidetoishandsome.xfire.test   
  15.             BookServiceTest.java   
  16.     src-util   
  17.         cn.hidetoishandsome.xfire.util   
  18.             XfireClientFactory.java   
  19.     web   
  20.         WEB-INF   
  21.             lib   
  22.             web.xml   
  23.         index.html  
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复制到WEB-INF/lib目录
web.xml内容如下:
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"  
  3.          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  4.   
  5.     <servlet>   
  6.         <servlet-name>xfire</servlet-name>   
  7.         <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>   
  8.     </servlet>   
  9.   
  10.     <servlet-mapping>   
  11.         <servlet-name>xfire</servlet-name>   
  12.         <url-pattern>/services/*</url-pattern>   
  13.     </servlet-mapping>   
  14.   
  15. </web-app>  
<?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>


写一个BookService
我们将创建一个从ISBM号得到Book的Title的简单查询Web服务
首先创建Book.java
Java代码 复制代码
  1. package cn.hidetoishandsome.xfire.model;   
  2.   
  3. public class Book {   
  4.   
  5.     private String title;   
  6.   
  7.     private String isbn;   
  8.   
  9.     public String getIsbn() {   
  10.         return isbn;   
  11.     }   
  12.   
  13.     public void setIsbn(String isbn) {   
  14.         this.isbn = isbn;   
  15.     }   
  16.   
  17.     public String getTitle() {   
  18.         return title;   
  19.     }   
  20.   
  21.     public void setTitle(String title) {   
  22.         this.title = title;   
  23.     }   
  24.   
  25. }  
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;
	}

}

然后写一个BookService接口BookService.java
Java代码 复制代码
  1. package cn.hidetoishandsome.xfire.service;   
  2.   
  3. import cn.hidetoishandsome.xfire.model.Book;   
  4.   
  5. public interface BookService {   
  6.     Book findBookByISBN(String isbn);   
  7. }  
package cn.hidetoishandsome.xfire.service;

import cn.hidetoishandsome.xfire.model.Book;

public interface BookService {
	Book findBookByISBN(String isbn);
}

然后是BookService的实现BookServiceImpl.java
Java代码 复制代码
  1. package cn.hidetoishandsome.xfire.service.impl;   
  2.   
  3. import cn.hidetoishandsome.xfire.model.Book;   
  4. import cn.hidetoishandsome.xfire.service.BookService;   
  5.   
  6. public class BookServiceImpl implements BookService {   
  7.   
  8.     private Book book;   
  9.   
  10.     public BookServiceImpl() {   
  11.         book = new Book();   
  12.         book.setTitle("XFire Quick Start");   
  13.         book.setIsbn("123456");   
  14.     }   
  15.   
  16.     public Book findBookByISBN(String isbn) {   
  17.         if (isbn.equals(book.getIsbn()))   
  18.             return book;   
  19.         throw new RuntimeException("Can't find book");   
  20.     }   
  21.   
  22. }  
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中配置要发布的服务
在src-conf的META-INF/xfire目录创建services.xml
Java代码 复制代码
  1. <beans xmlns="http://xfire.codehaus.org/config/1.0">   
  2.     <service>   
  3.         <name>BookService</name>   
  4.         <namespace>http://localhost:8080/xfire/services/BookService</namespace>   
  5.         <serviceClass>cn.hidetoishandsome.xfire.service.BookService</serviceClass>   
  6.         <implementationClass>cn.hidetoishandsome.xfire.service.impl.BookServiceImpl</implementationClass>   
  7.     </service>   
  8. </beans>  
<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标签决定了我们创建的该服务的WSDL的URL为http://xx.xx.xx/xx/xx/BookService?wsdl

在Tomcat中发布
可以简单的修改Tomcat的server.xml来发布该Web服务,在<Host>标签中添加以下内容:
Java代码 复制代码
  1. Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>  
Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>

现在打开浏览器访问 http://localhost:8080/webservice/services/BookService?wsdl来看看生成的WSDL文档

客户端调用测试
我们将使用一个XfireClientFactory.java工具类来帮我们调用该Web服务:
Java代码 复制代码
  1. package cn.hidetoishandsome.xfire.util;   
  2.   
  3. import java.net.MalformedURLException;   
  4.   
  5. import org.apache.commons.logging.Log;   
  6. import org.apache.commons.logging.LogFactory;   
  7. import org.codehaus.xfire.client.XFireProxyFactory;   
  8. import org.codehaus.xfire.service.Service;   
  9. import org.codehaus.xfire.service.binding.ObjectServiceFactory;   
  10. import org.springframework.util.Assert;   
  11.   
  12. public class XfireClientFactory {   
  13.     private static XFireProxyFactory serviceFactory = new XFireProxyFactory();   
  14.   
  15.     private static final Log log = LogFactory.getLog(XfireClientFactory.class);   
  16.   
  17.     private XfireClientFactory() {   
  18.     }   
  19.   
  20.     public static <T> T getClient(String serviceURL, Class<T> serviceClass) {   
  21.         Assert.notNull(serviceURL);   
  22.         Assert.notNull(serviceClass);   
  23.         Service serviceModel = new ObjectServiceFactory().create(serviceClass);   
  24.         try {   
  25.             return (T) serviceFactory.create(serviceModel, serviceURL);   
  26.         } catch (MalformedURLException e) {   
  27.             log.error(e.getMessage(), e);   
  28.             return null;   
  29.         }   
  30.     }   
  31.   
  32. }  
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;
		}
	}

}

然后编写一个BookServiceTest.java来调用我们刚才发布的Web服务:
Java代码 复制代码
  1. package cn.hidetoishandsome.xfire.test;   
  2.   
  3. import cn.hidetoishandsome.xfire.service.BookService;   
  4. import cn.hidetoishandsome.xfire.util.XfireClientFactory;   
  5.   
  6. public class BookServieTest {   
  7.   
  8.     public static void main(String[] args) {   
  9.         String serviceURL = "http://localhost:8080/webservice/services/BookService";   
  10.         try {   
  11.             BookService service = XfireClientFactory.getClient(serviceURL, BookService.class);   
  12.             System.out.println("Book with ISBN '123456': 《" + service.findBookByISBN("123456").getTitle() + "》");   
  13.         } catch (Exception e) {   
  14.             e.printStackTrace();   
  15.         }   
  16.   
  17.     }   
  18. }  
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();
		}

	}
}

服务调用成功,Console打印内容如下:
Java代码 复制代码
  1. Book with ISBN '123456': 《XFire Quick Start》  

你可能感兴趣的:(java,Web,xml,webservice,servlet)