Linux下Swagger与Spring Boot如何集成

Linux下Swagger与Spring Boot如何集成

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

第一步:添加依赖

在您的spring boot项目的pom.xml文件中,添加以下maven依赖:

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

创建一个名为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端口。

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享