日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

XFire WebService开发快速起步

發(fā)布時(shí)間:2025/3/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 XFire WebService开发快速起步 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
XFire WebService開發(fā)快速起步 環(huán)境: XFire-1.2.6 JDK1.5 MyEclipse 6.5 Tomcat-5.5.27 Windows XP Professional簡(jiǎn)體中文版 軟件下載地址: [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] 有關(guān)WebService的概念、原理、數(shù)據(jù)發(fā)現(xiàn)、描述、綁定等過程、方式也不說了。這里就只關(guān)注如何快速開發(fā)出來一個(gè)通用的、易懂的Hello World例子。 以下是開發(fā)步驟: 1、創(chuàng)建工程 ? 打開MyEclipse 6.5,新建一個(gè)WebService工程。如下圖 然后一路next,直到完成。 創(chuàng)建完成后,打開生成的web.xml文件,可以看到,XFire已經(jīng)配置好了。 <?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、創(chuàng)建WebService服務(wù) ? 創(chuàng)建兩個(gè)個(gè)包“wstest.server”和“wstest.client”,用來保存服務(wù)端和客戶端程序。然后開始創(chuàng)建服務(wù)端程序,如下圖 完成后,生成了一個(gè)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> 也生成了接口和默認(rèn)實(shí)現(xiàn),改寫后如下: 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;
??}
????
} 至此,服務(wù)端代碼已經(jīng)完成。 3、測(cè)試服務(wù)端代碼 測(cè)試依賴與Servlet容器Tomcat,需要將做好的服務(wù)端打包部署到tomcat上,然后啟動(dòng)。才可以進(jìn)行測(cè)試。假設(shè)你已經(jīng)配置了Tomcat服務(wù)器,并完成了WebService服務(wù)端的部署。那么,現(xiàn)在就啟動(dòng)Tomcat,然后: 輸入訪問地址:[url]http://localhost:8080/xfire126Demo/services/MyService?wsdl[/url]? ,然后go一把! 這樣,出現(xiàn)上上面的結(jié)果,表明測(cè)試成功了。 4、生成客戶端代碼 很郁悶,這個(gè)生成的客戶端代碼一部分跑到服務(wù)端的包里面了。真是垃圾,rubbish!!! 但是,這就是MyEclipse的功能,我改變不了。 5、客戶端測(cè)試 下面就耐心看怎么用這個(gè)客戶端代碼。 打開生成的代碼如下:
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”標(biāo)記處,我知道了: //TODO: Add custom client code here
????????????????????//
????????????????????//service.yourServiceOperationHere();
現(xiàn)在就在這里添加測(cè)試代碼吧: //TODO: Add custom client code here
????????????????????//
????????????????????//service.yourServiceOperationHere();
????????????????String helloString = service.sayHello("熔巖");
????????????????System.out.println(helloString); 添加了很傻蛋的兩行代碼后,就可以運(yùn)行起來看看測(cè)試代碼了。 運(yùn)行結(jié)果如下: 您好,熔巖
test client completed 終于可以松一口氣了。完整的例子跑起來了。 6、總結(jié) 總感覺這個(gè)開發(fā)過程不爽,其實(shí)有更好的工具和開發(fā)方式: WebService的編寫,比較麻煩的是客戶端代碼,客戶端代碼依靠人工去寫基本上是不可能的,除非你愿意付出驚人的時(shí)間和精力,既便如此也得不償失。 MyEclipse的客戶端開發(fā)太差,主要是生成的客戶端代碼混亂,解決辦法是將客戶端的編寫放到單獨(dú)一個(gè)工程里面來做。 其實(shí),只要服務(wù)端編寫好了,就完全可以用別的方式根據(jù)wsdl的url去生成客戶端代碼,在這里不得不將一個(gè)強(qiáng)大的工具IDEA8推薦出來,IDEA8自帶WebService開發(fā)工具,插件非常強(qiáng)大,易用。在后面的篇幅中,我會(huì)做專門介紹,敬請(qǐng)關(guān)注。 當(dāng)然,MyEclipse也并非一無是處,MyEclipse的服務(wù)端調(diào)試工具就很不錯(cuò),很喜歡。提高了開發(fā)效率,這也是MyEclipse的過人之處。 最后,告誡各位,即使WebService支持復(fù)雜對(duì)象參數(shù),也不建議使用,因?yàn)閿?shù)據(jù)綁定還不是那么完美,總有些缺憾,為了保險(xiǎn)起見,還是建議使用String作為參數(shù)最好了。

本文出自 “熔 巖” 博客,轉(zhuǎn)載請(qǐng)與作者聯(lián)系!


總結(jié)

以上是生活随笔為你收集整理的XFire WebService开发快速起步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。