在Linux環境中,Swagger的配置文件通常是一個YAML或JSON格式的文件,用于定義API的結構和行為。以下是一個簡單的Swagger配置文件示例,使用YAML格式編寫:
swagger: '2.0'
info:
title: Sample API
description: This is a sample API for Swagger documentation
version: '1.0.0'
host: api.example.com
basePath: /v1
schemes:
- https
paths:
/users:
get:
summary: List all users
description: Returns a list of users
responses:
200:
description: An array of users
schema:
type: array
items:
$ref: '#/definitions/User'
/users/{userId}:
get:
summary: Get a user by ID
description: Returns a user based on the provided ID
parameters:
- in: path
name: userId
type: string
required: true
responses:
200:
description: A single user
schema:
$ref: '#/definitions/User'
definitions:
User:
type: object
properties:
id:
type: string
name:
type: string
email:
type: string
2.0
。https
。get /users
: 獲取所有用戶的列表。get /users/{userId}
: 根據用戶ID獲取單個用戶的信息。User
: 用戶對象,包含id
、name
和email
屬性。swagger.yaml
。docker run -p 8080:8080 -e SWAGGER_JSON=/path/to/swagger.yaml -e SWAGGER_URL=http://petstore.swagger.io/v2/swagger.json swaggerapi/swagger-ui
將/path/to/swagger.yaml
替換為你的Swagger配置文件的路徑。
http://localhost:8080
,你應該能夠看到Swagger UI界面,并加載了你定義的API文檔。通過這種方式,你可以在Linux環境中配置和使用Swagger來文檔化和測試你的API。