Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Swagger Core Components

Swagger Core Components

Swagger is a set of open-source tools built around the OpenAPI specification, used for designing, building, documenting, and consuming RESTful APIs.

Swagger provides a standardized way to describe the structure of an API, enabling both developers and machines to understand its functionality without accessing source code or other documentation.

The core value of Swagger lies in:

Swagger Core Components

Component Input Output Applicable Stages
Swagger Editor Manually write YAML/JSON Real-time preview of API documentation API Design Phase
Swagger UI OpenAPI File Interactive Web Documentation Development/Testing Phase
Swagger Codegen OpenAPI File Client/Server Code Frontend/Backend Collaboration Phase

** Component Collaboration Flowchart:**



Swagger Editor(设计API)  

  ↓  

OpenAPI 规范文件(YAML/JSON)  

  ↓  

Swagger UI(展示文档) 或 Swagger Codegen(生成代码)  

Detailed Explanation of Swagger Core Components

1. Swagger Editor

Swagger Editor is a browser-based visual editor for writing and previewing OpenAPI specifications (YAML/JSON format) in real time.

Swagger Editor provides syntax highlighting, auto-completion, and error checking.

Swagger Editor's code generation, save, and export features are now part of SmartBear API Hub. Access address:https://try.platform.smartbear.com/new-organization.

Use cases:

Visit the online editor Swagger Editor:https://editor.swagger.io/

Enter the following YAML code (defining a simple GET /users endpoint):

Example

span style="color: green;">

openapi

:

3.0.3

info

:

  title

:

RUNOOB User Management System

  version

:

1.0.0

paths

:

  /users

:

    get

:

      summary

:

Get user list

      responses

:

        '200'

:

          description

:

Successfully return the user list

          content

:

            application/json

:

              schema

:

                type

:

array

                items

:

                  type

:

object

                  properties

:

                    id

:

                      type

:

integer

                    name

:

                      type

:

string

The right panel will render the API documentation and interactive UI in real time.


Swagger UI

Swagger UI converts the OpenAPI specification into visual interactive documentation, supporting direct API testing in the browser.

Swagger UI automatically generates request examples, response models, and a debugging interface.

Swagger UI: https://swagger.io/tools/swagger-ui/

** Use Cases:**

Integration example (using Node.js as an example), install dependencies:



npm install swagger-ui-express swagger-jsdoc

Create the swagger.js configuration file:

Example

const

swaggerJSDoc

=

require

(

'swagger-jsdoc'

)

;

const

options

=

{

  definition

:

{

    openapi

:

'3.0.0'

,

    info

:

{

title

:

'User API'

,

version

:

'1.0.0'

}

,

 

}

,

  apis

:

[

'./routes/*.js'

]

,

// Scan comments in route files

}

;

const

swaggerSpec

=

swaggerJSDoc

(

options

)

;

module.

exports

=

swaggerSpec

;

Mount the UI in Express:

Example

const

express

=

require

(

'express'

)

;

const

swaggerUi

=

require

(

'swagger-ui-express'

)

;

const

swaggerSpec

=

require

(

'./swagger'

)

;

const

app

=

express

(

)

;

app.

use

(

'/api-docs'

,

swaggerUi.

serve

,

swaggerUi.

setup

(

swaggerSpec

)

)

;

Visit http://localhost:3000/api-docs to view the documentation.


Swagger Codegen

Swagger Codegen automatically generates client SDKs (such as Java, Python) or server stub code (such as Spring, Flask) based on the OpenAPI specification.

Swagger Codegen supports custom templates.

Download page:https://swagger.io/tools/swagger-codegen/

** Use Cases:**

Operation example (generate Java client via command line), download Swagger Codegen CLI:

wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.35/swagger-codegen-cli-3.0.35.jar -O swagger-codegen-cli.jar

Generate code:

java -jar swagger-codegen-cli.jar generate \

  -i https://petstore.swagger.io/v2/swagger.json \

  -l java \

  -o ./petstore-api-client


Swagger Hub (Optional Extension)

Swagger Hub is Swagger's cloud platform, providing collaborative design, version management, and hosted documentation.

Swagger Hub supports team collaboration and API lifecycle management.

** Use Cases:**

Access address:https://swagger.io/api-hub/