项目名称: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
本文介绍了一个基于XFIRE框架的Web服务示例项目,包括实体Bean定义、业务接口及其实现类,以及如何通过测试类调用这些服务。演示了如何通过XFIRE发布和调用Web服务。
335

被折叠的 条评论
为什么被折叠?



