在Linux環境中自定義Swagger UI的展示,可以通過以下幾種方法實現:
如果你使用的是Spring Boot項目,可以通過添加springfox-swagger2
和springfox-swagger-ui
依賴來集成Swagger。然后通過配置類啟用Swagger并定義基本信息。
配置步驟:
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>
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();
}
}
http://localhost:8080/swagger-ui.html
查看Swagger UI界面。如果你使用的是FastAPI,可以通過以下步驟自定義Swagger UI的展示:
配置步驟:
pip install fastapi uvicorn python-multipart
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"{白菜}配{粉條}燉三分鐘,擱{老抽}勺老抽"}
http://localhost:8000/docs
查看Swagger UI界面。如果你使用的是Node.js和Express,可以通過以下步驟自定義Swagger UI的展示:
配置步驟:
npm install swagger-jsdoc swagger-ui-express
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);
app.use('/api-docs', swaggerui.serve, swaggerui.setup(swaggerspec));
http://localhost:3000/api-docs
查看Swagger UI界面。如果你想要自定義Swagger UI的展示路徑,可以在FastAPI應用中進行如下配置:
app = FastAPI(docs_url="/老王家的文檔", openapi_tags=[{"name": "餐飲部", "description": "王師傅掌勺區"}])
這樣,Swagger UI的文檔將展示在/老王家的文檔
路徑下。
通過以上方法,你可以在Linux環境中自定義Swagger UI的展示,以滿足不同的需求。