webservice(一)概念和cxf實現服務端

webservice

webservice是一種提供service的形式,它是通過httpweb)來提供service。他可以基於http來提供你想提供的任意的服務,其實現可以是rpc,也可以是restful。webservice,其標準的通信協議是soap協議。

webservice接口與http接口

這兩有什麼不同?這個問題困擾了我一好段時間,這裏對webservice接口與http接口進行一下區分:

1、http接口就是我們web開發常寫的接口,用於在展示層(網頁)中調用。而webservice接口,則不是在展示層調用,而在後臺進行調用。一般使用場景如,需要與別的系統進行對接(相互調用),我方使用webservice寫好接口,對接方則在後臺調用這個接口,進行數據的訪問。

2、還有一點就是在接口的調用說明也有不同,http接口需要另外編寫接口說明,而webservice接口,則不需要另外編寫接口說明文檔,可以自帶wsdl說明,其中就會說明,有哪些接口,接口需要傳什麼參數,返回數據類型。

3、webservice其標準的通信協議是soap協議。而soap協議是基於http協議的。所以是可以像http請求那樣在postmanm上請求進行測試 ,只是在傳遞參數 是soap協議格式的xml報文

 

SOA與SOAP

SOA(Service-Oriented Architecture)面向服務架構是一種思想

SOAP(簡單對象訪問協議)SOAP是一種數據交換協議規範,是一種輕量的、簡單的、基於XML的協議的規範。可以簡單理解爲http+xml,即使用基於XML的數據結構和超文本傳輸協議(HTTP)

 

cxf實現webService服務端

1、引入依賴

 <dependencies>
        <!-- 要進行jaxws 服務開發 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 內置jetty web服務器  -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- 日誌實現 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>

    </dependencies>

2、編寫接口

import javax.jws.WebService;

/**
 * 對外發布服務的接口
 */
@WebService
public interface HelloService {
    /**
     * 對外發布服務的接口的方法
     */
    public String sayHello(String name);
}

//實現
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return name + ",Welcome to Itheima!";
    }


}

3、發佈服務

public class Server {
    public static void main(String[] args) {
        //  發佈服務的工廠
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();

        //  設置服務地址
        factory.setAddress("http://localhost:8000/ws/hello");

        //  設置服務類
        factory.setServiceBean(new HelloServiceImpl());

        //  添加日誌輸入、輸出攔截器,觀察soap請求、soap響應內容,這裏需要配置log4j.properties,日誌才生效
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());

        //  發佈服務
        factory.create();

        System.out.println("發佈服務成功,端口8000.....");

    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章