记录学习过、研究过、使用过和总结过的内容,以备不时之需
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
<?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>
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; } }
package cn.hidetoishandsome.xfire.service; import cn.hidetoishandsome.xfire.model.Book; public interface BookService { Book findBookByISBN(String isbn); }
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"); } }
<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>
Context path="/webservice" docBase="D:\project\webservice\web" reloadable="true"/>
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; } } }
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(); } } }
posted on 2008-09-09 16:21 雪山飞狐 阅读(353) 评论(0) 编辑 收藏 所属分类: 开源框架
Powered by: BlogJava Copyright © 雪山飞狐