ubuntu

如何在Ubuntu搭建Swagger

小樊
36
2025-04-13 08:53:33
栏目: 智能运维

在Ubuntu上搭建Swagger主要包括安装Swagger UI和配置Swagger。以下是详细的步骤:

1. 安装Swagger UI

Swagger UI是一个用于展示和测试API的可视化工具。你可以通过npm(Node.js的包管理器)来安装Swagger UI。

步骤:

  1. 安装Node.js和npm: 如果你还没有安装Node.js和npm,可以使用以下命令来安装:

    sudo apt update
    sudo apt install nodejs npm
    
  2. 全局安装Swagger UI: 使用npm全局安装Swagger UI:

    sudo npm install -g swagger-ui-express
    
  3. 创建一个简单的Express应用: 创建一个新的目录并进入该目录:

    mkdir swagger-demo
    cd swagger-demo
    

    创建一个index.js文件,并添加以下内容:

    const express = require('express');
    const swaggerUi = require('swagger-ui-express');
    const YAML = require('yamljs');
    
    // Load Swagger document
    const swaggerDocument = YAML.load('./swagger.yaml');
    
    const app = express();
    
    // Serve Swagger docs
    app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
    
    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => {
      console.log(`Server is running on port ${PORT}`);
    });
    
  4. 创建Swagger文档: 在项目目录中创建一个swagger.yaml文件,并添加你的API文档。例如:

    swagger: '2.0'
    info:
      title: Sample API
      description: A sample API to demonstrate Swagger UI
      version: '1.0.0'
    paths:
      /users:
        get:
          summary: List all users
          responses:
            '200':
              description: An array of users
              schema:
                type: array
                items:
                  $ref: '#/definitions/User'
    definitions:
      User:
        type: object
        properties:
          id:
            type: integer
            format: int64
          name:
            type: string
          email:
            type: string
            format: email
    
  5. 运行应用: 使用以下命令启动你的Express应用:

    node index.js
    

    现在,你可以访问http://localhost:3000/api-docs来查看和测试你的API文档。

2. 配置Swagger

如果你使用的是其他框架(如Spring Boot),配置Swagger的方式会有所不同。以下是一个Spring Boot的示例:

步骤:

  1. 添加依赖: 在你的pom.xml文件中添加Swagger依赖:

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
    </dependency>
    
  2. 配置Swagger: 创建一个配置类来配置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.basePackage("com.example.demo"))
          .paths(PathSelectors.any())
          .build();
      }
    }
    
  3. 运行应用: 启动你的Spring Boot应用,然后访问http://localhost:8080/swagger-ui.html来查看和测试你的API文档。

通过以上步骤,你可以在Ubuntu上成功搭建Swagger并配置你的API文档。

0
看了该问题的人还看了