今天就跟大家聊聊有關IDEA 中的SpringBoot 項目使用Swagger2的方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1、SpringBoot-web項目并添加pom.xml依賴
2、編寫HelloController,測試成功運行
3、創建一個SwaggerConfig類,配置swagger-ui
流程確實是很簡單的,但是能真正的在項目中活用swagger卻不是那么簡單
1、SpringBoot-web項目并添加pom.xml依賴
可以直接在maven repository搜索
多最一句,一般在選擇maven依賴時,我們趨向于選擇最穩定版本,可以通過Usages判斷,盡量不要選擇最新版,及時并沒有什么影響。詳細經歷過版本沖突和版本不兼容的小伙伴應該深有體會。

<!-- 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、編寫HelloController,測試成功運行
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 = "測試-HelloWorld") //這個controller下的所有接口的描述
public class HelloController {
@ApiOperation("測試Swagger2") //顯示在接口的信息說明
@RequestMapping(value = "/test", method = RequestMethod.GET) //請求路徑和類型
public void testSwagger(){
}
}3、創建一個SwaggerConfig類,配置swagger-ui
package cn.swpu.myblog.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}然后就可以根據你的端口訪問swagger-ui了,例如我的是8099
http://localhost:8099/swagger-ui.html#/


至此IDEA 繼承Swagger就可以了,但這僅僅只是個開始,swagger還是有很多的實用技巧。
看完上述內容,你們對IDEA 中的SpringBoot 項目使用Swagger2的方法有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。