初識webservice

来源:http://www.cnblogs.com/zhangzongle/archive/2016/11/05/6034394.html
-Advertisement-
Play Games

一.神秘的webservice Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程式,可使用開放的XML(標準通用標記語言下的一個子集)標準來描述、發佈、發現、協調和配置這些應用程式,用於開發分散式的互操作的應用程式。Web Service技術, 能使得運行在不同 ...


一.神秘的webservice

        Web service是一個平臺獨立的,低耦合的,自包含的、基於可編程的web的應用程式,可使用開放的XML標準通用標記語言下的一個子集)標準描述、發佈、發現、協調和配置這些應用程式,用於開發分散式的互操作的應用程式。Web Service技術, 能使得運行在不同機器上的不同應用無須藉助附加的、專門的第三方軟體或硬體, 就可相互交換數據或集成。依據Web Service規範實施的應用之間, 無論它們所使用的語言、 平臺或內部協議是什麼, 都可以相互交換數據。Web Service是自描述、 自包含的可用網路模塊, 可以執行具體的業務功能。Web Service也很容易部署, 因為它們基於一些常規的產業標準以及已有的一些技術,諸如標準通用標記語言下的子集XML、HTTP。Web Service減少了應用介面的花費。Web Service為整個企業甚至多個組織之間的業務流程的集成提供了一個通用機制。

二.webservice技術支持

                                                                  

 

(詳情可參考webservice的百度百科) 

Web Service平臺需要一套協議來實現分散式應用程式的創建。任何平臺都有它的數據表示方法和類型系統。要實現互操作性,Web Service平臺必須提供一套標準的類型系統,用於溝通不同平臺、編程語言和組件模型中的不同類型系統。這些協議有:  XML和XSD 可擴展的標記語言標準通用標記語言下的一個子集)是Web Service平臺中表示數據的基本格式。除了易於建立和易於分析外,XML主要的優點在於它既與平臺無關,又與廠商無關。XML是由萬維網協會(W3C)創建,W3C制定的XML SchemaXSD 定義了一套標準的數據類型,並給出了一種語言來擴展這套數據類型。 Web Service平臺是用XSD來作為數據類型系統的。當你用某種語言如VB. NET或C# 來構造一個Web Service時,為了符合Web Service標準,所有你使用的數據類型都必須被轉換為XSD類型。如想讓它使用在不同平臺和不同軟體的不同組織間傳遞,還需要用某種東西將它包裝起來。這種東西就是一種協議,如 SOAP。   SOAP SOAP即簡單對象訪問協議(Simple Object Access Protocol),它是用於交換XML標準通用標記語言下的一個子集)編碼信息的輕量級協議。它有三個主要方面:XML-envelope為描述信息內容和如何處理內容定義了框架,將程式對象編碼成為XML對象的規則,執行遠程過程調用(RPC)的約定。SOAP可以運行在任何其他傳輸協議上。例如,你可以使用 SMTP,即網際網路電子郵件協議來傳遞SOAP消息,這可是很有誘惑力的。在傳輸層之間的頭是不同的,但XML有效負載保持相同。 Web Service 希望實現不同的系統之間能夠用“軟體-軟體對話”的方式相互調用,打破了軟體應用、網站和各種設備之間的格格不入的狀態,實現“基於Web無縫集成”的目標。    WSDL Web Service描述語言WSDL 就是用機器能閱讀的方式提供的一個正式描述文檔而基於XML標準通用標記語言下的一個子集)的語言,用於描述Web Service及其函數、參數和返回值。因為是基於XML的,所以WSDL既是機器可閱讀的,又是人可閱讀的。    UDDI UDDI 的目的是為電子商務建立標準;UDDI是一套基於Web的、分散式的、為Web Service提供的、信息註冊中心的實現標準規範,同時也包含一組使企業能將自身提供的Web Service註冊,以使別的企業能夠發現的訪問協議的實現標準。

三.為什麼需要Web服務

Web服務為Internet上應用程式之間的交互提供了方便

Web服務也減輕了企業級應用中出現的異構系統的整合危機

Web服務的優勢包括:

 

四.web廣泛用到的技術

  1. TCP/IP:通用網路協議,被各種設備使用
  2. HTML標準通用標記語言下的一個應用):通用用戶界面,可以使用HTML標簽顯示數據
  3. .NET: 不同應用程式間共用數據與數據交換
  4. Java:寫一次可以在任何系統運行的通用編程語言,因為java具有跨平臺特性
  5. XML標準通用標記語言下的一個子集):通用數據表達語言,在web上傳送結構化數據的容易方法
       他們的特點是其開放性,跨平臺性,開放性正是Web services的基礎。

五.Web服務在項目中的使用

  1.使用JAX-WS發佈和調用web服務

      (JAX-WS--->web服務標準,jdk中的一個組件,集成了JAXB,本質上其實是Scoket編程)

01.發佈自己的ws服務

源碼介紹:

HelloService.java

package cn.myservice;
//Service端(伺服器端)
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

//區域網任何人都可以訪問
@WebService
public class HelloService {
    
  public void say(String name){
      System.out.println("Hello"+name);
  }
  public static void main(String[] args) {
      /**
       * 埠號:50000
       * 一個標識(區分的作用):hello
       * 發佈者:new HelloService()
       */
     Endpoint.publish("http://localhost:50000/hello", new HelloService());
     System.out.println("server is listening ....");
  }
}
View Code

運行效果:

 

大家也可以用cmd命令 netstat -na來看看有沒有我們發佈的埠號的存在(如下圖,它是處於監聽狀態的)

 

 

現在我們的區域網上都可以訪問我發佈的(http://localhost:50000/hello)這個服務了

效果:

02.調用自己的ws服務

   001.MyEclipse自帶工具調用

   步驟一:

 步驟二:

步驟三:

 步驟四:

步驟五:

步驟六:

步驟七:

這時就完成了調用,控制台就會列印相應的信息

     002.書寫代碼調用

其中我們myservice包中的類我們是不需要自己去寫的,我們可以使用jdk中的wsimport.exe利用我們cmd命令給我們生成(當然是在保證我們的jdk安裝,環境變數配置成功的情況下)

操作如下:

這時,我們來看看我們的c盤根目錄:

 

源碼介紹:

1.HelloService.java

 

package cn.myservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebService(name = "HelloService", targetNamespace = "http://myservice.cn/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface HelloService {


    /**
     * 
     * @param arg0
     */
    @WebMethod
    @RequestWrapper(localName = "say", targetNamespace = "http://myservice.cn/", className = "cn.myservice.Say")
    @ResponseWrapper(localName = "sayResponse", targetNamespace = "http://myservice.cn/", className = "cn.myservice.SayResponse")
    @Action(input = "http://myservice.cn/HelloService/sayRequest", output = "http://myservice.cn/HelloService/sayResponse")
    public void say(
        @WebParam(name = "arg0", targetNamespace = "")
        String arg0);

}
View Code

 

2.HelloServiceService.java

package cn.myservice;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "HelloServiceService", targetNamespace = "http://myservice.cn/", wsdlLocation = "http://localhost:50000/hello?wsdl")
public class HelloServiceService
    extends Service
{

    private final static URL HELLOSERVICESERVICE_WSDL_LOCATION;
    private final static WebServiceException HELLOSERVICESERVICE_EXCEPTION;
    private final static QName HELLOSERVICESERVICE_QNAME = new QName("http://myservice.cn/", "HelloServiceService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:50000/hello?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        HELLOSERVICESERVICE_WSDL_LOCATION = url;
        HELLOSERVICESERVICE_EXCEPTION = e;
    }

    public HelloServiceService() {
        super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME);
    }

    public HelloServiceService(WebServiceFeature... features) {
        super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME, features);
    }

    public HelloServiceService(URL wsdlLocation) {
        super(wsdlLocation, HELLOSERVICESERVICE_QNAME);
    }

    public HelloServiceService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, HELLOSERVICESERVICE_QNAME, features);
    }

    public HelloServiceService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public HelloServiceService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns HelloService
     */
    @WebEndpoint(name = "HelloServicePort")
    public HelloService getHelloServicePort() {
        return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns HelloService
     */
    @WebEndpoint(name = "HelloServicePort")
    public HelloService getHelloServicePort(WebServiceFeature... features) {
        return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.class, features);
    }

    private static URL __getWsdlLocation() {
        if (HELLOSERVICESERVICE_EXCEPTION!= null) {
            throw HELLOSERVICESERVICE_EXCEPTION;
        }
        return HELLOSERVICESERVICE_WSDL_LOCATION;
    }

}
View Code

3.ObjectFactory.java

package cn.myservice;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the cn.myservice package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _SayResponse_QNAME = new QName("http://myservice.cn/", "sayResponse");
    private final static QName _Say_QNAME = new QName("http://myservice.cn/", "say");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: cn.myservice
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link SayResponse }
     * 
     */
    public SayResponse createSayResponse() {
        return new SayResponse();
    }

    /**
     * Create an instance of {@link Say }
     * 
     */
    public Say createSay() {
        return new Say();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link SayResponse }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "http://myservice.cn/", name = "sayResponse")
    public JAXBElement<SayResponse> createSayResponse(SayResponse value) {
        return new JAXBElement<SayResponse>(_SayResponse_QNAME, SayResponse.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Say }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "http://myservice.cn/", name = "say")
    public JAXBElement<Say> createSay(Say value) {
        return new JAXBElement<Say>(_Say_QNAME, Say.class, null, value);
    }

}
View Code

4.package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://myservice.cn/")
package cn.myservice;
View Code

5.Say.java

package cn.myservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>say complex type的 Java 類。
 * 
 * <p>以下模式片段指定包含在此類中的預期內容。
 * 
 * <pre>
 * &lt;complexType name="say">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "say", propOrder = {
    "arg0"
})
public class Say {

    protected String arg0;

    /**
     * 獲取arg0屬性的值。
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getArg0() {
        return arg0;
    }

    /**
     * 設置arg0屬性的值。
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setArg0(String value) {
        this.arg0 = value;
    }

}
View Code

6.SayResponse.java

package cn.myservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>sayResponse complex type的 Java 類。
 * 
 * <p>以下模式片段指定包含在此類中的預期內容。
 * 
 * <pre>
 * &lt;complexType name="sayResponse">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayResponse")
public class SayResponse {


}
View Code

7.MyTest.java(測試類)

package cn.test;

import cn.myservice.HelloService;
import cn.myservice.HelloServiceService;

public class MyTest {
    public static void main(String[] args) {
        HelloServiceService service = new HelloServiceService();
        HelloService port = service.getHelloServicePort();
        port.say("坤坤");
    }
}
View Code

8.運行效果

這就完成了調用。

 2.使用CXF發佈和調用web服務

未完待續。。。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在linux中,常常都要提示設置: umask 022 其作用如下: 功能說明:指定在建立文件時預設的許可權掩碼。語 法:umask [-S][許可權掩碼]補充說明:umask可用來設定[許可權掩碼]。[許可權掩碼]是由3個八進位的數字所組成,將現有的存取許可權減掉許可權掩碼後,即可產生建立文件時預設的許可權。參 ...
  • Linux 掛載 備註:如果掛載到有文件的目錄下 會遮擋原先文件 必須卸載掛載的硬碟 才能顯示 Linux umount 卸載 ...
  • avahi-daemon是一種Linux操作系統上運行在客戶機上實施查找基於網路的Zeroconf service的服務守護進程。 該服務可以為Zeroconf網路實現DNS服務發現及DNS組播規範。 用戶程式通過Linux D-Bus信息傳遞接收發現到網路服務和資源的通知。 該守護進程配合緩存用戶 ...
  • 從沒想到自己會開通博客,之前在編程上遇到問題 總會來到博客園來檢索點文檔看一下。 今天終於動手註冊開通了一個博客 希望能在這記錄下自己在程式世界的 一點一滴 每一步腳印 每一次進步 望大家共勉 ...
  • **************************************************************************************** ...
  • Makefile linux 程式開發的一道坎. 也是linux開發中最重要的基本功之一, 不求深入, 但求讀懂. ...
  • 這個方法百度到的,有個面試我的讓我做,百度了一堆資料才實現。 ...
  • *
    ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...