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ù)、請(qǐng)求示例、響應(yīng)示例等信息。這使得開發(fā)人員、測(cè)試人員和客戶端開發(fā)人員能夠輕松地理解和使用 API。
可視化交互界面: Swagger 生成的文檔通常包括一個(gè)可視化的交互界面,允許用戶測(cè)試 API 端點(diǎn)而無(wú)需編寫任何代碼。這簡(jiǎn)化了開發(fā)和測(cè)試的過(guò)程。
標(biāo)準(zhǔn)化接口設(shè)計(jì): Swagger 鼓勵(lì)開發(fā)團(tuán)隊(duì)使用標(biāo)準(zhǔn)的注解或規(guī)范來(lái)定義 API,這樣可以更容易地創(chuàng)建一致性的 API 設(shè)計(jì)。這對(duì)于多個(gè)開發(fā)團(tuán)隊(duì)協(xié)同工作的大型項(xiàng)目特別有用。
客戶端代碼生成: Swagger 可以生成客戶端代碼,使客戶端開發(fā)人員能夠使用 API 而無(wú)需手動(dòng)編寫 HTTP 請(qǐng)求和數(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> <!-- 使用正確的版本號(hào) -->
</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!";
}
}
訪問(wèn) Swagger UI:?jiǎn)?dòng)你的應(yīng)用程序后,你可以通過(guò)瀏覽器訪問(wèn) 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)建、測(cè)試和文檔化 API,并提供了可視化的交互界面,以改進(jìn)開發(fā)流程和加速 API 的采用。
到此這篇關(guān)于Springboot配置Swagger的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot配置swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud Gateway全局通用異常處理的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Mybatis-Plus可能導(dǎo)致死鎖的問(wèn)題分析及解決辦法
這篇文章給大家主要介紹了Mybatis-Plus可能導(dǎo)致死鎖的問(wèn)題分析及解決辦法,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12
SpringBoot配置文件注入值的簡(jiǎn)單實(shí)現(xiàn)
本文主要介紹了SpringBoot配置文件注入值的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-11-11
Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式詳析
這篇文章主要給大家介紹了關(guān)于Java如何將字符串轉(zhuǎn)為數(shù)字int的三種方式,在編程中我們經(jīng)常需要進(jìn)行各種數(shù)據(jù)類型之間的轉(zhuǎn)換操作,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
RabbitMQ延遲隊(duì)列及消息延遲推送實(shí)現(xiàn)詳解
這篇文章主要介紹了RabbitMQ延遲隊(duì)列及消息延遲推送實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
java實(shí)現(xiàn)動(dòng)態(tài)代理示例分享
動(dòng)態(tài)代理作為代理模式的一種擴(kuò)展形式,廣泛應(yīng)用于框架(尤其是基于AOP的框架)的設(shè)計(jì)與開發(fā),本文將通過(guò)實(shí)例來(lái)講解Java動(dòng)態(tài)代理的實(shí)現(xiàn)過(guò)程。2014-03-03
Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)圖片上傳功能詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)圖片上傳功能,包含從數(shù)據(jù)庫(kù)拿圖片傳遞前端渲染,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03

