Linux Swagger與Spring Boot如何集成

spring Boot項目輕松集成Swagger:詳細步驟指南

本文將指導您如何在spring boot項目中快速集成swagger,方便api文檔的生成和測試。 以下步驟將幫助您輕松完成集成,并開始使用swagger ui瀏覽您的api。

Linux Swagger與Spring Boot如何集成

第一步:添加依賴

在您的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>

請根據您的實際需求選擇合適的Swagger版本。

第二步:Swagger配置

創建一個名為SwaggerConfig.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")) // 請替換成您的Controller包名                 .paths(PathSelectors.any())                 .build();     } }

請務必將com.example.demo.controller替換為您實際的Controller包名。

第三步:訪問Swagger UI

啟動您的Spring Boot應用程序后,在瀏覽器中訪問以下URL:

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

(假設您的應用程序運行在8080端口)。您應該能夠看到Swagger UI界面,其中列出了您的API文檔,方便您進行測試和瀏覽。

現在,您已成功將Swagger集成到您的Spring Boot項目中。 您可以使用Swagger UI直觀地查看和測試您的API。

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享