Springboot配置Swagger的實(shí)現(xiàn)示例
Swagger 是什么
Swagger 是一個(gè)用于構(gòu)建、文檔和調(diào)用 RESTful Web 服務(wù)的強(qiáng)大工具。它提供了以下幾方面的好處:
自動(dòng)生成 API 文檔: Swagger 可以自動(dòng)生成 API 文檔,包括接口的描述、輸入?yún)?shù)、輸出參數(shù)、請求示例、響應(yīng)示例等信息。這使得開發(fā)人員、測試人員和客戶端開發(fā)人員能夠輕松地理解和使用 API。
可視化交互界面: Swagger 生成的文檔通常包括一個(gè)可視化的交互界面,允許用戶測試 API 端點(diǎn)而無需編寫任何代碼。這簡化了開發(fā)和測試的過程。
標(biāo)準(zhǔn)化接口設(shè)計(jì): Swagger 鼓勵(lì)開發(fā)團(tuán)隊(duì)使用標(biāo)準(zhǔn)的注解或規(guī)范來定義 API,這樣可以更容易地創(chuàng)建一致性的 API 設(shè)計(jì)。這對于多個(gè)開發(fā)團(tuán)隊(duì)協(xié)同工作的大型項(xiàng)目特別有用。
客戶端代碼生成: Swagger 可以生成客戶端代碼,使客戶端開發(fā)人員能夠使用 API 而無需手動(dòng)編寫 HTTP 請求和數(shù)據(jù)模型的代碼。這減少了代碼重復(fù)和錯(cuò)誤。
減少文檔維護(hù)成本: Swagger 自動(dòng)生成的文檔與實(shí)際代碼同步,因此當(dāng) API 發(fā)生更改時(shí),文檔也會(huì)相應(yīng)更新,減少了手動(dòng)維護(hù)文檔的工作。
安全集成: Swagger 可以與身份驗(yàn)證和授權(quán)機(jī)制集成,幫助開發(fā)人員更輕松地確保 API 的安全性。
代碼注釋: 使用 Swagger 注解,可以更清晰地記錄 API 的用途、參數(shù)、響應(yīng)等信息。這有助于提高代碼的可維護(hù)性。
Swagger配置springboot
代碼展示
添加 Maven 依賴:首先,在你的 Spring Boot 項(xiàng)目的 pom.xml 文件中,添加 Swagger2 依賴。以下是一個(gè)示例:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <!-- 使用正確的版本號 --> </parent> <groupId>com.example</groupId> <artifactId>swaggerdemo01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>swaggerdemo01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.vaadin.external.google</groupId> <artifactId>android-json</artifactId> <version>0.0.20131108.vaadin1</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.32</version> <scope>compile</scope> </dependency> </dependencies> </project>
創(chuàng)建 Swagger 配置類:在你的項(xiàng)目中創(chuàng)建一個(gè)配置類,通常命名為 SwaggerConfig,并添加 @Configuration 注解。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; 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 createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.example.swaggerdemo.controller")) .paths(PathSelectors.any()) .build().apiInfo(new ApiInfoBuilder() .title("雷達(dá)數(shù)據(jù)修正算法相關(guān)接口") .description("雷達(dá)數(shù)據(jù)修正算法邏輯展示") .version("9.0") .contact(new Contact("z s","blog.git.net"," ")) .license("The Apache License") .licenseUrl("http://www.s t.com/") .build()); } }
啟用 Swagger:在你的 Spring Boot 應(yīng)用程序主類上添加 @EnableSwagger2 注解。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 public class Swaggerdemo01Application { public static void main(String[] args) { SpringApplication.run(Swaggerdemo01Application.class, args); } }
編寫 API 文檔注釋:在你的控制器類和方法上使用 Swagger 注解編寫接口文檔的注釋,包括參數(shù)、響應(yīng)等信息。示例:
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Api(tags = "示例控制器") public class AccountController { @GetMapping("/hello") @ApiOperation("獲取歡迎消息") public String hello() { return "Hello, World!"; } }
訪問 Swagger UI:啟動(dòng)你的應(yīng)用程序后,你可以通過瀏覽器訪問 Swagger UI,通常在 http://localhost:8080/swagger-ui.html。
這些步驟將幫助你在 Spring Boot 項(xiàng)目中整合 Swagger,以便生成和展示 API 文檔。你可以根據(jù)你的項(xiàng)目需求和喜好進(jìn)行更多的配置和定制。
總結(jié)
總之,Swagger 是一種提高 API 開發(fā)和維護(hù)效率的工具,它使開發(fā)者能夠更輕松地構(gòu)建、測試和文檔化 API,并提供了可視化的交互界面,以改進(jìn)開發(fā)流程和加速 API 的采用。
到此這篇關(guān)于Springboot配置Swagger的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot配置swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)百度AOI數(shù)據(jù)的解析與轉(zhuǎn)換
Java作為一種成熟且廣泛應(yīng)用的編程語言,具有跨平臺(tái)、面向?qū)ο?、安全性高等特點(diǎn),非常適合用于開發(fā)各種類型的應(yīng)用程序,本文為大家整理了基于Java的AOI數(shù)據(jù)解析與轉(zhuǎn)換的實(shí)現(xiàn)方法,需要的可以參考下2025-02-02SpringCloud Eureka Provider及Consumer的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud Eureka 提供者及調(diào)用者的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

Mybatis中Collection集合標(biāo)簽的使用詳解

Java利用遞歸算法實(shí)現(xiàn)查詢斐波那契數(shù)