① 如何用java調用cxf webservice介面
java調用cxf webservice介面可以自動生成的。
一:先下載CXF程序
二:在命令行下進入cxf下的bin目錄
三:執行下面版命令:
wsdl2java -frontend jaxws21 -p com.webservice.cxfclient.sayhello -d src -all http://172.16.66.6:10000/test/sayHello?wsdl
四:點擊權com.webservice.cxfclient右鍵,把生成的文件導入到工程中來。
② 如何將 webservice 部署 osb
1、jar包沖突
對照著wsdl,很快就手寫好了客戶端和服務端的代碼,包括介面和領域模型。然後配一配cxf和spring的集成,搭了一個樁,一下就弄好了
可是將這部分代碼放到項目里驗證,用cxf2.5.2.jar替換了cxf2.0.13,就拋異常了:java.lang.IncompatibleClassChangeError: Implementing class
2、jar包升級後,舊功能不好用
好不容易定位出來,原來是2個版本的cxf所依賴的jar包是不同的,將相關的jar包也替換之後,發布成功了。可是系統中原來的幾個老功能壞了,看來cxf的版本不能隨便替換
3、jar包回退後,新功能不好用
於是又把cxf2.0.13替換了回去,這次老功能是可以了,我們新開發的web service也發布起來了,可是調用的時候報異常:
java.lang.IllegalArgumentException: object is not an instance of declaring class
後來發現,是cxf2.0.13和cxf2.5.2的配置文件的寫法不同,改了一下以後,調用成功了
這3個問題的解決方法,見http://kyfxbl.iteye.com/blog/1467354
4、spring注入異常
當我們往服務端web service里注入業務邏輯組件,或者往業務邏輯組件里注入web service客戶端介面時,依賴注入經常發生一些奇怪的問題,經常聲明的bean找不到什麼的
最後通過各種方式解決了,詳情見http://kyfxbl.iteye.com/blog/1470923
不過還是遺留了一些問題沒想通,以後再慢慢研究
5、根據wsdl手寫的代碼不好使
由於客戶提供的wsdl比較復雜,手寫的代碼聯調不通,最終還是只能用wsdl2java工具來做反向生成
6、用wsdl2java生成的代碼,發布失敗
生成了代碼以後,發布又失敗了,報了以下異常
Schema name conflict in collection. Namespace:xxxxxx
又花了1天時間解決了這個問題,詳情見http://kyfxbl.iteye.com/blog/1483953
7、https證書問題
經過上面幾個步驟,web service是發布起來了,通過http可以調通,但是客戶要求用https協議來走,而且要求是雙向認證。大家對https都不熟悉,這塊花了3天時間才做出來,可以說是最困難的一步
8、實際聯調時,客戶端校驗不通過
搞定了https協議問題之後,終於能業務聯調了,可是失敗了,客戶系統那邊報
Error code 'BEA-382505'. OSB Validate action failed validation: Expected element 'requestHeader' instead of 'requestBody' here in element XXX
定位發現原來是我們發送請求的時候,只填寫了RequestBody的內容,以為不重要就沒有填寫RequestHead的內容,所以客戶那邊校驗沒通過
把這塊改完以後,就OK了
總結一下:
1、開發新功能時,盡量在現有的jar包基礎上開發。因為如果用與實際環境不同的jar包,很難保證不會出現jar包沖突,或者影響現有功能的問題
2、根據wsdl手寫代碼不是那麼容易的,對於比較復雜的wsdl,還是用wsdl2java工具會比較保險
3、用spring配置文件來配置cxf還是很容易的,但是當集成spring和cxf的時候,依賴注入要特別小心,會有一些很異常的情況。這塊的原因我現在還沒有搞清楚
③ 如何用WSDL4J構建Spring+AXIS自動創建的Web service對應客戶端
pache項目WSDL4J則是根據WSDL的XML文檔就可以自動生成Web service的客戶端代碼,這樣的話,構建web service服務端和客戶端都顯得沒有多少手工編碼了。其實WSDL4J也是可以生成服務端代碼的,可是手工編寫WSDL還不如用AXIS自動生成來得快。
用WSDL4J創建客戶端代碼的命令如下:
java -cp %AXIS_CLASSPATH% java org.apache.axis.wsdl.WSDL2Java -o DIR_NAME -p PACKAGE_NAME me?wsdl
其中AXIS_CLASSPATH應該包括axis.jar,commons-discovery.jar,commons-logging.jar,jaxrpc.jar,log4j.jar,saaj.jar,wsdl4j.jar,activation.jar,mail.jar。
該命令會在所指定目錄下創建JAVA包,裡面有[web_service]Locator.java, [web_service].java, [web_service]PortType.java, [web_service]SOAP11BindingStub.java,[web_service]SOAP12BindingStub.java,以及方法中所有涉及的參數bean等。
④ springmvc框架如何實現像webservice一樣通過wsdl2Java生成客戶端代碼
在用到web service時,如果是調用自己開發的web service還好,可以有一個方法的介面或參數對象實體類什麼的,但如果是調用第三方的服務,除了得到web service服務地址的wsdl文檔外,沒有任何實質性編碼的東西,寫原生調用太麻煩,這時就需要自己寫一個方法的介面類。
這個,目前已經有比較成熟的工具,可以自動生成,下面作一下簡單的介紹。
apache的wsdl2java工具,目前貌似CXF和axis各有一套,使用方式大同小異,但生成的代碼會有所區別,這時說的是CXF提供的wsdl2java工具。
wsdl2java用法:
wsdl2java -p com -d src -all aa.wsdl
-p 指定其wsdl的命名空間,也就是要生成代碼的包名:
-d 指定要產生代碼所在目錄
-client 生成客戶端測試web service的代碼
-server 生成伺服器啟動web service的代碼
-impl 生成web service的實現代碼
-ant 生成build.xml文件
-all 生成所有開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
詳細用法見:http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html
一開始使用了CXF的這套工具,發現不是很方便,畢竟依賴CXF提供的工具jar包,個人不是很喜歡用。
後來發現JDK居然也自帶了對web service生成java代碼的功能,貌似1.6版本開始的,試用後發現效果十分不錯,果斷投入它的懷抱。下面作下簡單的介紹,以備忘。
打開jdk下的bin目錄 看下能否找到"wsimport.exe"這個文件
一般情況下都會有
如果沒有則說明你的JDK不支持這個功能
然後在DOS窗口下輸入wsimport 敲回車
如果提示錯誤的話 說明你的JDK環境變數還沒有配好
一句話總結,如果你在dos窗口下輸入wsimport正常,就可以
如果OK的話 輸入以下命令就可以將wsdl文件生成java文件了
wsimporthttp://127.0.0.1/TicketMobile/services/Cococ?wsdl -keep -p com.llg.ws2 -s g:/ws
參數說明
wsimport 這個是必須的 該工具的名稱
http://127.0.0.1/TicketMobile/services/Cococ?wsdl wsdl文件
-keep 是否生成源文件
-p com.llg.ws2 生成後的java包名
-s g:/ws 生成後放哪個目錄
但是前幾天,在對一個第三方提供的web service使用該工具生成java代碼的時候,居然出錯了,網上查找後終於找到原因。
使用cxf wsdl2java或javax wsimport工具的時候,可能會遇到關於生成的Response類文件名沖突的問題
Console代碼
WSDLToJavaError:ThrownbyJAXB:Aclass/interfacewiththesamename"***"isalreadyinuse..
目前可選擇的方案:
apache的wsdl2java工具,使用-autoNameResolution自動處理
wsdl2java -autoNameResolutionhttp://hello.joy2everyone.com/yourWebService?wsdl
JDK自帶的工具
wsimport -p com.test.client -keephttp://hello.joy2everyone.com/yourWebService?wsdl-B-XautoNameResolution
2.如果web service是己方開發的,可以修改代碼,使用自定義bindings,詳細可看sun webservice文檔
例如:
Java代碼
@WebMethod
@WebResult(name="response")
(@WebParam(name="request")ValidateCCRequestrequest);
這個情況下定義的方法名,如果使用工具生成客戶端代碼,很可能存在Response沖突,因為定義的wsdl中會有一個關於介面方法的message
<wsdl:message name="validateCCResponse">
</wsdl:message>
方法名定義的message與介面定義返回的ValidateCCResponse,在工具生成客戶端代碼時就會產生命名沖突。
但是通過更改介面方法名為:
Java代碼
@WebMethod
@WebResult(name="response")
(@WebParam(name="request")ValidateCCRequestrequest);
即可解決該沖突,
⑤ 求關於JAVA WEB的英文資料
真的嗎??
那你去 http://en.wikipedia.org/wiki/Main_Page
然後在左邊的搜索欄搜索 JAVA WEB
會出來一溜關於JAVA WEB的 我給你的是第一個
關於JAVA WEB START 的
去這個網站一看就一目瞭然了
http://en.wikipedia.org/wiki/Java_web_start
一定比我說的好
我給的網站差不多有20000字。。
這里還有 一小篇
Java Web Services Tools
by Madan Revoor, in Editorials - Sat, Feb 24th 2007 00:00 PDT
Web Service tools in Java are into their third generation now. Web services were introced with the hype of a loosely coupled technology for inter-connecting disparate endpoint systems. But they are, in fact, suffering from their tight coupling based on the WSDL data specifications and data types. Most of the current tools offer quick solutions to expose existing code as web services, but none offer a simple, yet intuitive and full-featured client.
--------------------------------------------------------------------------------
Copyright notice: All reader-contributed material on freshmeat.net is the property and responsibility of its author; for reprint rights, please contact the author directly.
--------------------------------------------------------------------------------
This article focuses on the Java-based Web Service APIs and compares some of the most commonly available tools, like Axis1.x, Axis2, XFire, WSIF, and SAAJ, along with stating the necessity for a Java Reflection-like API for Web Services based on WSDL that can retrieve the port type, operation name, service namespace, service style, method parameters, return types, Java data mappings, etc.
The first question that might pop into everyone's mind might be about Apache WSIF. Though WSIF mainly focuses on using the meta-information available in the WSDL, it doesn't offer a solution to directly work with the actual underlying XML message for a SOAP-based WSDL service. Also, it can look up the default port type and create default input and output messages for a specific operation, yet it does not offer a full-fledged Reflection API-like lookup for any and every piece of meta-information in the WSDL. Such a dynamic lookup can be used to check for available operations and exposed services at runtime and invoke them appropriately.
Also, as organizations look forward to web services as high-volume, transaction-capable, reliable, and interoperable workhorses, the ability to turn off Java object binding becomes highly desirable. This would be handy especially in scenarios in which the application is just a broker to a web service, and the actual data consumers are elsewhere. WSIF also depends on stubs generated from the Apache Axis WSDL2Java tool.
Apache SOAP was based on DOM and had to maintain the entire XML tree in memory, and hence was not suited to handle data-intensive services. Though Axis1.x (which was developed from Apache SOAP) was based on SAX and has enjoyed huge success in many corporate procts, it also has a serious flaw with web services that serve out huge XML content. It failed miserably in handling huge XML-based transactions, since the SAX-based parsing was tightly coupled to Java object binding. Thus, even though the SAX parsing could handle huge XML data efficiently, the data had to be stored in memory by generating and mapping to POJOs via its type mapping registry mechanism, coupled with its serializers and deserializers. It did not allow for a direct method to deal with the XML content transparently. There was no way one could turn off the generation of objects and deal with the XML content in a memory-efficient manner. Though it offered handlers and handler chaining mechanisms that could subscribe to receive the XML request/response message content, the object binding was mandatory, and each handler was presented with its own local of the message, which all added up to memory woes and hindered performance. However, Axis1.x has a very good WSDL parser tool called WSDL2Java, though it does not expose a complete set of APIs that clients can use to look up datatypes, method parameters, and their XML name tags or return parameters.
Axis2 is a complete re-architecture of Axis1.x from the ground up to address these issues. It is based on the new StAX (Streaming API for XML) and is poised to perform better than Axis1.x because of the advantages offered by StAX-based pull parsing. It provides more XML-oriented approaches and has a good Object Model named AXIOM that revolves around StAX to provide partial or deferred building support. This dependence on StAX allows it to boast of a low memory footprint. It also does not depend on storing the entire XML content in object forms in memory. It has support for plugable data bindings, and either SOAP or Java transport mechanisms can be used. However, one flaw with Axis2 is that it also does not allow a transparent and efficient method to dynamically invoke web services like WSIF, nor any lookup methods based on the WSDL.
SAAJ1.3 from Sun is a reliable SOAP messaging API. However, it does not deal with other Java-based transport mechanisms like JMS, RPC, etc., as Axis2 does. It's surprising that SAAJ offers much better performance than most of the Axis clients, though it offers a DOM-based API.
XFire has been developed by the Codehaus team as an alternative option to Axis1.x (at the same time that Axis2 was envisioned and drafted) to improve performance, along with the goals of catering to SOAP1.2 and being WS-I 1.1 compliant. It has an embedded HTTP server (Embedded XFire HTTP Service) and can be used to easily expose Java methods as web service endpoints with easy integration in Spring. It is also based on the current generation of pull parsers and incorporates StAX. However, XFire does not support encoded RPC-style services, and is yet to support dynamic clients (which might be featured in the next release).
Conclusion
Though the performance aspects of current Java web service tools are pepped up by the use of XML pull-based parsers like StAX and a more XML-oriented approach with optional object endpoints, the missing link in Java-based web service tools is good Reflection API-like dynamic WSDL parsing capability. It could be used to provide a handle to the meta-information available on the WSDL at runtime for clients. A mix of WSIF-like (with a more extensive WSDL parser) and Axis2-like StAX-based functionality is desired. It should not only offer a lot of possibilities like handling of small changes on the web service specification (WSDL) at runtime (an open issue would be to decide which changes are small and which can be handled), but empower clients at times to even ward off a complete rebuild and code changes. Another great feature would be to allow the clients to choose between options to turn on/off the WSDL parser and cache the WSDL metadata, as it need not affect the performance every time a call to the WSDL reflection API is made. This would be useful when the web service definition is not static and is in development or re-evolving stages, so that clients do not need to wait for the final release, but can continue with their development and testing. It would also lend much flexibility to the client design, as it could transparently decide which WSDL-exposed operation to choose or which transport protocol to use at runtime. Let's hope such a tool with the best features of all existing APIs will soon be released.
⑥ Java學習路線
java的學習內容很多,涵蓋較多方面,這里大致分為幾個階段提供給你參考。
一、預科學習:
HTML5:HTML5標簽入門、HTML5表格、表單
CSS3:CSS3選擇器和簡單屬性、CSS3定位和布局、CSS3復雜選擇器和高級屬性
資料庫:mysql資料庫安裝和數據操作、約束和簡單查詢、復雜查詢、資料庫設計、oracle的安裝與數據操作、oracle與mysql的對比學習
二、JavaSE
Java語言基礎、程序邏輯:環境配置和第一個語言程序-HelloWorld 變數運算符 條件和循環 方法和數組
Java面向對象:面向對象入門 面向對象應用_管理系統類 Java常用類、String相關、演算法相關 面向對象深入(重載、this、static )繼承(重寫、super、初始化順序) 多態(抽象類和介面、final、克隆和比較介面 設計模式、對象和類的生命周期)
API:異常、日誌 集合 集合工具類和泛型 IO JDBC基礎線程 網路編程 反射 NIO Junit
Java面向對象思想:設計模式 面向對象原則
Java底層理論:集合底層 性能監控工具 反編 JUC
三、Java web
web基礎:TOMCAT/WEB程序結構/HTTP協議 Servlet基礎入門、servlet作用域(cookie、session、ServletContext)、 Cookie和Session 、Servlet的交互/jsP原理及運用、 JavaBean/EL/JSTL/MVC思想 、JSP+Servlet+JDBC綜合練習、Session購物車案例/驗證碼/防止表單重復提交、監聽器過濾器
第三方工具包:連接池、事務、分頁、文件上傳下載、Dom4j/Log4j/Log back
JavaScript和jQuery框架技術:JS入門和DOM基礎 、DOM模型深入 、jQ基礎、 jQ操作DOM
MVC動態Web開發技術:自定義MVC框架、DAO框架、前端框架(layUI)
Web開發高級運用:tomcat server伺服器配置 、nginx使用、 jetty配置
網路編程:網路原理、HTTP協議基礎、Linux操作系統、雲服務搭建
四、SSM框架
Spring框架、SpringMVC框架、MyBatis框架:mybatis入門、 配置文件詳解和動態sql的使用、 mybatis管理關系映射和延遲載入、 查詢緩存和逆向工程 、Spring入門和集成、myBatis SpringMVC入門 、SSM集成、 Spring配置詳解 、Spring AOP、 Spring事務配置 、SpringMVC高級功能 、SpringMVC原理
五、前沿技術
高可用、高並發、高擴展:Spring Boot 、緩存 、分布式 、全文索引、 服務中間件、 myCat、 雲服務 、人臉識別 、語言識別 、JVM底層+優化
希望能夠幫到你!!!
⑦ Java開發的學習內容都有哪些
java學習內容你可以參考以下:
一、JavaEE基礎
Java基礎語法、面向對象、核心類庫、集合、異常、IO、線程、JDK新特性;
二、JavaWeb開發
前端技術、資料庫、JAVA資料庫操作、軟體伺服器及伺服器相關技術、動態網頁JSP、AJAX;
三、Java高級框架
SpringMVC、MyBatis、Spring、MySQL高級、Linux&Redis&Nginx、Maven;
四、大型微服務分布式項目實戰
SpringBoot、SpringCloud、Git、Dubbo、Zookeeper、AngularJS、Spring Security、BCrypt加密、FastDFS、分布式事務處理、Redis Cluster、Solr、SolrCloud、Freemarker、JMS、簡訊發送平台、SSO解決方案、CORS、Twitter的Snowflake、SpringTask、Maven Profile、MongoDB簡介、MyCat、Docker、Jenkins;
五、微服務大型項目實戰
⑧ 誰遇到過在Tomcat下面部署的CXF出現這樣的錯誤
詳細介紹了在tomcat容器中創建、發布和訪問CXF服務的步驟和各種方法。一、伺服器端1.添加CXF包1)基本包:commons-logging-1.1.1.jargeronimo-activation_1.1_spec-1.0.2.jargeronimo-annotation_1.0_spec-1.1.1.jargeronimo-javamail_1.4_spec-1.6.jargeronimo-jaxws_2.1_spec-1.0.jargeronimo-servlet_2.5_spec-1.2.jargeronimo-stax-api_1.0_spec-1.0.1.jargeronimo-ws-metadata_2.0_spec-1.1.2.jarjaxb-api-2.1.jarjaxb-impl-2.1.12.jarjetty-6.1.21.jarjetty-util-6.1.21.jarneethi-2.0.4.jarsaaj-api-1.3.jarsaaj-impl-1.3.2.jarwsdl4j-1.6.2.jarwstx-asl-3.2.8.jarxml-resolver-1.2.jarXmlSchema-1.4.5.jar2)jsf和jstl(非必要,用MyEclipse創建WebProject時會自動加入,無須再添加)jsf-api.jarjsf-impl.jarjstl-1.2.jar3)cxfcxf-2.2.4.jar4)Springjars,為XMLConfiguration添加Spring支持。aopalliance-1.0.jarspring-core-2.5.5.jarspring-beans-2.5.5.jarspring-context-2.5.5.jarspring-web-2.5.5.jar2.服務介面及實現1)介面類,如HelloWorld.java:packagecxf.test;importjavax.jws.WebService;@{//一個簡單的方法,返回一個字元串Stringsay(Stringhello);}2)實現類,如HelloWorldImpl.java:packagecxf.test;importjavax.jws.WebService;//WebService實現類.//使用@WebService指向Interface定義類即可.@WebService(endpointInterface="cxf.test.HelloWorld"){publicStringsay(Stringhello){return"hello"+hello;}}3.服務配置1)web.xmlcontextConfigLocationWEB-INF/beans.xmlorg.springframework.web.context..apache.cxf.transport.servlet.CXFServletCXFServlet/services/*2)beans.xml註:服務無需諸如index.jsp之類的配置,MyEclipse自動加入的可刪除,對服務及配置無任何影響。4.服務發布(到Tomcat)方法一:通過MyEclipse發布點擊工具欄圖標,或者右鍵單擊項目→MyEclipse→。方法二:使用Ant。二、客戶端創建WebProject,執行下列步驟即可。無須設置web.xml。1.添加CXF包針對不同的方法,需要的包不盡相同。如下面「3.服務調用」的方法三,需要如下包:commons-logging-1.1.1.jarcxf-2.2.4.jarneethi-2.0.4.jarspring-core-2.5.5.jarspring-beans-2.5.5.jarspring-context-2.5.5.jarwsdl4j-1.6.2.jarXmlSchema-1.4.5.jar2.獲取服務服務介面類(類似於C/C++中的.h頭文件)方法一:直接從原項目中這當然是最簡單的方法,也是最「難」的方法(如果服務不是自己做的,顯然沒法獲得)。方法二:從wsdl文檔中生成。需要先安裝cxf程序包。生成步驟如下:1)安裝cxf,設置環境變數,如:D:/Apache/apache-cxf-2.2.4;同時,PATH後加上「;%CXF_HOME%/bin」(可選)。wsdl2java的用法如下:wsdl2java–p包名–d目錄名wsdl路徑如:wsdl2java–pdemo.service.client–de:/srchtt://localhost:8080/helloWorld?wsdl-p指定其wsdl的命名空間,也就是要生成代碼的包名-d指定要產生代碼所在目錄-client生成客戶端測試webservice的代碼-server生成伺服器啟動webservice的代碼-impl生成webservice的實現代碼-ant生成build.xml文件-compile生成代碼後編譯-quient靜默模式,不輸出警告與錯誤信息-all生成所有開始端點代碼:types,serviceproxy,serviceinterface,servermainline,clientmainline,implementationobject,andanAntbuild.xmlfile.2)執行wsdl2java批處理程序,如:wsdl2java-pcxf.test-dd:/src-{_NAME=newQName("","HelloWorld");//首參為介面實現類包名的反綴privatestaticfinalQNamePORT_NAME=newQName("","HelloWorldPort");privateClient(){}publicstaticvoidmain(Stringargs[])throwsException{Serviceservice=Service.create(SERVICE_NAME);//="HelloWorld";//AddaporttotheServiceservice.addPort(PORT_NAME,SOAPBinding.SOAP11HTTP_BINDING,endpointAddress);HelloWorldhw=service.getPort(HelloWorld.class);System.out.println(hw.say("World"));}}方法二:使用較下層的代碼更加精確的控製程序的行為,如:packagecxf.test;importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;importcxf.test.HelloWorld;//{privateClient(){}publicstaticvoidmain(Stringargs[])throwsException{=newJaxWsProxyFactoryBean();factoryBean.getInInterceptors().add(newLoggingInInterceptor());(可選)factoryBean.getOutInterceptors().add(newLoggingOutInterceptor());(可選)factoryBean.setServiceClass(cxf.test.HelloWorld.class);factoryBean.setAddress("HelloWorld");HelloWorldclient=(HelloWorld)factoryBean.create();System.out.println(client.say("God"));System.exit(0);}}備註:LoggingInInterceptor和LoggingOutInterceptor是日誌攔截器,用於輸入和輸出時顯示日誌。使用與否並不影響程序的行為。方法三:使用Spring,例如:packagecxf.test;importorg.springframework.context.support.;importcxf.test.HelloWorld;//{privateClient(){}publicstaticvoidmain(Stringargs[])throwsException{context=new(newString[]{"cxf/test/client-beans.xml"});HelloWorldclient=(HelloWorld)context.getBean("client");Stringresponse=client.say("Joe");System.out.println("Response:"+response);System.exit(0);}}注意:要想使用Spring來完成,在cxf.test包中必須有client-beans.xml存在,內容如下:4.執行RunAsJavaApplication
⑨ 如何在Tomcat中創建,發布和訪問基於CXF的服務
詳細介紹了在tomcat容器中創建、發布和訪問CXF服務的步驟和各種方法。
一、伺服器端
1.添加CXF包
1)基本包:
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.6.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-servlet_2.5_spec-1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
xml-resolver-1.2.jar
XmlSchema-1.4.5.jar
2)jsf和jstl(非必要,用MyEclipse創建Web Project時會自動加入,無須再添加)
jsf-api.jar
jsf-impl.jar
jstl-1.2.jar
3)cxf
cxf-2.2.4.jar
4)Spring jars,為XML Configuration添加Spring支持。
aopalliance-1.0.jar
spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-web-2.5.5.jar
2.服務介面及實現
1)介面類,如HelloWorld.java:
package cxf.test;
import javax.jws.WebService;
@WebService
public interface HelloWorld
{
// 一個簡單的方法,返回一個字元串
String say(String hello);
}
2)實現類,如HelloWorldImpl.java:
package cxf.test;
import javax.jws.WebService;
// WebService實現類.
// 使用@WebService指向Interface定義類即可.
@WebService(endpointInterface = "cxf.test.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
public String say(String hello)
{
return "hello " + hello;
}
}
3.服務配置
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
2)beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="webServiceHelloWorld"
address="/HelloWorld"
implementor="cxf.test.HelloWorldImpl"/>
</beans>
註:服務無需諸如index.jsp之類的配置,MyEclipse自動加入的可刪除,對服務及配置無任何影響。
4.服務發布(到Tomcat)
方法一:通過MyEclipse發布
點擊工具欄 圖標,或者右鍵單擊項目 → MyEclipse → Add and Remove Project Deployments。
方法二:使用Ant。
二、客戶端
創建Web Project,執行下列步驟即可。無須設置web.xml。
1.添加CXF包
針對不同的方法,需要的包不盡相同。如下面「3.服務調用」的方法三,需要如下包:
commons-logging-1.1.1.jar
cxf-2.2.4.jar
neethi-2.0.4.jar
spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar
2.獲取服務服務介面類(類似於C/C++中的.h頭文件)
方法一:直接從原項目中
這當然是最簡單的方法,也是最「難」的方法(如果服務不是自己做的,顯然沒法獲得)。
方法二:從wsdl文檔中生成。
需要先安裝cxf程序包。生成步驟如下:
1) 安裝cxf,設置環境變數,如:D:/Apache/apache-cxf-2.2.4;同時,PATH後加上「;%CXF_HOME%/bin」(可選)。wsdl2java的用法如下:
wsdl2java –p 包名 –d 目錄名 wsdl路徑
如:wsdl2java –p demo.service.client –d e:/src htt://localhost:8080/helloWorld?wsdl
-p 指定其wsdl的命名空間,也就是要生成代碼的包名
-d 指定要產生代碼所在目錄
-client 生成客戶端測試web service的代碼
-server 生成伺服器啟動web service的代碼
-impl 生成web service的實現代碼
-ant 生成build.xml文件
-compile 生成代碼後編譯
-quient 靜默模式,不輸出警告與錯誤信息
-all 生成所有開始端點代碼:types,service proxy,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
2) 執行wsdl2java批處理程序,如:
wsdl2java -p cxf.test -d d:/src -server http://localhost:8080/CXFTomcat/services/ HelloWorld?wsdl
3) 將java介面類導入項目。
上一步生成的java類文件很多,一般的應用中只要將說明介面的那個類文件導入項目即可,如上例生成的HelloWorld.java文件。
3.服務調用
方法一:使用jws的高層封裝,如:
package cxf.test;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import cxf.test.HelloWorld; // necessary
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://test.cxf/", "HelloWorld"); // 首參為介面實現類包名的反綴
private static final QName PORT_NAME
= new QName("http://test.cxf/", "HelloWorldPort");
private Client() { }
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:8080/CXFTomcat/services/HelloWorld";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld hw = service.getPort(HelloWorld.class);
System.out.println(hw.say("World"));
}
}
方法二:使用較下層的代碼更加精確的控製程序的行為,如:
package cxf.test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cxf.test.HelloWorld; // necessary
public final class Client {
private Client() { }
public static void main(String args[]) throws Exception {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());(可選)
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());(可選)
factoryBean.setServiceClass(cxf.test.HelloWorld.class);
factoryBean.setAddress("http://localhost:8080/CXFTomcat/services/HelloWorld");
HelloWorld client = (HelloWorld)factoryBean.create();
System.out.println(client.say("God"));
System.exit(0);
}
}
備註:LoggingInInterceptor和LoggingOutInterceptor是日誌攔截器,用於輸入和輸出時顯示日誌。使用與否並不影響程序的行為。
方法三:使用Spring,例如:
package cxf.test;
import org.springframework.context.support.;
import cxf.test.HelloWorld; // necessary
public final class Client {
private Client() { }
public static void main(String args[]) throws Exception {
context = new (new String[] {"cxf/test/client-beans.xml"});
HelloWorld client = (HelloWorld)context.getBean("client");
String response = client.say("Joe");
System.out.println("Response: " + response);
System.exit(0);
}
}
注意:要想使用Spring來完成,在cxf.test包中必須有client-beans.xml存在,內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="cxf.test.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="cxf.test.HelloWorld"/>
<property name="address" value="http://localhost:8080/CXFTomcat/services/HelloWorld"/>
</bean>
</beans>
4.執行
Run As Java Application
⑩ 學習Java的最佳路線
說到學習Java,我們今天就來說說Java開發需要學習的內容,說說java學習路線,說說Java學習的課程內容。
Java學習大致分為四大階段,具體內容如下:
第一階段——Java基礎
Java的學習內容從計算機基本概念,DOS命令開始,為你入門編程語言掃盲,什麼是程序,如何配置Java開發環境,Java編程的過程是怎樣的,Java有什麼物特點,程序是如何運行的,這些你都可以在這里得到答案。
Java編程的基礎語法,共分為10個知識點,我們將學習變數,基本數據類型,進制,轉義字元,運算符,分支語句和循環語句等,以達到訓練基礎語法和邏輯能力的目的。還有對數組、面向對象和異常處理等。
第二階段——JavaWeb
Web前端開發基礎和框架、Servlet和JSP在Web後端的應用、Web後端開發相關專題、MVC和分層架構以及項目開發流程及CASE工具的使用等。
第三階段——Java框架
框架是程序中另一種存儲數據的方式,比直接使用數組來存儲更加的靈活,在項目中應用十分廣泛。同時,框架整合開發(SSH/SSS)、RESTful架構和移動端介面設計、第三方介面和在線支付功能、網站安全和Spring Security應用實戰、復雜用戶交互處理和Spring Web Flow的應用、MyBatis的應用和SSM整合,我們將深入研究其中涉及到的數據結構和演算法,對學員的技術深度有了一個質的提升。
第四階段——Java+雲數據
億級並發架構演進、Linux基礎、搭建tomcat環境以實戰演練,企業真實項目供學員應用學習,進行知識體系的「二次學習」。
其實學習Java技術,大可參考此學習路線,該學習路線對從零基礎小白到Java初級開發工程師,Java高級開發工程師,後面的Java大神級開發工程師都有一個明確清晰的指導。