Java:Web Service初入門

前言

Web Service技術在我第一次接觸,又沒有實際使用時徹底不理解這是什麼。覺得是一種相似Spring,Shiro的編程框架。後來漸漸理解,WS(即Web Service縮寫)是一種通用的接口規範,並按照該規範編寫接口對外提供服務。html

一 啥是WS

這個問題在我沒有編寫WS代碼時但是困擾了我好久,甚至第一次須要寫WS接口都不知道老大到底要我寫什麼。由於我習慣於去網上尋找資料自學並實踐某些知識點,而我在百度時查到的WS介紹基本都是這樣的。java

百度百科:web

Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可以使用開放的XML(標準通用標記語言下的一個子集)標準來描述、發佈、發現、協調和配置這些應用程序,用於開發分佈式的互操做的應用程序。spring

百度搜索WS排名前列的博客:apache

Web Service技術, 能使得運行在不一樣機器上的不一樣應用無須藉助附加的、專門的第三方軟件或硬件, 就可相互交換數據或集成。依據Web Service規範實施的應用之間, 不管它們所使用的語言、 平臺或內部協議是什麼, 均可以相互交換數據。編程

這種解釋對於不瞭解WS的人來看徹底是一頭霧水,後來在實踐中漸漸明白了什麼是WS。自我總結以下:app

Web Service就是一種跨編程語言跨操做系統平臺遠程調用技術。所謂跨編程語言和跨操做平臺,就是說服務端程序採用Java編寫,客戶端程序則能夠採用其餘編程語言編寫,反之亦然。跨操做系統平臺則是指服務端程序和客戶端程序能夠在不一樣的操做系統上運行。 遠程調用,就是一臺計算機的應用能夠調用其餘計算機上的應用,也能夠直接理解爲接口。例如:支付寶,支付寶並無銀行卡等數據,它只是去調用銀行提供的接口來得到數據。還有天氣預報等,也是氣象局把本身的系統服務以webservice服務的形式暴露出來,讓第三方網站和程序能夠調用這些服務功能。框架

因此Web Servic就是一種跨語言跨平臺的接口技術。frontend

二 Spring整合CXF

Web Service只是一套接口規範,實際運用中實現Web Service的方法有不少種,Java自己在jdk1.7以後也對webservice有了默認的實現(Oracle JAX-WS RI),可是在咱們實際開發中通常仍是會使用框架來,好比這裏所提到的CXF就有着普遍的應用。
CXF單獨發佈就不說了,直接講Spring整合CXF,畢竟如今的JavaEE開發是離不開Spring了。maven

2.1 maven配置

加入maven依賴

<!--cxf-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.6</version>
</dependency>

2.2 配置 web.xml

<!--定義一個cxf的servlet-->
<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

2.3 寫一個WS接口及其實現

很是簡潔的接口和實現
接口:HelloWorld.java

package com.erictao.cxf;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
    public String say(String str);
}

實現:HelloWorldImpl.java

package com.erictao.cxf.impl;
import com.erictao.cxf.HelloWorld;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
@Component("helloWorld")
@WebService
public class HelloWorldImpl implements HelloWorld {
    public String say(String str) {
        return "Hello"+str;
    }
}

2.4 修改Spring配置文件

在spring主配置文件spring.xml中添加引入spring-cxf.xml

<!-- lang: xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
    <context:component-scan base-package="com.erictao"/>
    <!-- 省略其餘配置內容 -->
    <import resource="spring-cxf.xml"/>
</beans>

而且建立配置文件spring-cxf.xml

<!-- lang: xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 
 
http://cxf.apache.org/jaxws
 
 
http://cxf.apache.org/schemas/jaxws.xsd">
 
    <!-- 定義webservice的發佈接口  -->
    <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld">
 
</beans>

更加具體的配置能夠查看官方給出的文檔:http://cxf.apache.org/docs/how-do-i-develop-a-service.html。
#helloWorld指的是咱們在HelloWorldImpl類中所自定義的名字,即@Component("helloWorld")定義的bean id,/HelloWorld則是咱們定義的接口地址。

三 運行發佈WS

以後咱們運行項目輸入該地址:http://127.0.0.1:8080/ssm/webservice/HelloWorld?wsdl若是出現以下界面:

則說明咱們的webservice發佈成功了。接下來只須要經過客戶端調用這個接口便可得到返回結果了。客戶端的代碼能夠直接使用工具生成,可參考:https://blog.csdn.net/andyliulin/article/details/53680915。

總結 以上就是一個簡單的Web Service入門實例,更多的關於CXF攔截器,客戶端調用就沒有作過多介紹,後續有時間的話再接着更新,明白了Web Service服務端和客戶端怎麼構建,後續只須要當作普通接口編寫業務代碼便可。

相關文章
相關標籤/搜索