首页 > 新闻中心 > 技术百科

Linux下Swagger与Spring Boot如何集成 返回列表

月夜之吻2025-03-14 00:00:00编辑发布,已经有个小可爱看过这篇文章啦

在Linux系统中,借助Swagger集成Spring Boot项目,可以高效便捷地生成RESTful API文档。以下步骤将指导您完成集成过程:

第一步:添加依赖

在您的Spring Boot项目的pom.xml文件中,添加以下Maven依赖:


    io.springfox
    springfox-swagger2
    2.9.2


    io.springfox
    springfox-swagger-ui
    2.9.2

第二步:Swagger配置

创建一个名为SwaggerConfig.java的Java配置类,并添加如下代码:

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.controller")) // 请替换为您的控制器包名
                .paths(PathSelectors.any())
                .build();
    }
}

请务必将com.example.demo.controller替换为您实际的控制器包名。

第三步:访问Swagger UI

启动Spring Boot应用后,在浏览器中访问以下URL:

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

您将看到Swagger UI界面,其中包含了所有已注册的API接口信息。您可以直接在此界面测试您的API。

重要提示: 如果在Linux环境下无法访问http://localhost:8080/swagger-ui.html,请检查您的防火墙设置,确保8080端口已开放。 此外,请确认Spring Boot应用已成功启动并在监听8080端口。

  • html
  • 接口
  • ui
  • 第二步
  • linux
  • 在此
  • 并在
  • 第三步
  • 您的
  • Java

热门新闻

来电咨询