webservice接口的開發和調用


一、開發webservice接口的方式

1、使用jdk開發

2、使用第三方工具,如cxf、shiro等

二、使用jdk開發webservice接口以及調用

首先定義一個天氣預報的接口,Weather

@WebService
public interface Weather {
    String queryWeather();
}

定義一個實現類,實現該接口

@WebService
public class WeatherImpl implements Weather{
    public String queryWeather() {
        return "今日天氣為晴,偏北風二到三級";
    }
}

寫一個普通的類,使其繼承自spring的上下文監聽器,並在初始化方法中發布接口,這樣在容器啟動時自動會發布

復制代碼
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容器中設置該監聽器

 <listener>
      <listener-class>springframe.listener.MyListener</listener-class>
  </listener>

啟動容器(如果啟動過程中報錯:出現類似

Wrapper class webservice.jaxws.SayHi is not found. Have you run APT to generate them?

則說明使用的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的訪問端口不一樣,否則,會出現無法訪問項目。

三、使用cxf開發webservice接口

待定。。

from:https://www.cnblogs.com/yxjdragon/p/6030740.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM