XFire WebService開發快速起步
環境:
XFire-1.2.6
JDK1.5
MyEclipse 6.5
Tomcat-5.5.27
Windows XP Professional簡體中文版
軟體下載下傳位址:
[url]http://repository.codehaus.org/org/codehaus/xfire/xfire-distribution/1.2.6/xfire-distribution-1.2.6.zip[/url]
[url]http://apache.mirror.phpchina.com/tomcat/tomcat-5/v5.5.27/bin/apache-tomcat-5.5.27.zip[/url]
有關WebService的概念、原理、資料發現、描述、綁定等過程、方式也不說了。這裡就隻關注如何快速開發出來一個通用的、易懂的Hello World例子。
以下是開發步驟:
1、建立工程
打開MyEclipse 6.5,建立一個WebService工程。如下圖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcugDOzkjN0YDOwQjMyEjNxATM4ADMy8CXwEDOwAjMvwFduVWboNWY0RXYvwVbvNmLvR3YxUjL1M3Lc9CX6MHc0RHaiojIsJye.png)
然後一路next,直到完成。
建立完成後,打開生成的web.xml檔案,可以看到,XFire已經配置好了。
<?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 [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]">
<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>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、建立WebService服務
建立兩個個包“wstest.server”和“wstest.client”,用來儲存服務端和用戶端程式。然後開始建立服務端程式,如下圖
完成後,生成了一個Service的配置services.xml:
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>MyService</name>
<serviceClass>wstest.server.IMyService</serviceClass>
<implementationClass>
wstest.server.MyServiceImpl
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
也生成了接口和預設實作,改寫後如下:
package wstest.server;
//Generated by MyEclipse
public interface IMyService {
public String sayHello(String user);
}
public class MyServiceImpl implements IMyService {
public String sayHello(String user) {
return "您好,"+user;
}
至此,服務端代碼已經完成。
3、測試服務端代碼
測試依賴與Servlet容器Tomcat,需要将做好的服務端打包部署到tomcat上,然後啟動。才可以進行測試。假設你已經配置了Tomcat伺服器,并完成了WebService服務端的部署。那麼,現在就啟動Tomcat,然後:
輸入通路位址:[url]http://localhost:8080/xfire126Demo/services/MyService?wsdl[/url] ,然後go一把!
這樣,出現上上面的結果,表明測試成功了。
4、生成用戶端代碼
很郁悶,這個生成的用戶端代碼一部分跑到服務端的包裡面了。真是垃圾,rubbish!!!
但是,這就是MyEclipse的功能,我改變不了。
5、用戶端測試
下面就耐心看怎麼用這個用戶端代碼。
打開生成的代碼如下:
package wstest.client;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;
public class MyServiceClient {
private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
private Service service0;
public MyServiceClient() {
create0();
Endpoint MyServicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint"), new QName("http://server.wstest", "MyServicePortTypeLocalBinding"), "xfire.local://MyService");
endpoints.put(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint"), MyServicePortTypeLocalEndpointEP);
Endpoint MyServiceHttpPortEP = service0 .addEndpoint(new QName("http://server.wstest", "MyServiceHttpPort"), new QName("http://server.wstest", "MyServiceHttpBinding"), "http://localhost:8080/xfire126Demo/services/MyService");
endpoints.put(new QName("http://server.wstest", "MyServiceHttpPort"), MyServiceHttpPortEP);
}
public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL", e);
}
public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
return getEndpoint((endpoint));
public Collection getEndpoints() {
return endpoints.values();
private void create0() {
TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
HashMap props = new HashMap();
props.put("annotations.allow.interface", true);
AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
asf.setBindingCreationEnabled(false);
service0 = asf.create((wstest.client.MyServicePortType.class), props);
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server.wstest", "MyServiceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server.wstest", "MyServicePortTypeLocalBinding"), "urn:xfire:transport:local");
public MyServicePortType getMyServicePortTypeLocalEndpoint() {
return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest", "MyServicePortTypeLocalEndpoint")));
public MyServicePortType getMyServicePortTypeLocalEndpoint(String url) {
MyServicePortType var = getMyServicePortTypeLocalEndpoint();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
public MyServicePortType getMyServiceHttpPort() {
return ((MyServicePortType)(this).getEndpoint(new QName("http://server.wstest", "MyServiceHttpPort")));
public MyServicePortType getMyServiceHttpPort(String url) {
MyServicePortType var = getMyServiceHttpPort();
public static void main(String[] args) {
MyServiceClient client = new MyServiceClient();
//create a default service endpoint
MyServicePortType service = client.getMyServiceHttpPort();
//TODO: Add custom client code here
//
//service.yourServiceOperationHere();
System.out.println("test client completed");
System.exit(0);
}
看得很暈,不知道啥意思,但是從“TODO”标記處,我知道了:
現在就在這裡添加測試代碼吧:
String helloString = service.sayHello("熔岩");
System.out.println(helloString);
添加了很傻蛋的兩行代碼後,就可以運作起來看看測試代碼了。
運作結果如下:
您好,熔岩
test client completed
終于可以松一口氣了。完整的例子跑起來了。
6、總結
總感覺這個開發過程不爽,其實有更好的工具和開發方式:
WebService的編寫,比較麻煩的是用戶端代碼,用戶端代碼依靠人工去寫基本上是不可能的,除非你願意付出驚人的時間和精力,既便如此也得不償失。
MyEclipse的用戶端開發太差,主要是生成的用戶端代碼混亂,解決辦法是将用戶端的編寫放到單獨一個工程裡面來做。
其實,隻要服務端編寫好了,就完全可以用别的方式根據wsdl的url去生成用戶端代碼,在這裡不得不将一個強大的工具IDEA8推薦出來,IDEA8自帶WebService開發工具,插件非常強大,易用。在後面的篇幅中,我會做專門介紹,敬請關注。
當然,MyEclipse也并非一無是處,MyEclipse的服務端調試工具就很不錯,很喜歡。提高了開發效率,這也是MyEclipse的過人之處。
最後,告誡各位,即使WebService支援複雜對象參數,也不建議使用,因為資料綁定還不是那麼完美,總有些缺憾,為了保險起見,還是建議使用String作為參數最好了。