How to get request body and response body in JSON

2019-07-16 03:25发布

Here is my code:

public static void main(String[] args) {
    Swagger swagger = new SwaggerParser().read("D:\\espace201612\\rest-services.yaml");
    Map<String, Path> paths = swagger.getPaths();
    for (Map.Entry<String, Path> p : paths.entrySet()) {
      Path path = p.getValue();
      Map<HttpMethod, Operation> operations = path.getOperationMap();
      for (Entry<HttpMethod, Operation> o : operations.entrySet()) {
        System.out.println("===");
        System.out.println("PATH:" + p.getKey());
        System.out.println("Http method:" + o.getKey());
        System.out.println("Summary:" + o.getValue().getSummary());
        System.out.println("Parameters number: " + o.getValue().getParameters().size());
        for (Parameter parameter : o.getValue().getParameters()) {
          System.out.println(" - " + parameter.getName());
        }
        System.out.println("Responses:");
        for (Map.Entry<String, Response> r : o.getValue().getResponses().entrySet()) {
          System.out.println(" - " + r.getKey() + ": " + r.getValue().getDescription());
        }
        System.out.println("");
      }

    }
  }

Here is my output:

===
PATH:/tenantconsolewebsite/v1
Http method:GET
Summary:查询是否部署成功
Parameters number: 0
Responses:
 - 200: Get Service Information
 - 400: 参数错误
 - 403: URL鉴权
 - 404: rest接口没找到
 - 406: 流控限流
 - 500: 默认ServiceException异常码,应用内部错误
 - 502: 网关错误
 - 503: 服务不可用
 - 504: 表示时间超时,总线超时时间300s
 - 888: 服务摘机,不提供服务

===
PATH:/tenantconsolewebsite/v1/orders
Http method:POST
Summary:查询订单列表
Parameters number: 1
 - orderRequest
Responses:
 - 200: 订单列表查询结果

===
PATH:/tenantconsolewebsite/v1/order/{orderID}
Http method:POST
Summary:null
Parameters number: 2
 - orderID
 - orderDetailsRequest
Responses:
 - 200: 订单详情查询结果

Here is my yaml file:

# 服务生成默认接口定义文件  
---
swagger: '2.0'
# 服务基本信息定义,包括服务名称,版本,服务描述(包括SLA),服务Owner
info:
  # 服务接口版本
  version: v1
  # 定义服务名称
  title: TenantConsoleWebsite
  # 描述服务提供的功能、限制、注意事项、服务的SLA等
  description: |
    默认生成yaml文件提供了默认接口,用于检查应用部署是否成功

    ** 服务SLA:**a

    |SLA项|定义|
    |-----|-----|
    |请求成功率| >=99.9%|
    |可用性|Tair1|
    |数据一致性|最终一致,不一致时长<1分钟|
    |吞吐量|4000tps|
    |TP50请求时延|2ms|
    |TP99.9请求时延|5ms|
  # 声明服务维护人员信息  
  contact:
    name: xxxx
    email: xxxxx
schemes:
  - https
# Base PATH, 完整的访问路径为 basePath + path
basePath: /rest
paths:
  # 资源URL定义,服务生成默认接口,用于检查应用部署是否成功
  /tenantconsolewebsite/v1:
    # GET方法定义
    get:
      summary: '查询是否部署成功'
      description: '查询是否部署成功'
      tags:
        - TenantConsoleWebsite    # 用于代码生成,声明方法所在类名称
      operationId: getServiceInfo  # 用于代码生成,声明方法名称
      produces:
        - application/json  # 声明返回Json格式数据
      responses:
        200:
          description: Get Service Information
          schema:  # 声明返回值Schema
            type: string  # 直接返回简单String类型数据
        400:
          description: '参数错误'
        403:
          description: 'URL鉴权'
        404:
          description: 'rest接口没找到'
        406:
          description: '流控限流'
        500:
          description: '默认ServiceException异常码,应用内部错误'
        502:
          description: '网关错误'
        503:
          description: '服务不可用'
        504:
          description: '表示时间超时,总线超时时间300s'
        888:
          description: '服务摘机,不提供服务'
  #查询订单列表
  /tenantconsolewebsite/v1/orders:
    # POST方法
    post: 
      summary: '查询订单列表'
      tags: 
        - TenantConsoleWebsite
      operationId: getOrders
      produces:
        - application/json
      parameters:
        - name: orderRequest
          in: body
          required: true
          schema: 
            $ref: '#/definitions/OrderRequest'
      responses:
        '200':
          description: 订单列表查询结果
          schema: # 声明返回值Schema
            type: array
            items:
              $ref: '#/definitions/Order'
  #查询订单详情
  /tenantconsolewebsite/v1/order/{orderID}:
    # post方法
    post:
      tags: 
        - TenantConsoleWebsite
      operationId: getOrderDetails
      produces:
        - application/json
      parameters: 
        - name: orderID
          in: path
          required: true
          type: string 
        - name: orderDetailsRequest
          in: body
          required: true
          schema: 
            $ref: '#/definitions/OrderDetailsRequest'
      responses:
        '200':
          description: 订单详情查询结果
          schema: # 声明返回值Schema
            $ref: '#/definitions/OrderDetails'

definitions:
  ServiceInfo:
    properties:
      serviceName:
        type: string
      serviceVersion:
        type: string
      serviceOwner:
        type: string
  #订单相关数据结构
  OrderRequest: 
    properties: 
      orderQueryCond:
        type: object
        $ref: '#/definitions/OrderQueryCond'
      pageNum: 
        type: integer
      pageSize:
        type: integer
  OrderQueryCond:
    properties: 
      orderUserID:
        type: string
      orderBeginDate:
        type: string
        format: date-time
      orderType: 
        type: string
      status: 
        type: string
  Order: 
    properties: 
      orderID: 
        type: string
      orderUserID: 
        type: string
      createUserID: 
        type: string
      status: 
        type: integer
      currency: 
        type: integer
      totalAmount: 
        type: integer
        format: int64
      createTime: 
        type: string
        format: date-time
      extInfo: 
        type: object
        $ref: '#/definitions/OrderExtInfo'
  OrderExtInfo:
    properties: 
      orderUserInfo: 
        type: object
        $ref: '#/definitions/OrderUserInfo'
      contactPersonInfo: 
        type: array
        items: 
          $ref: '#/definitions/ContactPersonInfo'
  OrderUserInfo: 
    properties: 
      name: 
        type: string
  ContactPersonInfo: 
    properties: 
      contactPersonId:
        type: string
      name: 
        type: string
      mobilePhone: 
        type: string
      email: 
        type: string
  OrderDetailsRequest: 
    properties: 
      orderUserID: 
        type: string
  OrderDetails: 
    properties: 
      orderID: 
        type: string
      orderUserID: 
        type: string
      createUserID: 
        type: string
      status: 
        type: integer
      orderItems: 
        type: array
        items: 
          $ref: '#/definitions/OrderItem'
      currency: 
        type: integer
      totalAmount: 
        type: integer
        format: int64
      createTime: 
        type: string
        format: date-time
      extInfo: 
        type: object
        $ref: '#/definitions/OrderExtInfo' 
  OrderItem: 
    properties: 
      orderItemID: 
        type: string
      itemOfferingInst: 
        type: array
        items: 
          $ref: '#/definitions/itemOfferingInstItem'
      quantity:
        type: integer
      currency:
        type: integer
      amount:
        type: integer
        format: int64
  itemOfferingInstItem: 
    properties:
      offeringInst:
        type: object
        $ref: '#/definitions/OfferingInst'
  OfferingInst: 
    properties: 
      offeringInstID: 
        type: string
      offeringID: 
        type: string
      offeringName: 
        type: string
      effectiveDate:
        type: string
        format: data-time 
      expiryDate: 
        type: string
        format: data-time
      itemObjectAttr: 
        type: object
        $ref: '#/definitions/ItemObjectAttr' 
      extOfferingInstID: 
        type: string
      subProductInsts:
        type: array
        items:  
          $ref: '#/definitions/ProductInst'
  ProductInst: 
    properties: 
      productInstID:
        type: string
      productID:
        type: string
      productName:
        type: string
      effectiveDate:
        type: string
        format: data-time
      expiryDate:
        type: string
        format: data-time
      edition:
        type: object
        $ref: '#/definitions/Edition' 
      extProdInstInfo: 
        type: object
        $ref: '#/definitions/ExtProdInstInfo' 
  Edition: 
    properties: 
      editionCode:
        type: string 
      feature: 
        type: array
        items: 
          $ref: '#/definitions/Feature' 
      resources: 
        type: array
        items: 
          $ref: '#/definitions/Resources'
  ExtProdInstInfo: 
    properties: 
      url: 
        type: string
      acctnum:
        type: string
      password: 
        type: string
  ItemObjectAttr:
    properties: 
      attrId:
        type: integer
        format: int64
      attrCode:
        type: string
      sValue:
        type: string
      attachInsType:
        type: string
      attachInstId:
        type: integer
        format: int64
  Feature:
    properties: 
      attrCode: 
        type: string
      attrValue: 
        type: string
  Resources:
    properties: 
      resName:
        type: string
      resValue: 
        type: string

But my output is not enough for me.I also want to get the request and response body.For example,in /tenantconsolewebsite/v1,it is a get method,so I don't request.Its response is String type. In tenantconsolewebsite/v1/orders, its request body is orderRequest model(or example) and its response is also a model(or example) which is not shown in my output.

标签: yaml swagger
0条回答
登录 后发表回答