溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何集成Swagger3

發(fā)布時間:2021-12-29 12:44:53 來源:億速云 閱讀:323 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹SpringBoot如何集成Swagger3,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一,什么是swagger?

1,  Swagger 是一個規(guī)范和完整的文檔框架,

    用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)文檔

    官方網(wǎng)站:https://swagger.io/

2,使用swagger要注意的地方:

     在生產(chǎn)環(huán)境中必須關(guān)閉swagger,

     它本身只用于前后端工程師之間的溝通,

     可以專門使用一臺內(nèi)部服務(wù)器來展示ui供訪問,

     即使在這上面要做好安全措施

3,  因為swagger3.0.0已發(fā)布,本文使用了最新版

     如果有還在用2.x版本的請參考時注意區(qū)分

二,SpringBoot 集成swagger3

 pom.xml 集成Swagger3依賴

<!-- swagger3 接口文檔生成器 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.plugin</groupId>
					<artifactId>spring-plugin-core</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework.plugin</groupId>
					<artifactId>spring-plugin-metadata</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

Swagger3 配置對象定義

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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
 
@Configuration
@EnableOpenApi
public class Swagger3Config {
	@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.digipower.controller"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("在奮斗的大道上 - 微信預(yù)約查檔系統(tǒng)").termsOfServiceUrl("https://blog.csdn.net/zhouzhiwengang")
				.description("API接口")
				.contact(new Contact("https://blog.csdn.net/zhouzhiwengang","",""))
				.version("3.0").build();
    }
 
}

SpringBoot 集成Swagger3 接口文檔效果截圖

SpringBoot如何集成Swagger3

 SpringBoot 訪問Swagger3接口文檔地址

默認接口訪問地址:http://192.168.0.1:5988/swagger-ui/

三,swagger3 注解標(biāo)簽使用

@ApiModel用于類上面說明功能

@ApiModelProperty用于字段上說明功能

示列截圖:

SpringBoot如何集成Swagger3

@Api用來指定一個controller中的各個接口的通用說明

@ApiOperation用來說明一個方法

@ApiImplicitParams:用來包含多個包含多個 @ApiImplicitParam

@ApiImplicitParam:用來說明一個請求參數(shù) 

示列截圖:

SpringBoot如何集成Swagger3

 溫馨提示:針對任何請求參數(shù)結(jié)構(gòu)說明,請一定記得要添加@ApiParam 標(biāo)簽,如果不添加@ApiParam標(biāo)簽,在Swagger3文檔中針對參數(shù)結(jié)構(gòu)說明就是空

以上是“SpringBoot如何集成Swagger3”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI