Spring boot 整合Swagger2

注意:jdk 1.8以上才能運(yùn)行swagger2

1、導(dǎo)入jar包依賴

<dependency>

? ? <groupId>io.springfox</groupId>

? ? <artifactId>springfox-swagger2</artifactId>

? ? <version>2.9.2</version>

? ? <exclusions>

? ? ? ? <exclusion>

? ? ? ? ? ? <groupId>io.swagger</groupId>

? ? ? ? ? ? <artifactId>swagger-models</artifactId>

? ? ? ? </exclusion>

? ? </exclusions>

</dependency>

<dependency>

? ? <groupId>io.swagger</groupId>

? ? <artifactId>swagger-models</artifactId>

? ? <version>1.5.22</version>

</dependency>

<dependency>

? ? <groupId>io.springfox</groupId>

? ? <artifactId>springfox-swagger-ui</artifactId>

? ? <version>2.9.2</version>

</dependency>

2、通過配置類配置 Swagger


package com.dgood.cms.config;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

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.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Map;

//配置類注解

@Configuration

//開啟swagger2

@EnableSwagger2

//讀取application.properties中的swagger.config 配置

@ConfigurationProperties("swagger.config")

@Data

public class SwaggerConfig {

? ? //swagger cms的配置

? ? private Map<String, String> cms;

? ? //swagger weixin的配置

? ? private Map<String, String> weixin;

//創(chuàng)建cms的swagger

? ? @Bean

? ? public Docket createRestApi() {

? ? ? ? return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName(cms.get("groupName")).enable("1".equals(cms.get("enable")) ? true : false)

? ? ? ? ? ? ? ? .select()

? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage(cms.get("basePackage")))

? ? ? ? ? ? ? ? .paths(PathSelectors.any()).build();

? ? }

//創(chuàng)建weixin的swagger

? ? @Bean

? ? public Docket createWeiXinRestApi() {

? ? ? ? return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(weixinApiInfo()) //swagger2 配置

.groupName(weixin.get("groupName")) //設(shè)置分組名稱

.enable("1".equals(weixin.get("enable")) ? true : false)//是否開啟swagger? 如果是false 則通過swagger-ui.html 訪問不到

? ? ? ? ? ? ? ? .select()

? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage(weixin.get("basePackage")))? // 設(shè)置swagger2掃描包

? ? ? ? ? ? ? ? .paths(PathSelectors.any()).build();

? ? }

? ? private ApiInfo apiInfo() {

? ? ? ? return new ApiInfoBuilder()

? ? ? ? ? ? ? ? .title(cms.get("title"))? ?//swagger標(biāo)題

? ? ? ? ? ? ? ? .description(cms.get("description"))// 描述

? ? ? ? ? ? ? ? .version(cms.get("version"))//版本

? ? ? ? ? ? ? ? .build();

? ? }

? ? private ApiInfo weixinApiInfo() {

? ? ? ? return new ApiInfoBuilder()

? ? ? ? ? ? ? ? .title(weixin.get("title"))

? ? ? ? ? ? ? ? .description(weixin.get("description"))

? ? ? ? ? ? ? ? .version(weixin.get("version"))

? ? ? ? ? ? ? ? .build();

? ? }

}


application.properties 中swagger配置信息

#swagger 配置

swagger.config.cms.enable=1

swagger.config.cms.groupName=CMS

swagger.config.cms.title=CMS API 文檔

swagger.config.cms.description=CMS接口文檔

swagger.config.cms.version=v1.0.0

swagger.config.cms.basePackage=com.dgood.cms.controller

swagger.config.weixin.enable=1

swagger.config.weixin.groupName=微信

swagger.config.weixin.title=WeiXin API 文檔

swagger.config.weixin.description=微信接口文檔

swagger.config.weixin.version=v1.0.0

swagger.config.weixin.basePackage=com.dgood.weixin.controller


通過以上的配置swagger2就配置成功了

訪問http://localhost:8086/swagger-ui.html則可以看下swagger2頁面



swagger-ui.html 分為以上4個(gè)部分

第一部分--API分組:如果沒有配置分組默認(rèn)是default。通過Swagger實(shí)例Docket的groupName()方法即可配置分組

第二部分--基本描述:可以通過Swagger實(shí)例Docket的apiInfo()方法中的ApiInfo實(shí)例參數(shù)配置文檔信息

第三部分--請求接口列表:在組范圍內(nèi),只要被Swagger2掃描匹配到的請求都會在這里出現(xiàn)。

第四部分--實(shí)體列表:只要實(shí)體在請求接口的返回值上(即使是泛型),都能映射到實(shí)體項(xiàng)中!


第一部分:配置API分組

API分組通過configuration 中的進(jìn)行配置

?@Bean

? ? public Docket createRestApi() {

????return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName(cms.get("groupName")).enable("1".equals(cms.get("enable")) ? true : false)

? ? ? ? ? ? ? ? .select()

? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage(cms.get("basePackage")))

? ? ? ? ? ? ? ? .paths(PathSelectors.any()).build();

? ? }

?private ApiInfo apiInfo() {

? ? ? ? return new ApiInfoBuilder()

? ? ? ? ? ? ? ? .title(cms.get("title"))? ?//swagger標(biāo)題

? ? ? ? ? ? ? ? .description(cms.get("description"))// 描述

? ? ? ? ? ? ? ? .version(cms.get("version"))//版本

? ? ? ? ? ? ? ? .build();

? ? }

如果想配置多個(gè)分組則配置多個(gè)Docket即可

Swagger2的常用注解


Controller 上配置

@Api(tags ="用戶操作", value ="用戶")

@RequestMapping("/user")

@RestController

public class UserController {

@ApiOperation(value ="用戶登錄")

@ApiImplicitParams({

@ApiImplicitParam(name ="username", value ="用戶名", required =true, dataType ="String"),

? ? ? ? ? ? @ApiImplicitParam(name ="password", value ="密碼", required =true, dataType ="String")

})

@ApiResponses({

@ApiResponse(code =1, message ="登錄成功"),

? ? ? ? ? ? @ApiResponse(code =2, message ="登錄失敗")

})

@PostMapping({"/login"})

public Stringlogin(String username, String password) {

if ("admin".equals(username) &&"admin".equals(password)) {

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

? ? ? ? ? ? request.getSession().setAttribute("user", username);

? ? ? ? ? ? return "1";

? ? ? ? }else {

return "2";

? ? ? ? }

}

}

model實(shí)體配置

@Data

@Slf4j

@ApiModel(value="用戶實(shí)體類",description ="用戶")

public class User {

@ApiModelProperty(value ="id",example ="1")

private Longid;

? ? @ApiModelProperty(value ="用戶名",example ="admin")

private Stringusername;

? ? @ApiModelProperty(value ="密碼",example ="password")

private Stringpassword;

}

最終效果


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,750評論 1 375
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,703評論 2 380

推薦閱讀更多精彩內(nèi)容