MuleSoft系列(三)- 使用API設計器通過RAML定義API


在MuleSoft中,創建Application的方式有三種

  • Flow Designer:用於構建連接系統並使用API的集成應用程序的Web應用程序
  • API Designer:用於設計,記錄和模擬API的Web應用
  • Anypoint Studio:桌面IDE,用於實現API和構建集成應用程序

那么來看看如何使用API Designer 設計API並進行測試

在Anypoint Platform中,進入Design Center,點擊Create New,在彈出框中選擇 Create API Specification,輸入名稱:GetPerson,其他保持默認選擇,點擊創建

來看看進入的編輯頁面,主要包括左側的文檔區域,右側的API控制台,中間部分是編輯區域,底部有關鍵詞提示區域,在MuleSoft中被稱為Shelf

API Designer主要幫助我們編寫RAML並提供語法建議,錯誤檢測和實時模擬服務,並使用API Designer來測試你的API

在代碼的編輯區域,我們先來寫一個person的get接口,首先定義了get接口的header中必須從傳入一個叫Required_Id的參數,然后可選參數包括字符串類型的name和枚舉類型的type,在右側API 控制台,可以看到實時顯示你定義的接口內容,點擊Get方法可以查看詳細的內容

 

然后繼續添加get方法的響應體,這里我們學習如何引入左側文檔區域的內容,點擊Files右側的➕,添加一個文件夾:DataType,然后在這個文件夾中創建一個文件:person-api.raml

定義的person-api如下,聲明了id(可選),name,type,address,createdDate五個字段

#%RAML 1.0 DataType
displayName: person-api
description: Defines the fields in person Datatype

type: object
properties:
  id?: string
  name: string
  type: string
  address: string
  createdDate: datetime

在API中如何引用呢? 我們使用 !include 關鍵字

types:
  Person: !include DataType/person-api.raml

添加了Person類型之后,我們可以繼續添加get方法的響應體,狀態碼為200時返回一個Person集合,但是在返回值上,我們希望能返回一些樣例數據

responses:
      200:
        description: success
        body:
          application/json:
            type: Person[]
            examples:

所以這時候,我們再新建一個examples的文件夾,在里面添加一個帶有測試樣例的文件:person-example-api.raml, 里面的數據字段根據之前添加的Person對象添加

#%RAML 1.0 NamedExample

value:
  -
    id: '0001'
    name: 'test name'
    type: 'personal'
    address: 'beijing'
    createdDate: 2020-04-23T04:34:41.098Z
  -
    id: '0002'
    name: 'ricardo'
    type: 'business'
    address: 'shanghai'
    createdDate: 2020-04-22T04:34:41.098Z

回到編輯界面,我們再使用 !include 關鍵字,將測試樣例添加到response中

examples:
              output: !include examples/person-example-api.raml

寫完這一步后,我們可以打開右上角的模擬測試(Mocking Service), 選擇GET方法,點擊 Try It, 在填些必填的Required_Id之后,點擊Send按鈕,就能看到響應狀態碼200的返回值

 

到這里為止,我們已經成功定義了一個get接口,下面附上完整的RAML描述

 1 #%RAML 1.0
 2 title: GetPerson
 3 
 4 types:
 5   Person: !include DataType/person-api.raml 
 6   
 7 /person:
 8   get:
 9     displayName: getPerson
10     description: get the person record
11     headers:
12       Required_Id:
13         type: string
14         required: true
15     queryParameters:
16       name:
17         displayName: person name
18         description: query by name
19         required: false
20         type: string
21       type:
22         displayName: person type
23         description: person's type
24         enum: [busines , personal]
25     responses:
26       200:
27         description: success
28         body:
29           application/json:
30             type: Person[]
31             examples:
32               output: !include examples/person-example-api.raml

當我們完成了接口的開發后,緊接着需要將接口發布出去,點擊右上角的Publish, 選擇Publish to Exchange,會彈窗出來填接口的版本號,格式要求:x.y.z,然后點Publish to Exchange,完成后,切換到Exchange,就能找到你剛剛發布的接口:GetPerson

點GetPerson接口進入詳情界面,在這里你可以查看接口的詳細信息,添加接口的描述信息,或者模擬接口測試,你可以將接口Share給你的同事,讓他們可以查看這個接口,MuleSoft提供了一個評論系統,能查看這個接口的同事也可以對接口進行評論打分。當你需要發布新版本的接口時,你只要修改后重新publish即可

以上是對MuleSoft API Design整個操作周期的描述,了解了在Anypoint平台上應該如何應用API Design去設計一個API,測試接口的同時發布到Exchange上分享給別人使用

 


免責聲明!

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



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