溫馨提示×

Linux環境中Swagger UI如何自定義展示

小樊
72
2025-03-08 00:03:32
欄目: 智能運維

在Linux環境中自定義Swagger UI的展示,可以通過以下幾種方法實現:

1. 使用Springfox-Swagger2和Springfox-Swagger-UI

如果你使用的是Spring Boot項目,可以通過添加springfox-swagger2springfox-swagger-ui依賴來集成Swagger。然后通過配置類啟用Swagger并定義基本信息。

配置步驟:

  1. pom.xml中添加依賴:
<dependencies>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>YOUR_DESIRED_VERSION</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>YOUR_DESIRED_VERSION</version>
    </dependency>
    <!-- 其他依賴 -->
</dependencies>
  1. 創建配置類啟用Swagger:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}
  1. 啟動Spring Boot項目后,訪問http://localhost:8080/swagger-ui.html查看Swagger UI界面。

2. 使用FastAPI和Swagger UI

如果你使用的是FastAPI,可以通過以下步驟自定義Swagger UI的展示:

配置步驟:

  1. 安裝FastAPI和相關模塊:
pip install fastapi uvicorn python-multipart
  1. 創建FastAPI應用并添加Swagger UI支持:
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title="老王家豬肉燉粉條子API")

class Item(BaseModel):
    name: str
    qty: int
    price: float

@app.get("/亂燉/")
async def read_items(白菜: str = "二斤", 粉條: str = "一把", 老抽: int = 3):
    return {"火候掌握人": "王師傅", "成品": f"{白菜}{粉條}燉三分鐘,擱{老抽}勺老抽"}
  1. 訪問http://localhost:8000/docs查看Swagger UI界面。

3. 使用Swagger UI和Express

如果你使用的是Node.js和Express,可以通過以下步驟自定義Swagger UI的展示:

配置步驟:

  1. 安裝Swagger UI和相關模塊:
npm install swagger-jsdoc swagger-ui-express
  1. 初始化Swagger JSdoc并配置:
const swaggerjsdoc = require('swagger-jsdoc');
const swaggerui = require('swagger-ui-express');

const swaggerdefinition = {
    openapi: '3.0.0',
    info: {
        title: '我的api文檔',
        version: '1.0.0',
        description: '這是我的api文檔的描述',
    },
    servers: [{ url: 'http://localhost:3000', description: '開發服務器', }],
};

const options = {
    swaggerdefinition,
    apis: ['./routes/*.js'], // 指向api文檔的路徑
};

const swaggerspec = swaggerjsdoc(options);
  1. 使用Swagger UI提供可視化界面:
app.use('/api-docs', swaggerui.serve, swaggerui.setup(swaggerspec));
  1. 啟動Node.js應用后,通過訪問http://localhost:3000/api-docs查看Swagger UI界面。

4. 自定義Swagger UI路徑

如果你想要自定義Swagger UI的展示路徑,可以在FastAPI應用中進行如下配置:

app = FastAPI(docs_url="/老王家的文檔", openapi_tags=[{"name": "餐飲部", "description": "王師傅掌勺區"}])

這樣,Swagger UI的文檔將展示在/老王家的文檔路徑下。

通過以上方法,你可以在Linux環境中自定義Swagger UI的展示,以滿足不同的需求。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女