有關WebService的概念、原理、數據發現、描述、綁定等過程、方式也不說了。這里就只關注如何快速開發出來一個通用的、易懂的Hello World例子。
以下是開發步驟:
1、創建工程
打開MyEclipse 6.5,新建一個WebService工程。如下圖
然后一路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>
<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>
2、創建WebService服務
創建兩個個包“wstest.server”和“wstest.client”,用來保存服務端和客戶端程序。然后開始創建服務端程序,如下圖
完成后,生成了一個Service的配置services.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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);
}
package wstest.server;
//Generated by MyEclipse
public class MyServiceImpl implements IMyService {
public String sayHello(String user) {
return "您好,"+user;
}
}
至此,服務端代碼已經完成。
3、測試服務端代碼
測試依賴與Servlet容器Tomcat,需要將做好的服務端打包部署到tomcat上,然后啟動。才可以進行測試。假設你已經配置了Tomcat服務器,并完成了WebService服務端的部署。那么,現在就啟動Tomcat,然后:
這樣,出現上上面的結果,表明測試成功了。
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();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}
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”標記處,我知道了:
//TODO: Add custom client code here
//
//service.yourServiceOperationHere();
現在就在這里添加測試代碼吧:
//TODO: Add custom client code here
//
//service.yourServiceOperationHere();
String helloString = service.sayHello("熔巖");
System.out.println(helloString);
添加了很傻蛋的兩行代碼后,就可以運行起來看看測試代碼了。運行結果如下:
您好,熔巖
test client completed
終于可以松一口氣了。完整的例子跑起來了。
6、總結
總感覺這個開發過程不爽,其實有更好的工具和開發方式:WebService的編寫,比較麻煩的是客戶端代碼,客戶端代碼依靠人工去寫基本上是不可能的,除非你愿意付出驚人的時間和精力,既便如此也得不償失。MyEclipse的客戶端開發太差,主要是生成的客戶端代碼混亂,解決辦法是將客戶端的編寫放到單獨一個工程里面來做。其實,只要服務端編寫好了,就完全可以用別的方式根據wsdl的url去生成客戶端代碼,在這里不得不將一個強大的工具IDEA8推薦出來,IDEA8自帶WebService開發工具,插件非常強大,易用。在后面的篇幅中,我會做專門介紹,敬請關注。當然,MyEclipse也并非一無是處,MyEclipse的服務端調試工具就很不錯,很喜歡。提高了開發效率,這也是MyEclipse的過人之處。最后,告誡各位,即使WebService支持復雜對象參數,也不建議使用,因為數據綁定還不是那么完美,總有些缺憾,為了保險起見,還是建議使用String作為參數最好了。