javaWeb服务详解(含源代码,测试通过,注释)
?????? javaweb服務(wù)分為兩個(gè)部分,一部分是web服務(wù)端,另一部分就是你調(diào)用的客戶(hù)端了。首先我說(shuō)下實(shí)現(xiàn)web服務(wù)的簡(jiǎn)單思路:
一、服務(wù)器端實(shí)現(xiàn):
1.添加webservice? jar包 spring支持
2.添加一個(gè)web服務(wù)
3.在實(shí)體類(lèi)和接口以及對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)中添加注解,讓它們具有公開(kāi)的一種能力
4.在spring配置文件中把具有公開(kāi)能力的服務(wù)進(jìn)行發(fā)布
詳細(xì)步驟:
使用spring完成服務(wù)器端的步驟:
?第一步:編寫(xiě)一服務(wù)接口和服務(wù)實(shí)現(xiàn)類(lèi)(包括實(shí)體類(lèi))
?第二步:公開(kāi)服務(wù)和方法
???? 前提:需要導(dǎo)入相關(guān)的jar包
???? 在實(shí)體類(lèi)中 添加注解? @XmlRootElement(name="WeatherInfo")
???? 在接口和實(shí)現(xiàn)類(lèi)中 添加注解:
????????????????? 公開(kāi)方法中添加?????? @WebMethod
???????????????? 非公開(kāi)方法中 添加??? @WebMethod(exclude=true)
???????????????????? ?
?第三步:在spring配置文件中
??? 1.頭部添加 命名空間
????????? xmlns:jaxws="http://cxf.apache.org/jaxws"
????????? http://cxf.apache.org/jaxws
?? ? ? ?? http://cxf.apache.org/schemas/jaxws.xsd
?? ?????? http://cxf.apache.org/bindings/soap
?? ?????? http://cxf.apache.org/schemas/configuration/soap.xsd???????????? ?
??? 2.定義service的bean
????? <bean id="weatherService" class="springwebService.service.impl.WeatherServiceImpl"></bean>
?? ?
??? 3.定義EndPoint (端點(diǎn))
???? <jaxws:endpoint id="wsServiceBean" implementor="#weatherService" address="/getWeather" publish="true"></jaxws:endpoint>
?? ?
?第四步:在web.xml中配置servlet
?? <servlet>
? <servlet-name>CXFServlet</servlet-name>
? <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
? <load-on-startup>1</load-on-startup>
? </servlet>
? <servlet-mapping>
?? <servlet-name>CXFServlet</servlet-name>
?? <url-pattern>/services/*</url-pattern>
? </servlet-mapping>
?第五步:測(cè)試服務(wù)發(fā)布是否成功
?在瀏覽器中輸入:
?? http://localhost:8080/spring09webService/services/getWeather?wsdl
?? 或:利用myeclipse測(cè)試 點(diǎn)擊 launch soap web service explorer(發(fā)布web程序按鈕前面)
?
//
http://localhost:8080/spring-09server/services/getWeather?wsdl
客戶(hù)端的實(shí)現(xiàn) ?
二、使用spring完成客戶(hù)端的配置從而調(diào)用服務(wù)的步驟:
第一步:生成所需的文件
??? 1.在Dos中進(jìn)入apache-cxf-2.7.6的bin目錄輸入 wsdl2java? http://localhost:8080/spring09webService/services/getWeather?wsdl? ?
第二步:.創(chuàng)建web工程,把第一步生成的實(shí)體和接口放入工程中,添加spring支持,導(dǎo)入cxf需要的jar包
第三步:編寫(xiě)spring配置文件
??????? <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
?? <property name="serviceClass" value="springwebClient.service.IWeatherService"></property>
?? <property name="address" value="http://localhost:8080/spring09webService/services/getWeather?wsdl"></property>
? </bean>
? <bean id="wsClient" class="springwebClient.service.IWeatherService" factory-bean="clientFactory" factory-method="create"></bean>
第四步:測(cè)試
??? 把 wsClient當(dāng)作服務(wù),注入到Action中,直接調(diào)用方法,獲取數(shù)據(jù)
?? 接下來(lái)看看源代碼吧,走你!!!
????? Dept的web服務(wù)
????? Emp的web服務(wù)
總結(jié)
以上是生活随笔為你收集整理的javaWeb服务详解(含源代码,测试通过,注释)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: com.sun.istack.SAXEx
- 下一篇: javaWeb服务详解(含源代码,测试通