在Linux環境下配置和使用Swagger主要分為以下幾個步驟:
首先,確保你的Linux系統上已經安裝了Node.js和npm。如果沒有安裝,可以參考Node.js官方文檔進行安裝。
創建一個目錄來存放Swagger Editor:
mkdir /opt/swagger
cd /opt/swagger
下載并解壓Swagger Editor的最新版本:
wget https://github.com/swagger-api/swagger-editor/archive/refs/tags/v3.16.1.tar.gz
tar -xvf v3.16.1.tar.gz
rm v3.16.1.tar.gz
初始化Node.js項目并安裝Express:
npm init -y
npm install express --save
在項目目錄中創建一個index.js
文件,并添加以下內容來啟動HTTP服務器:
const express = require('express');
const app = express();
const port = 8081;
app.use('/swagger-editor', express.static('swagger-editor-3.16.1'));
app.listen(port, () => {
console.log(`Swagger Editor is running at http://localhost:${port}`);
});
啟動服務器:
node index.js
現在,你可以通過訪問http://localhost:8081
來使用Swagger Editor。
在Swagger Editor的目錄中,下載并解壓Swagger UI的最新版本:
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v3.48.0.tar.gz
tar -xvf v3.48.0.tar.gz
rm v3.48.0.tar.gz
初始化Node.js項目并安裝Express:
npm init -y
npm install express --save
在項目目錄中創建一個index.js
文件,并添加以下內容來啟動HTTP服務器:
const express = require('express');
const swaggerui = require('swagger-ui-express');
const swaggerjsdoc = require('swagger-jsdoc');
const swaggerdefinition = {
openapi: '3.0.0',
info: {
title: 'My API Documentation',
version: '1.0.0',
description: 'This is my API documentation',
},
servers: [{ url: 'http://localhost:3000', description: 'Development server' }],
};
const options = {
swaggerDefinition,
apis: ['./routes/*.js'], // 指向API文檔的路徑
};
const swaggerspec = swaggerjsdoc(options);
app.use('/api-docs', swaggerui.serve, swaggerui.setup(swaggerspec));
app.listen(3000, () => {
console.log('Swagger UI is running at http://localhost:3000/api-docs');
});
啟動服務器:
node index.js
現在,你可以通過訪問http://localhost:3000/api-docs
來使用Swagger UI。
在你的Spring Boot項目中配置Swagger,可以參考以下步驟:
在你的pom.xml
文件中添加以下依賴:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
創建一個配置類來啟用Swagger:
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.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
}
在你的Controller中使用Swagger注解來描述API:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
@Api(tags = "Sample API")
public class SampleController {
@GetMapping("/hello")
@ApiOperation(value = "Say hello", notes = "This is a sample API to demonstrate Swagger usage")
public String sayHello() {
return "Hello, World!";
}
}
啟動你的Spring Boot應用程序后,你可以通過訪問http://localhost:8080/swagger-ui.html
來查看和使用Swagger文檔。
通過以上步驟,你就可以在Linux環境下成功配置和使用Swagger了。