登录
首页 >  Golang >  Go教程

Swagger在Debian系统配置指南

时间:2025-06-25 11:41:57 494浏览 收藏

本篇文章给大家分享《Swagger在Debian系统配置教程》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

Swagger在Debian上如何配置使用

在Debian系统上配置Swagger需要经历几个关键步骤,包括安装所需的软件包、配置API文档生成工具以及设置Swagger UI。以下是一个全面的指导流程:

1. 更新系统并安装软件包

首先,确保你的Debian系统处于最新状态:

sudo apt-get update
sudo apt-get upgrade

随后,安装与Swagger相关的软件包。Swagger一般会和Spring Boot项目结合使用,所以需要安装Spring Boot开发工具及其相关依赖:

sudo apt-get install spring-boot-devtools

2. 配置Spring Boot项目

在你的Spring Boot项目里,加入Swagger依赖项。如果使用Maven,则需在 pom.xml 文件内添加如下依赖:

io.springfoxspringfox-boot-starter3.0.0

若采用Gradle,则应在 build.gradle 文件中加入以下依赖:

implementation 'io.springfox:springfox-boot-starter:3.0.0'

3. 创建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"))
                .paths(PathSelectors.any())
                .build();
    }
}

这里,com.example.demo 应被替换为你控制器所在的具体包名。

4. 浏览Swagger UI

启动Spring Boot应用之后,可通过以下链接来浏览Swagger UI:

http://localhost:8080/swagger-ui.html

5. Docker配置(可选)

如欲在Docker容器内运行Spring Boot应用,可按以下步骤操作:

制作Dockerfile

在项目主目录下建立一个 Dockerfile:

FROM openjdk:11-jre-slim
COPY target/demo-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

打造Docker镜像

于项目主目录下执行以下指令来打造Docker镜像:

docker build -t demo .

启动Docker容器

构建完毕后,可用以下命令启动Docker容器:

docker run -p 8080:8080 demo

此时,便能经由 http://localhost:8080/swagger-ui.html 在浏览器中访问Swagger UI了。

今天关于《Swagger在Debian系统配置指南》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>