1、開發webservice接口的方式web
一、使用jdk開發spring
二、使用第三方工具,如cxf、shiro等tomcat
2、使用jdk開發webservice接口以及調用app
首先定義一個天氣預報的接口,Weather工具
@WebService public interface Weather { String queryWeather(); }
定義一個實現類,實現該接口url
@WebService public class WeatherImpl implements Weather{ public String queryWeather() { return "今日天氣爲晴,偏北風二到三級"; } }
寫一個普通的類,使其繼承自spring的上下文監聽器,並在初始化方法中發佈接口,這樣在容器啓動時自動會發布spa
public class MyListener extends ContextLoaderListener{ public void contextInitialized(ServletContextEvent event) { String address="http://localhost:8080/weather"; Endpoint.publish(address, new WeatherImpl()); super.contextInitialized(event); } }
在web容器中設置該監聽器code
<listener> <listener-class>springframe.listener.MyListener</listener-class> </listener>
啓動容器(若是啓動過程當中報錯:出現相似blog
則說明使用的jdk版本太低,請使用jdk1.6或更高),訪問http://localhost:8080/weather,結果以下:繼承
表示發佈成功。
接下來是如何調用一個發佈的webservice接口
新建一個項目test_p
選中項目,鼠標右鍵,選擇NEW,選擇other,找到web service client,next,在彈出的框中選擇WSDL URL,病輸入wsdl的url,這裏是http://localhost:8080/weather?wsdl,next,finish
而後爲咱們生成了一堆類
不過咱們只需用到最後兩個,Weather_service和Weather
下面寫一個main方法
public static void main(String[] args) { Weather_Service factory=new Weather_Service(); Weather wea=factory.getWeatherImplPort(); System.out.println(wea.queryWeather()); }
執行,會輸出以下的結果:
表明調用成功。
注意:若是webservice用到的端口最好與tomcat的訪問端口不同,不然,會出現沒法訪問項目。
3、使用cxf開發webservice接口
該方法尚在研究中,敬請期待。。。