Swagger Core
Swagger is an open-source toolset 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 the API's functionality without accessing source code or other documentation.
The core value of Swagger lies in:
* **Standardization**: Provides a unified API description format
* **Visualization**: Automatically generates interactive API documentation
* **Testability**: Allows testing API endpoints directly within the documentation
* **Code Generation**: Supports client and server code generation for multiple languages
### Swagger Core Components
| Component | Input | Output | Applicable Phase |
| --- | --- | --- | --- |
| **Swagger Editor** | Manually written YAML/JSON | Real-time preview 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 (Design API) β OpenAPI Specification File (YAML/JSON) β Swagger UI (Display Documentation) or Swagger Codegen (Generate Code)
!(#)
## 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 validation.
Swagger Editor code generation, save, and export features are now part of the SmartBear API Hub. Access it here: [https://try.platform.smartbear.com/new-organization](https://try.platform.smartbear.com/new-organization).
**Usage Scenarios:**
* Quickly design API prototypes
* Learn OpenAPI specification syntax
Access the online editor Swagger Editor: [https://editor.swagger.io/](https://editor.swagger.io/)
Enter the following YAML code (defines a simple GET /users interface):
## Example
span style="color: green;">
openapi: 3.0.3
info:
title: TUTORIAL User Management System
version: 1.0.0
paths:
/users:
get:
summary: Get user list
responses:
'200':
description: Successfully returned user list
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string
The right side will render the API documentation and interactive UI in real-time.
!(#)
* * *
## Swagger UI
Swagger UI converts OpenAPI specifications into visual, interactive documentation, supporting direct API testing within the browser.
Swagger UI automatically generates request examples, response models, and a debugging interface.
Swagger UI: [https://swagger.io/tools/swagger-ui/](https://swagger.io/tools/swagger-ui/)
**Usage Scenarios:**
* Team sharing of API documentation
* Frontend developers debugging interfaces
Integration example (using Node.js), install dependencies:
npm install swagger-ui-express swagger-jsdoc
Create a 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 for annotations 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 (e.g., Java, Python) or server stub code (e.g., Spring, Flask) based on the OpenAPI specification.
Swagger Codegen supports custom templates.
Download page: [https://swagger.io/tools/swagger-codegen/](https://swagger.io/tools/swagger-codegen/)
**Usage Scenarios:**
* Quickly generate client code for API calls
* Automated server interface development
Operation example (command line generation for Java client), 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.
**Usage Scenarios:**
* Enterprise-level API project management
* Distributed teams requiring online collaboration
Access address: [https://swagger.io/api-hub/](https://swagger.io/api-hub/)
YouTip