Swagger体验版
生活随笔
收集整理的這篇文章主要介紹了
Swagger体验版
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、Swagger簡(jiǎn)介
1.1前后端分離
前端—> 前端控制層、視圖層
后端—> 后端控制層、服務(wù)層、數(shù)據(jù)訪問(wèn)層
前后端通過(guò)API進(jìn)行交互
前后端相對(duì)獨(dú)立且松耦合
1.2產(chǎn)生的問(wèn)題
前后端集成,前端或者后端無(wú)法做到“及時(shí)協(xié)商,盡早解決”,最終導(dǎo)致問(wèn)題集中爆發(fā)
1.3解決方案
首先定義schema【計(jì)劃的提綱】,并實(shí)時(shí)跟蹤最新的API,降低集成風(fēng)險(xiǎn);
因此,Swagger應(yīng)運(yùn)而生
1.4Swagger
(一)號(hào)稱世界上最流行的API框架
(二)Restful Api 文檔在線自動(dòng)生成器 => Api文檔與API定義同步更新
(三)直接運(yùn)行,在線測(cè)試API
(四)支持多種語(yǔ)言【如java、PHP等】
(五)官網(wǎng)
二、SpringBoot集成Swagger
2.1 新建一個(gè)SpringBoot=web項(xiàng)目
2.2 導(dǎo)入相關(guān)的依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency>2.3創(chuàng)建config>SwaggerConfig
//定義配置類 @Configuration @EnableSwagger2//開(kāi)啟swagger2 public class SwaggerConfig {}2.4配置Swagger
//配置Swagger的Docket的bean實(shí)例@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());}//配置swagger信息public ApiInfo apiInfo(){Contact contact = new Contact("琴江","http://www.zhaoliangliang.work/","1003989507@qq.com");return new ApiInfo("亮哥的SwaggerAPI文檔","即使再小的帆也能遠(yuǎn)航","v2.2","http://www.zhaoliangliang.work/",contact,"Apache 2.0","http://www.zhaoliangliang.work/",new ArrayList());}2.5測(cè)試瞅瞅
三、Swagger配置掃描接口
//配置Swagger的Docket的bean實(shí)例@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()//RequestHandlerSelectors,配置要掃描接口的方式//basePackage:指定要掃描的包//any():掃描全部//none():不掃描//withClassAnnotation:掃描類上的注解,參數(shù)是一個(gè)注解的反射對(duì)象//withMethodAnotation:掃描方法上的注解.apis(RequestHandlerSelectors.basePackage("com.teaiyang.swagger.controller"))//path() 過(guò)濾什么路徑.paths(PathSelectors.ant("/swagger/**")).build();}配置是否啟動(dòng)Swagger
//配置Swagger的Docket的bean實(shí)例@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("亮亮組").enable(false)//enable是否啟動(dòng)swagger,如果false 則swagger不能再瀏覽器中訪問(wèn).select().apis(RequestHandlerSelectors.basePackage("com.teaiyang.swagger.controller"))//.paths(PathSelectors.ant("/swagger/**")).build();}四、問(wèn)題:我只希望我的Swagger在生成環(huán)境中使用,在發(fā)布環(huán)境不使用?
(一)在application.yml中
springconfig:enable: true(二)swagger配置類中
@Value("${springconfig.enable}")private boolean enable;五、用戶實(shí)體
@ApiModel( ) //主要用來(lái)標(biāo)注返回的實(shí)體類
@ApiModelProperty( ) //主要用來(lái)標(biāo)注實(shí)體類中的屬性
@ApiModelProperty用來(lái)標(biāo)注API接口
@RestController public class UserController {@ApiModelProperty("得到一個(gè)user")@GetMapping("/getUser")public User getUser(){return new User(1,"趙亮亮","papap",12);} }測(cè)試:
總結(jié)
以上是生活随笔為你收集整理的Swagger体验版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 经典Windows 扫雷小游戏源码 展示
- 下一篇: PPT放映的小问题