文章主要介紹:
1:用xfire+spring+maven建構webservice伺服器。
2:用xfire的eclipse插件生成用戶端通路方式。
3:以知接口和bean生成用戶端。
4:用戶端動态通路。包括傳回java自定義對象。
(一)肯定首先配置spring上下文監聽器和xfire的servert配置,将下列代碼加入web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>webservice</display-name>
<!-- springmvc 上下文監聽器 ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<!-- springmvc 上下文監聽器 ContextLoaderListener-->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
(二)配置 spring的配置檔案,最基本的包括導入xfire.xml和baseWebService,剩餘2個bean是自定義的webservice接口和實作類。代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<property name="serviceFactory" ref="xfire.serviceFactory" />
<property name="xfire" ref="xfire" />
</bean>
<bean id="bookWS" class="com.xiaoji.webservice.xfire.service.impl.BookServiceImpl">
<bean id="bookService" parent="baseWebService">
<property name="serviceBean" ref="bookWS" />
<property name="serviceClass" value="com.xiaoji.webservice.xfire.service.BookService" />
</bean>
</beans>
(三)剩下的就是自定義接口和實作類,對象bean.代碼如下
book.java

package com.xiaoji.webservice.xfire.entity;
public class Book {
private int bookId;
private String name;
public Book(){
}
public Book(int bookId,String name){
this.bookId = bookId;
this.name = name;
public int getBookId() {
return bookId;
public void setBookId(int bookId) {
public String getName() {
return name;
public void setName(String name) {
}
BookService和BookServiceimpl,定義了2個方法,一個傳入字元串并傳回字元串,另外一個傳入個數字傳回個對象:

package com.xiaoji.webservice.xfire.service;
import com.xiaoji.webservice.xfire.entity.Book;
public interface BookService {
public Book getBookById(int id);
public String sayHello(String str);

package com.xiaoji.webservice.xfire.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.xiaoji.webservice.xfire.service.BookService;
public class BookServiceImpl implements BookService {
public Book getBookById(int id) {
Book book = new Book(1,"china");
return book;
public String sayHello(String str) {
// TODO Auto-generated method stub
return "this is " + str + ".";
最後還是要貼上pom.xml源碼

<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>com.xiaoji.webservice</groupId>
<artifactId>webservice-xfire</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webservice-xfire Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-core</artifactId>
<version>1.2.5</version>
</dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-spring</artifactId>
<version>1.2.6</version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>webservice-xfire</finalName>
</build>
</project>
1.伺服器注解:伺服器配置基本這樣,有一點不了解的地方就是不能傳回List,map等集合對象。如果有人實作可以互相交流哈。
啟動jetty,通路127.0.0.1:8080/webservice-xfire/service/BookService?wsdl。
啟動tomcat7,通路:127.0.0.1:8888/service/BookService?wsdl。
2.用xfire的eclipse插件生成用戶端通路方式。
建立個maven-archetype-webapp,右鍵建立的項目,選擇new-》other-》Code generation from WSDL document,wsdl url or path為:127.0.0.1:8080/webservice-xfire/service/BookService?wsdl;output directory選擇建立項目下的src/java/main;最後輸入package(随便輸);确定即可。
你會發現包裡面多了個xfire組包 可以删掉,建立個用戶端類,代碼如下:

package com.xiaoji.webservice.xfire.client;
import java.net.MalformedURLException;
public class WebserviceXfireClient {
public static void main(String[] args) throws Exception {
//xfire用戶端通路webservice第3種方式
//xfire自動生成用戶端
BookServiceClient client = new BookServiceClient();
BookServicePortType bs = client.getBookServiceHttpPort();
System.out.println(bs.sayHello("小吉"));
System.out.println(bs.getBookById(0).getName().getValue());
最後貼上xfire的pom.xml:

<groupId>com.xiaoji.webservice.xfire</groupId>
<artifactId>eclipseplugin</artifactId>
<name>eclipseplugin Maven Webapp</name>
<dependency>
<artifactId>xfire-all</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<finalName>eclipseplugin</finalName>
3:第二種通路方式是知道接口的自定義對象bean時用,這裡要注意,必須類名和包名一直,要不通路不到的。
這裡隻貼用戶端代碼。pom.xml,類和接口如上,

// 第一種通路方式需要知道接口和bean
public static void testClient1() throws MalformedURLException {
String serviceUrl = "http://127.0.0.1:8888/service/BookService";
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
Service service = new ObjectServiceFactory().create(BookService.class);
BookService bs = (BookService) factory.create(service, serviceUrl);
System.out.println(bs.sayHello("小吉111111"));
System.out.println(bs.getBookById(0).getName());
4:第4中用戶端動态通路,這裡我剛剛開始的時候始終不能傳回自定義對象,傳回的類型為DocumentImpl,我就想辦法解析這個類型查找類型相關資料,終于解析成功,這裡共享給大家:

// 動态用戶端第2種通路方式
public static void testClient2() throws MalformedURLException, Exception {
Client client = new Client(
new URL(
"http://127.0.0.1:8888/service/BookService?wsdl"));
Object[] results = client.invoke("sayHello", new Object[] { "吉淩夷" });
Object[] results2 = client.invoke("getBookById", new Object[] { 1 });
System.out.println(results[0]);
//傳回對象解析
Document xmlTree = (Document)results2[0];
Element root = xmlTree.getDocumentElement();
parseElement(root);
System.out.println(map.get("bookId")+"->"+map.get("name"));
/**
* 解析傳回樹
* @param root
*/
private static void parseElement(Element root) {
String key = root.getNodeName();
NodeList list = root.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node instanceof Element) {
Element e = (Element) node;
parseElement(e);
} else if (node instanceof Text) {
Text t = (Text) node;
map.put(key, t.getNodeValue());
}
}
注:pom.xml和第2中是一樣的,這裡要說一下xfire,官網很久沒更新,官網上說的是和cxf合并了,具體咋樣大家有待考證。
剩下的就是axis2,也是資料最多的。如果不是很忙的話我會很快更新出來。
下面我會上傳源碼:其中一個是伺服器,2個是用戶端,第2中通路和第3中通路我放在一起,請自行分别。
本文轉自 興趣e族 51CTO部落格,原文連結:http://blog.51cto.com/simplelife/1847131