溫馨提示×

Swagger在Linux環境下如何配置與使用

小樊
57
2025-02-21 06:26:47
欄目: 智能運維

在Linux環境下配置和使用Swagger主要分為以下幾個步驟:

1. 安裝Node.js和npm

首先,確保你的Linux系統上已經安裝了Node.js和npm。如果沒有安裝,可以參考Node.js官方文檔進行安裝。

2. 安裝Swagger Editor和Swagger UI

安裝Swagger Editor

  1. 創建一個目錄來存放Swagger Editor:

    mkdir /opt/swagger
    cd /opt/swagger
    
  2. 下載并解壓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
    
  3. 初始化Node.js項目并安裝Express:

    npm init -y
    npm install express --save
    
  4. 在項目目錄中創建一個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}`);
    });
    
  5. 啟動服務器:

    node index.js
    

現在,你可以通過訪問http://localhost:8081來使用Swagger Editor。

安裝Swagger UI

  1. 在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
    
  2. 初始化Node.js項目并安裝Express:

    npm init -y
    npm install express --save
    
  3. 在項目目錄中創建一個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');
    });
    
  4. 啟動服務器:

    node index.js
    

現在,你可以通過訪問http://localhost:3000/api-docs來使用Swagger UI。

3. 配置Swagger

在你的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

創建一個配置類來啟用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();
  }
}

使用Swagger注解

在你的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!";
  }
}

4. 訪問Swagger文檔

啟動你的Spring Boot應用程序后,你可以通過訪問http://localhost:8080/swagger-ui.html來查看和使用Swagger文檔。

通過以上步驟,你就可以在Linux環境下成功配置和使用Swagger了。

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