javaWeb服务详解(含源代码,测试通过,注释)
?????? javaweb服務分為兩個部分,一部分是web服務端,另一部分就是你調用的客戶端了。首先我說下實現web服務的簡單思路:
一、服務器端實現:
1.添加webservice? jar包 spring支持
2.添加一個web服務
3.在實體類和接口以及對應的實現類中添加注解,讓它們具有公開的一種能力
4.在spring配置文件中把具有公開能力的服務進行發布
詳細步驟:
使用spring完成服務器端的步驟:
?第一步:編寫一服務接口和服務實現類(包括實體類)
?第二步:公開服務和方法
???? 前提:需要導入相關的jar包
???? 在實體類中 添加注解? @XmlRootElement(name="WeatherInfo")
???? 在接口和實現類中 添加注解:
????????????????? 公開方法中添加?????? @WebMethod
???????????????? 非公開方法中 添加??? @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 (端點)
???? <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>
?第五步:測試服務發布是否成功
?在瀏覽器中輸入:
?? http://localhost:8080/spring09webService/services/getWeather?wsdl
?? 或:利用myeclipse測試 點擊 launch soap web service explorer(發布web程序按鈕前面)
?
//
http://localhost:8080/spring-09server/services/getWeather?wsdl
客戶端的實現 ?
二、使用spring完成客戶端的配置從而調用服務的步驟:
第一步:生成所需的文件
??? 1.在Dos中進入apache-cxf-2.7.6的bin目錄輸入 wsdl2java? http://localhost:8080/spring09webService/services/getWeather?wsdl? ?
第二步:.創建web工程,把第一步生成的實體和接口放入工程中,添加spring支持,導入cxf需要的jar包
第三步:編寫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>
第四步:測試
??? 把 wsClient當作服務,注入到Action中,直接調用方法,獲取數據
?? 接下來看看源代碼吧,走你!!!
????? Dept的web服務
????? Emp的web服務
總結
以上是生活随笔為你收集整理的javaWeb服务详解(含源代码,测试通过,注释)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: com.sun.istack.SAXEx
- 下一篇: javaWeb服务详解(含源代码,测试通