IDEA SpringBoot 項(xiàng)目配置Swagger2的詳細(xì)教程
在項(xiàng)目中也經(jīng)常用到swagger2,于是動(dòng)手記錄一下swagger2配置過(guò)程,希望能帶來(lái)一點(diǎn)幫助。
在SpringBoot項(xiàng)目當(dāng)中使用Swagger主要分為以下幾步:
1、SpringBoot-web項(xiàng)目并添加pom.xml依賴(lài)
2、編寫(xiě)HelloController,測(cè)試成功運(yùn)行
3、創(chuàng)建一個(gè)SwaggerConfig類(lèi),配置swagger-ui
流程確實(shí)是很簡(jiǎn)單的,但是能真正的在項(xiàng)目中活用swagger卻不是那么簡(jiǎn)單
1、SpringBoot-web項(xiàng)目并添加pom.xml依賴(lài)
可以直接在maven repository搜索
多最一句,一般在選擇maven依賴(lài)時(shí),我們趨向于選擇最穩(wěn)定版本,可以通過(guò)Usages判斷,盡量不要選擇最新版,及時(shí)并沒(méi)有什么影響。詳細(xì)經(jīng)歷過(guò)版本沖突和版本不兼容的小伙伴應(yīng)該深有體會(huì)。

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version></dependency><!-- 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>
2、編寫(xiě)HelloController,測(cè)試成功運(yùn)行
package cn.swpu.myblog.controller;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@Api(tags = '測(cè)試-HelloWorld') //這個(gè)controller下的所有接口的描述public class HelloController { @ApiOperation('測(cè)試Swagger2') //顯示在接口的信息說(shuō)明 @RequestMapping(value = '/test', method = RequestMethod.GET) //請(qǐng)求路徑和類(lèi)型 public void testSwagger(){ }}
3、創(chuàng)建一個(gè)SwaggerConfig類(lèi),配置swagger-ui
package cn.swpu.myblog.config;import org.springframework.context.annotation.Configuration;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class SwaggerConfig {}
然后就可以根據(jù)你的端口訪(fǎng)問(wèn)swagger-ui了,例如我的是8099
http://localhost:8099/swagger-ui.html#/


至此IDEA 繼承Swagger就可以了,但這僅僅只是個(gè)開(kāi)始,swagger還是有很多的實(shí)用技巧。
到此這篇關(guān)于IDEA SpringBoot 項(xiàng)目配置Swagger2的詳細(xì)教程的文章就介紹到這了,更多相關(guān)IDEA SpringBoot 項(xiàng)目配置Swagger2內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:

網(wǎng)公網(wǎng)安備