闲来无事,顺便写个XFire的小例子,也算是打发时间了,o(∩_∩)o...哈哈
定义POM文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.danlley.xfire</groupId> <artifactId>xfire_001</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>xfire_001 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-spring</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.3</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>xfire_001</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project>
构造需用的数据模型:
一:构造Book模型
package org.codehaus.xfire.demo;
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 String toString() {
return "[title=" + this.title + "\tisbn=" + this.isbn + "\tauthor="
+ this.author + "]";
}
}
二:构造Order模型:
package org.codehaus.xfire.demo;
public class Order {
private String order_id;
private String quantity;
private String price;
public String getOrder_id() {
return order_id;
}
public void setOrder_id(String order_id) {
this.order_id = order_id;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String toString() {
return "[price=" + this.price + "\tquantity=" + this.quantity
+ "\torder_id=" + this.order_id + "]";
}
}
三:构造调用返回对象
package org.codehaus.xfire.demo;
public class Result {
private boolean isSuccess = false;
private String message;
public boolean isSuccess() {
return isSuccess;
}
public void setSuccess(boolean isSuccess) {
this.isSuccess = isSuccess;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
定义XFire接口:
package org.codehaus.xfire.demo;
import java.util.Map;
public interface BookService {
public Book[] getBooks();
public Book findBook(String isbn);
@SuppressWarnings("unchecked")
public Map getBooksMap();
public Book initBooks(Book book);
public Result record(Book book, Order order);
}
XFire对外接口实现:
package org.codehaus.xfire.demo;
import java.util.HashMap;
import java.util.Map;
public class BookServiceImpl implements BookService {
private Book onlyBook;
public Book initBooks(Book book) {
book.setAuthor("danlley");
return book;
}
public Result record(Book book, Order order) {
System.out.println(order.toString());
System.out.println(book);
Result result = new Result();
result.setSuccess(true);
return result;
}
public BookServiceImpl() {
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) {
System.out.println(isbn);
return onlyBook;
}
@SuppressWarnings("unchecked")
public Map getBooksMap() {
Map result = new HashMap();
result.put(onlyBook.getIsbn(), onlyBook);
return result;
}
}
在 src\main\webapp\WEB-INF\classes\META-INF\xfire下定义services.xml,如下:
<beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>BookService</name> <namespace>http://xfire.codehaus.org/BookService</namespace> <serviceClass>org.codehaus.xfire.demo.BookService</serviceClass> <implementationClass> org.codehaus.xfire.demo.BookServiceImpl </implementationClass> </service> </beans>
在src\main\webapp\WEB-INF下定义web.xml,如下:
<!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>
开始编写客户端调用:
1). 无参数方法调用
@SuppressWarnings("unchecked")
private void testBookService() {
String serviceURL = "http://localhost:8080/xfire_001/services/BookService";
Service serviceModel = new ObjectServiceFactory().create(
BookService.class, null,
"http://xfire.codehaus.org/BookService", null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
try {
BookService service = (BookService) serviceFactory.create(
serviceModel, serviceURL);
Client client = Client.getInstance(service);
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
Book[] books = service.getBooks();
Map booksMap = service.getBooksMap();
System.out.print("Received map with " + booksMap.size()
+ " book(s) \n");
System.out.println("BOOKS:");
for (int i = 0; i < books.length; i++) {
System.out.println(books[i].getTitle());
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
2). 参数为单个对象的方法调用
private void testBook_002() {
String service_url = "http://localhost:8080/xfire_001/services/BookService";
Service serviceModel = new ObjectServiceFactory().create(
BookService.class, null,
"http://xfire.codehaus.org/BookService", null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
try {
BookService service = (BookService) serviceFactory.create(
serviceModel, service_url);
Client client = Client.getInstance(service);
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
Book book = new Book();
book.setAuthor("min.weixm");
Book my = service.initBooks(book);
System.out.println("my.getAuthor()" + my.getAuthor());
} catch (Exception e) {
e.printStackTrace();
}
}
3). 接口参数为多个的情况下,方法调用
private void testBook_001() {
String service_url = "http://localhost:8080/xfire_001/services/BookService";
Service serviceModel = new ObjectServiceFactory().create(
BookService.class, null,
"http://xfire.codehaus.org/BookService", null);
XFireProxyFactory serviceFactory = new XFireProxyFactory();
try {
BookService service = (BookService) serviceFactory.create(
serviceModel, service_url);
Client client = Client.getInstance(service);
client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");
Result result = service.record(getBook(), getOrder());
System.out.println("result:" + result.isSuccess());
} catch (Exception e) {
e.printStackTrace();
}
}
未完待续