項目名稱:XFireDemo
實體bean
package stoneyang.bean;
public class Book {
private String name;
private String author;
private String year;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Book() {
super();
}
public Book(String name, String author, String year, String price) {
super();
this.name = name;
this.author = author;
this.year = year;
this.price = price;
}
public Book(String name) {
super();
this.name = name;
}
public Book(String name, String year) {
super();
this.name = name;
this.year = year;
}
public String toString(){
StringBuilder builder = new StringBuilder();
if( name != null && !"".equals(name.trim())){
builder.append( " 名稱: "+name );
}
if( author != null && !"".equals(author.trim())){
builder.append( " 作者: "+author );
}
if( year != null && !"".equals(year.trim())){
builder.append( " 年份: "+year );
}
if( price != null && !"".equals(price.trim())){
builder.append( " 價格: "+price );
}
return builder.toString();
}
}
//介面
package stoneyang.businessI;
import java.util.List;
import stoneyang.bean.Book;
public interface BusinessI {
public Book getBookByName(String name);
public List<Book> getBooksByNames(String[] bookNames);
public List<Book> getBooksWithSpecialYear(Book book);
}
實作類別
package stoneyang.businessIpml;
import stoneyang.businessI.BusinessI;
import java.util.ArrayList;
import java.util.List;
import stoneyang.bean.Book;
import stoneyang.businessI.BusinessI;
public class BusinessImpl implements BusinessI{
public Book getBookByName(String name) {
return new Book(name);
}
public List<Book> getBooksByNames(String[] bookNames) {
List<Book> books = new ArrayList<Book>();
for( String bookName : bookNames ){
books.add(new Book( bookName ));
}
return books;
}
public List<Book> getBooksWithSpecialYear(Book book) {
List<Book> books = new ArrayList<Book>();
books.add(new Book(book.getName()+"翻印版",book.getYear()));
books.add(new Book(book.getName()+"影音版",book.getYear()));
return books;
}
}
測試類別
package stoneyang.test;
import java.net.MalformedURLException;
import java.util.List;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import stoneyang.bean.Book;
import stoneyang.businessI.BusinessI;
public class Test {
public static void main(String args[]) {
// 通過介面類建立Service對象
Service srvcModel = new ObjectServiceFactory().create(BusinessI.class);
// 通過XFire的工廠類建立工廠對象
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
// 訪問的地址
String url = "http://localhost:8080/XFireDemo/services/business";
// 異常處理
try {
// 建立服務物件
BusinessI srvc = (BusinessI) factory.create(srvcModel, url);
// 調用服務中的方法,並顯示其結果
Book book = srvc.getBookByName("《小紅帽》");
book.setYear("1986年");
List<Book> books1 = srvc.getBooksByNames(new String[] { "《社會心理學》","《心理學與生活》" });
List<Book> books2 = srvc.getBooksWithSpecialYear(book);
System.out.println(book);
for (Book b : books1) {
System.out.println("b1---"+b);
}
for (Book b : books2) {
System.out.println("b2---"+b);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
//調用另一個發布的webservice項目
/*srvcModel = new ObjectServiceFactory().create(IAdd.class);
factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
url = "http://localhost:8080/WebServiceTest/services/add";
try {
IAdd iAdd=(IAdd) factory.create(srvcModel, url);
System.err.println(iAdd.add(1, 2));
} catch (MalformedURLException e) {
e.printStackTrace();
}*/
}
}
services.xml設定檔
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>business</name>
<serviceClass>stoneyang.businessI.BusinessI</serviceClass>
<implementationClass>stoneyang.businessIpml.BusinessImpl</implementationClass>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
web.xml設定檔
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
//----------------如果介面中的傳回值需要使用泛型但是實際並沒有使用則添加以下配置,此配置需要和介面在同一個包下邊,命名規則為(介面名.aegis.xml),以上執行個體由於進行了泛型因此不需要此配置-----------------
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="getBooksByNames">
<parameter index="0" componentType="java.lang.String" />
<return-type componentType="stoneyang.bean.Book" />
</method>
<method name="getBooksWithSpecialYear">
<parameter index="0" componentType="stoneyang.bean.Book" />
<return-type componentType="stoneyang.bean.Book" />
</method>
</mapping>
</mappings>
查看地址為:http://localhost:8080/XFireDemo/services/business?wsdl