MuleSoft知识总结-4.使用RAML设计接口

23 篇文章 6 订阅
2 篇文章 0 订阅

文章目录

前言

RAML:
RAML的全称是RESTful API Modeling Language(RESTful API建模语言),这是一种基于YAML格式的新规范,书写格式和YAML相同。RAML官网

设计API

  1. 设计一个企业员工信息API,可以获取企业员工的个人信息。进入Design Center
    在这里插入图片描述

  2. 首先撰写API的基本信息

#%RAML 1.0
title: EmployeeAPI
documentation:
  - title: 个人信息
    content: |
      个人信息,可以通过访问接口获取员工的个人信息
标签作用
#%RAML x.x指定RAML版本
titleAPI名
documentation为API撰写文档
-title文档标题
content文档内容
  1. API返回值类型
types:
  employee:
    type: object
    properties:
      number:
        type: string
        displayName: "员工号"
        pattern: "[0-9]{8}"
        example: "12345678"
      name:
        type: string
        displayName: "员工姓名"
        description: "员工姓名只能是大小写字母"
        required: true
        maxLength: 20
        minLength: 2
        pattern: "[a-zA-Z]{2,20}"
        example: "zhangsan"
      age:
        type: string
        displayName: "员工年龄"
        pattern: "[0-9]{1,2}"
        example: "20"
      sex:
        type: string
        displayName: "员工性别"
        enum:
          - man
          - woman
        example: "man"
      phone:
        type: string
        displayName: "员工电话"
        pattern: "[0-9]{11}"
        example: "12345678901"
      Email:
        type: string
        displayName: "电子邮箱"
        pattern: "[\\w]+(\\.[\\w]+)*@[\\w]+(\\.[\\w]+)"
        example: "san.b.zhang@acc.com"
      state:
        type: string
        displayName: "居住省市"
        pattern: "[a-zA-Z]{1,20}"
        example: "liaoning"
      city:
        type: string
        displayName: "居住城市"
        pattern: "[a-zA-Z]{1,20}"
        example: "dalian"
      group:
        type: string
        displayName: "项目名"
        pattern: "[a-zA-Z]{1,20}"
        example: "afl"
      team:
        type: string
        displayName: "组名"
        pattern: "[a-zA-Z]{1,20}"
        example: "mulesoft"
  Enumber: 
    properties:
      number:
        type: string
        displayName: "员工号"
        pattern: "[0-9]{8}"
        example: "12345678"
标签作用
types设置类型标识符
employee类名设置为employee(可自定义)
type类型,值可以为(any,array,boolean,datetime…,file,integer,number,object,string,自定义类型)
properties变量名(为类设置变量)
displayName为变量起一个容易理解的别名
description变量的描述内容
required必须或可选(true,false)
maxLength最大长度
minLength最小长度
pattern正则表达式内容
example输出例子
enum枚举类型
  1. 定义接口
/getEmpInf:
  get:
    responses:
      200:
        body:
          application/json:
            type: employee[]
  post:
    body:
      application/json:
        type: Enumber
    responses:
      200:
        body:
          application/json:
            type: employee
标签作用
/路由
get请求方式
body外层为请求体,responses内为响应体
responses响应
200HTTP状态码
application/json消息格式
  1. Design Center内容
    在这里插入图片描述
    6.现在的employee可以进行拆解分类,比如电话和邮箱可以整合成联系方式类,居住省市和居住城市可以整合为地址类,整合后代码如下:
types:
  employee:
    type: object
    properties:
      number:
        type: string
        displayName: "员工号"
        pattern: "[0-9]{8}"
        example: "12345678"
      name:
        type: string
        displayName: "员工姓名"
        description: "员工姓名只能是大小写字母"
        required: true
        maxLength: 20
        minLength: 2
        pattern: "[a-zA-Z]{2,20}"
        example: "zhangsan"
      age:
        type: string
        displayName: "员工年龄"
        pattern: "[0-9]{1,2}"
        example: "20"
      sex:
        type: string
        displayName: "员工性别"
        enum:
          - man
          - woman
        example: "man"
      group:
        type: string
        displayName: "项目名"
        pattern: "[a-zA-Z]{1,20}"
        example: "afl"
      team:
        type: string
        displayName: "组名"
        pattern: "[a-zA-Z]{1,20}"
        example: "mulesoft"
      contact:
        type: contact
        displayName: "联系方式"
      address:
        type: address
        displayName: "地址"
  contact: 
    type: object
    properties:
      phone:
        type: string
        displayName: "员工电话"
        pattern: "[0-9]{11}"
        example: "12345678901"
      Email:
        type: string
        displayName: "电子邮箱"
        pattern: "[\\w]+(\\.[\\w]+)*@[\\w]+(\\.[\\w]+)"
        example: "san.b.zhang@acc.com"
  address:
    type: object
    properties:
      state:
        type: string
        displayName: "居住省市"
        pattern: "[a-zA-Z]{1,20}"
        example: "liaoning"
      city:
        type: string
        displayName: "居住城市"
        pattern: "[a-zA-Z]{1,20}"
        example: "dalian"
  Enumber: 
    properties:
      number:
        type: string
        displayName: "员工号"
        pattern: "[0-9]{8}"
        example: "12345678"
  1. Design Center支持设置单独的类文件,把types拆解分类为四个文件,分别为:employeeType.raml,contactType.raml,addressType.raml,EnumberType.raml
    在EmployeeAPI中进行引用,这样分类方便管理,效果会更好
types:
  employee: !include entity/employeeType.raml
  Enumber: !include entity/EnumberType.raml

创建entity文件夹
在这里插入图片描述
按照如下步骤创建employeeType.raml,contactType.raml,addressType.raml,EnumberType.raml四个文件

  1. 创建文件
    在这里插入图片描述
  2. 定义文件类型,选择文件类型为Data Type,为文件命名
    在这里插入图片描述
  3. 代码如下

employeeType.raml:

#%RAML 1.0 DataType

type: object
properties:
  number:
    type: string
    displayName: "员工号"
    pattern: "[0-9]{8}"
    example: "12345678"
  name:
    type: string
    displayName: "员工姓名"
    description: "员工姓名只能是大小写字母"
    required: true
    maxLength: 20
    minLength: 2
    pattern: "[a-zA-Z]{2,20}"
    example: "zhangsan"
  age:
    type: string
    displayName: "员工年龄"
    pattern: "[0-9]{1,2}"
    example: "20"
  sex:
    type: string
    displayName: "员工性别"
    enum:
      - man
      - woman
    example: "man"
  group:
    type: string
    displayName: "项目名"
    pattern: "[a-zA-Z]{1,20}"
    example: "afl"
  team:
    type: string
    displayName: "组名"
    pattern: "[a-zA-Z]{1,20}"
    example: "mulesoft"
  contact:
    type: !include contactType.raml
    displayName: "联系方式"
  address:
    type: !include addressType.raml
    displayName: "地址"

contactType.raml:

#%RAML 1.0 DataType

    type: object
    properties:
      phone:
        type: string
        displayName: "员工电话"
        pattern: "[0-9]{11}"
        example: "12345678901"
      Email:
        type: string
        displayName: "电子邮箱"
        pattern: "[\\w]+(\\.[\\w]+)*@[\\w]+(\\.[\\w]+)"
        example: "san.b.zhang@acc.com"

addressType.raml:

#%RAML 1.0 DataType

type: object
properties:
    state:
      type: string
      displayName: "居住省市"
      pattern: "[a-zA-Z]{1,20}"
      example: "liaoning"    
    city:
      type: string
      displayName: "居住城市"
      pattern: "[a-zA-Z]{1,20}"
      example: "dalian"    

EnumberType.raml:

#%RAML 1.0 DataType

properties:
  number:
    type: string
    displayName: "员工号"
    pattern: "[0-9]{8}"
    example: "12345678"

employeeapi.raml:

#%RAML 1.0
title: EmployeeAPI
documentation:
  - title: 个人信息
    content: |
      个人信息,可以通过访问接口获取员工的个人信息
types:
  employee: !include entity/employeeType.raml
  Enumber: !include entity/EnumberType.raml

/getEmpInf:
  get:
    responses:
      200:
        body:
          application/json:
            type: employee[]
  post:
    body:
      application/json:
        type: Enumber
    responses:
      200:
        body:
          application/json:
            type: employee

完成后如图:

在这里插入图片描述

下回介绍测试接口

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值