SpringBoot整合swagger的操作指南
簡介
Swagger 是一個開源的框架,用于設(shè)計、構(gòu)建、文檔化和使用 RESTful 風(fēng)格的 Web 服務(wù)。Spring Boot 是一個用于構(gòu)建獨(dú)立的、基于生產(chǎn)級別的 Spring 應(yīng)用程序的框架。它可以集成 Swagger 來簡化 RESTful API 的開發(fā)和文檔生成。
通過集成 Swagger,你可以在 Spring Boot 應(yīng)用程序中自動生成 API 文檔,這些文檔描述了你的 API 的各種端點(diǎn)、請求參數(shù)、響應(yīng)等詳細(xì)信息。Swagger 還提供了一個交互式 UI,讓開發(fā)人員可以方便地瀏覽和測試 API。
實(shí)例
版本依賴
JDK17
SpringBoot 2.5.4
swagger 3.0.0
knife4j-spring-boot-starter 3.0.2
引入依賴
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> </parent> <groupId>com.example</groupId> <artifactId>springboot-swagger</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>17</java.version> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入spring web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入swagger3 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <!--Knife4j--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.SpringbootSwaggerSampleApplication</mainClass> </configuration> </plugin> </plugins> </build>
SwaggerConfig
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.ResponseBuilder; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.service.Response; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import java.util.ArrayList; import java.util.List; /** * @Description: (Swagger配置) * @Date 2023/09/07 */ @Configuration @EnableOpenApi @EnableKnife4j public class SwaggerConfig { /** * swagger3的配置文件 */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build() //.globalRequestParameters(getGlobalRequestParameters()) .globalResponses(HttpMethod.GET, getGlobalResponseMessage()) .globalResponses(HttpMethod.POST, getGlobalResponseMessage()) .globalResponses(HttpMethod.DELETE, getGlobalResponseMessage()) .globalResponses(HttpMethod.PUT, getGlobalResponseMessage()); } /** * 構(gòu)建 api文檔的詳細(xì)信息函數(shù),注意這里的注解引用的是哪個 */ private ApiInfo apiInfo() { // 獲取工程名稱 String projectName = System.getProperty("user.dir"); return new ApiInfoBuilder() .title(projectName.substring(projectName.lastIndexOf("\\") + 1) + " API接口文檔") .contact(new Contact("coderJim", "https://www.example.com", "coderJim@163.com")) .version("1.0") .description("API文檔") .build(); } /** * 生成通用響應(yīng)信息 * * @return */ private List<Response> getGlobalResponseMessage() { List<Response> responseList = new ArrayList<>(); responseList.add(new ResponseBuilder().code("404").description("找不到資源").build()); return responseList; } }
User
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.ToString; /** * @Description: (用一句話描述該文件做什么) * @date 2023/09/07 */ @Data @ToString @ApiModel(value = "用戶實(shí)體類", description = "用戶信息描述類") public class User { @ApiModelProperty(value = "用戶id") private Integer id; @ApiModelProperty(value = "用戶名") private String username; @ApiModelProperty(value = "用戶密碼") private String password; }
UserController
import com.example.entity.User; import io.swagger.annotations.*; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; /** * * @Package com.example.controller * @Description: (用一句話描述該文件做什么) * @date 2023/09/07 */ @RestController @Api(tags = "用戶數(shù)據(jù)接口") // @Api 注解標(biāo)注在類上用來描述整個 Controller 信息。 public class UserController { /** * @ApiOperation 注解標(biāo)注在方法上,用來描述一個方法的基本信息。 */ @ApiOperation(value = "修改用戶", notes = "傳入用戶信息進(jìn)行更新修改") @PutMapping("/user") public String updateUser(@RequestBody User user){ return user.toString(); } /** * @ApiImplicitParam 注解標(biāo)注在方法上,用來描述方法的參數(shù)。其中 paramType 是指方法參數(shù)的類型,有如下可選值: * path:參數(shù)獲取方式為 @PathVariable * query:參數(shù)獲取方式為 @RequestParam * header:參數(shù)獲取方式為 @RequestHeader * body * form */ @ApiOperation(value ="查詢用戶", notes = "根據(jù) id 查詢用戶") @ApiImplicitParam(paramType = "path", name = "id", value = "用戶 id", required = true) @GetMapping("/user/{id}") public String getUserById(@PathVariable Integer id){ return "查找的用戶id是:" + id; } /** * 如果有多個參數(shù),可以將多個參數(shù)的 @ApiImplicitParam 注解放到 @ApiImplicitParams 中 */ @ApiOperation(value = "新增用戶", notes = "根據(jù)傳入的用戶名和密碼添加一個新用戶") @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", name = "username", value = "用戶名", required = true, defaultValue = "test"), @ApiImplicitParam(paramType = "query", name = "password", value = "密碼", required = true, defaultValue = "123") }) @PostMapping("/user") public String addUser(@RequestParam String username, @RequestParam String password) { return "新增用戶:" + username + " " + password; } /** * @ApiResponse 是對響應(yīng)結(jié)果的描述。code 表示響應(yīng)碼,message 為相應(yīng)的描述信息。如果有多個 @ApiResponse,則放在一個 @ApiResponses 中 */ @ApiOperation(value = "刪除用戶", notes = "根據(jù) id 刪除用戶") @ApiResponses({ @ApiResponse(code = 200, message = "刪除成功!"), @ApiResponse(code = 500, message = "刪除失?。?) }) @DeleteMapping("/user/{id}") public Integer deleteUserById(@PathVariable Integer id) { return id; } /** * @ApiIgnore 注解表示不對某個接口生成文檔。 */ @ApiIgnore @GetMapping("/user/test") public String test() { return "這是一個測試接口,不需要在api文檔中顯示。"; } }
測試結(jié)果
啟動打開鏈接:http://127.0.0.1:8080/swagger-ui/index.html
打開鏈接:http://127.0.0.1:8080/doc.html
以上就是SpringBoot整合swagger的操作指南的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合swagger的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互相關(guān)介紹全面總結(jié)
Spring MVC 是 Spring 提供的一個基于 MVC 設(shè)計模式的輕量級 Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),這篇文章主要介紹了SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互2022-10-10quartz實(shí)現(xiàn)定時功能實(shí)例詳解(servlet定時器配置方法)
Quartz是一個完全由java編寫的開源作業(yè)調(diào)度框架,下面提供一個小例子供大家參考,還有在servlet配置的方法2013-12-12Kotlin基礎(chǔ)教程之?dāng)?shù)據(jù)類型
這篇文章主要介紹了Kotlin基礎(chǔ)教程之?dāng)?shù)據(jù)類型的相關(guān)資料,需要的朋友可以參考下2017-05-05SpringMVC實(shí)現(xiàn)登錄與注冊功能的詳細(xì)步驟
本文介紹了如何通過Maven配置依賴,創(chuàng)建前端登錄和注冊頁面,并實(shí)現(xiàn)后端邏輯,詳細(xì)步驟包括配置文件、創(chuàng)建User類、配置中文過濾器及DispatcherServlet,并使用Spring?MVC和JQuery處理前端請求,需要的朋友可以參考下2024-11-11java 調(diào)用wsdl協(xié)議接口簡單實(shí)用方法最新推薦
文章介紹了如何使用POM導(dǎo)入依賴,并編寫一個測試類來調(diào)用不同的Web服務(wù)接口,通過訪問接口地址,我們可以獲取請求和返回的body,并進(jìn)一步解析返回的JSON結(jié)果,感興趣的朋友一起看看吧2025-03-03Springboot詳解實(shí)現(xiàn)食品倉庫管理系統(tǒng)流程
這是一個使用Springboot開發(fā)的食品倉庫管理系統(tǒng),是為商家提供商品貨物進(jìn)銷存的信息化管理系統(tǒng),具有一個倉庫管理系統(tǒng)該有的所有功能,感興趣的朋友快來看看吧2022-06-06基于Springboot的漫畫網(wǎng)站平臺設(shè)計與實(shí)現(xiàn)
本文將基于Springboot實(shí)現(xiàn)開發(fā)一個漫畫主題的網(wǎng)站,實(shí)現(xiàn)一個比漂亮的動漫連載的網(wǎng)站系統(tǒng),界面設(shè)計優(yōu)雅大方,比較適合做畢業(yè)設(shè)計和課程設(shè)計使用,需要的可以參考一下2022-08-08