swagger-ui-react渲染openapi.json显示api文档,配置服务器和认证信息

import { useState } from "react"
import "./App.css"
import SwaggerUI from "swagger-ui-react"
import "swagger-ui-react/swagger-ui.css"

function App() {
  const apiJson = {
    openapi: "3.0.0",
    info: {
      title: "Example API",
      description: "这是一个示例API,用于展示如何在OpenAPI规范中配置授权验证和多服务器选项。",
      version: "1.0.0",
      termsOfService: "https://example.com/terms/"
    },
    servers: [
      {
        url: "https://{ip}:{port}/{basePath}",
        description: "测试环境服务器",
        variables: {
          ip: {
            default: "127.0.0.1",
            description: "服务器ip"
          },
          port: {
            // enum: ["8081", "8082"],
            default: "8081"
          },
          basePath: {
            default: ""
          }
        }
      },
      {
        url: "https://api.example.com/v1",
        description: "生产环境"
      }
    ],

    components: {
      securitySchemes: {
        // oauth2: {
        //   type: "oauth2",
        //   flows: {
        //     implicit: {
        //       authorizationUrl: "https://example.com/oauth2/authorize",
        //       scopes: {
        //         read: "读取权限",
        //         write: "写入权限"
        //       }
        //     }
        //   }
        // },
        // basicAuth: {
        //   type: "http",
        //   scheme: "basic"
        // },
        // Jwt: {
        //   type: "http",
        //   scheme: "bearer",
        //   bearerFormat: "JWT"
        // },
        apiKey: {
          type: "apiKey",
          name: "X-Test-Authorization",
          in: "header"
        }
      },
      schemas: {
        User: {
          type: "object",
          properties: {
            id: {
              type: "integer",
              format: "int64"
            },
            username: {
              type: "string"
            },
            email: {
              type: "string",
              format: "email"
            }
          },
          required: ["id", "username"]
        },
        Lanzi: {
          type: "array",
          items: {
            type: "string"
          },
          required: true
        }
      }
    },
    paths: {
      "/users/me": {
        get: {
          summary: "获取当前用户的个人信息",
          operationId: "getUserProfile",
          responses: {
            200: {
              description: "成功返回用户信息",
              content: {
                "application/json": {
                  schema: {
                    $ref: "#/components/schemas/User"
                  }
                }
              }
            },
            401: {
              description: "未授权"
            }
          }
        }
      }
    },
    security: [
      {
        apiKey: []
      }
    ]
  }

  return <SwaggerUI spec={apiJson} displayOperationId />
}

export default App


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值