温馨提示×

Ubuntu Swagger如何快速上手

小樊
42
2025-03-04 04:23:39
栏目: 智能运维
Ubuntu服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Ubuntu上快速上手Swagger,通常涉及以下几个步骤:

1. 安装Swagger UI

首先,你需要安装Swagger UI。可以通过以下命令在Ubuntu上安装Swagger UI:

sudo apt update
sudo apt install -y openjdk-11-jre-headless
wget https://repo1.maven.org/maven2/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.html
unzip springfox-swagger-ui-2.9.2.html
sudo mv springfox-swagger-ui-2.9.2 /var/www/html/swagger-ui

2. 配置Swagger UI

编辑 /var/www/html/swagger-ui/index.html 文件,找到以下行并进行修改:

<script>
  window.onload = function() {
    // Begin Swagger UI call region
    const ui = SwaggerUIBundle({
      url: "http://petstore.swagger.io/v2/api-docs",
      dom_id: '#swagger-ui',
      deepLinking: true,
      presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIStandalonePreset
      ],
      plugins: [
        SwaggerUIBundle.plugins.DownloadUrl
      ],
      layout: "StandaloneLayout"
    });
    // End Swagger UI call region
    window.ui = ui;
  };
</script>

3. 启动Web服务器

你可以使用Apache或Nginx作为Web服务器。以下是使用Apache的示例:

sudo a2ensite default.conf
sudo systemctl restart apache2

或者使用Nginx:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup
sudo nano /etc/nginx/sites-available/default

修改 server 块中的 rootindex 指令:

server {
  listen 80 default_server;
  listen [::]: 80 default_server;
  root /var/www/html;
  index index.html index.htm;
  server_name _;
  location / {
    try_files $uri $uri/ =404;
  }
}

保存并退出编辑器,然后重启Nginx:

sudo systemctl restart nginx

4. 访问Swagger UI

现在,你应该能够通过浏览器访问 http://your_server_ip/swagger-ui 来查看和使用Swagger UI。

5. 在Spring Boot项目中集成Swagger(可选)

如果你使用的是Spring Boot项目,可以通过以下步骤集成Swagger:

添加依赖

在项目的 pom.xml 文件中添加以下依赖:

<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>

配置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.any())
                .paths(PathSelectors.any())
                .build();
    }
}

添加文档内容

在完成了上述配置后,启动Spring Boot项目后,访问 http://localhost:8080/swagger-ui.html(假设服务端口为8080),可以看到Swagger UI界面,其中展示了项目中定义的所有API接口及其详细信息。

通过以上步骤,你应该能够在Ubuntu上快速上手Swagger。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:Swagger在Ubuntu上如何快速部署

0