Spring boot整合Springfox生成restful的在線api文檔
Springfox是什么,有什么用?
Springfox基于Swagger,能更方便的集成到spring boot 中,Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。Swagger的目標(biāo)是對REST API定義一個(gè)標(biāo)準(zhǔn)的和語言無關(guān)的接口,可讓人和計(jì)算機(jī)無需訪問源碼、文檔或網(wǎng)絡(luò)流量監(jiān)測就可以發(fā)現(xiàn)和理解服務(wù)的能力。當(dāng)通過Swagger進(jìn)行正確定義,用戶可以理解遠(yuǎn)程服務(wù)并使用最少實(shí)現(xiàn)邏輯與遠(yuǎn)程服務(wù)進(jìn)行交互。與為底層編程所實(shí)現(xiàn)的接口類似,Swagger消除了調(diào)用服務(wù)時(shí)可能會(huì)有的猜測。
Springfox官方文檔:http://springfox.github.io/springfox/docs/snapshot/
Springfox的依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
Springfox的配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("demo")
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
//.pathMapping("/") //根路徑
.select()
.paths(PathSelectors.regex("/user/.*"))//篩選展示的接口,使用PathSelectors.any(),展示所有接口
.build()
.apiInfo(demoApiInfo())
;
}
//api信息
private ApiInfo demoApiInfo() {
ApiInfo apiInfo = new ApiInfo("自己平臺(tái)的api",//大標(biāo)題
"swagger搭建api平臺(tái)",//小標(biāo)題
"1.0",//版本
"NO terms of service",
"632104866@qq.com",//作者
"這是我的技術(shù)博客站點(diǎn)",//鏈接顯示文字
"http://www.kailing.pub"http://網(wǎng)站鏈接
);
return apiInfo;
}
}測試的Controll
@RestController
@RequestMapping("/user")
public class SwaggerDemoController {
@RequestMapping(value = "/demo2",method = RequestMethod.POST)
@ApiOperation(value="測試接口1", notes="測試接口詳細(xì)描述")
@ResponseBody
ModelMap getDemo(@RequestBody User user) {
ModelMap map = new ModelMap();
map.addAttribute("userId", 111);
map.addAttribute("userName", "kl博客");
return map;
}
@ResponseBody
@RequestMapping(value = "/demo3", method = RequestMethod.POST)
@ApiOperation(value="測試接口2", notes="測試接口詳細(xì)描述",code = 200,produces = "application/json")
public ModelMap getDemoa(@RequestParam("name") String demoName, @RequestParam String content) {
ModelMap map = new ModelMap();
map.addAttribute("result",demoName + "AAA");
return map;
}
@ResponseBody
@ApiIgnore //使用這個(gè)注解忽略這個(gè)接口
@RequestMapping(value = "/demo4", method = RequestMethod.POST)
public ModelMap getDemob(@RequestParam String content) {
ModelMap map = new ModelMap();
map.addAttribute("result",new java.util.Date());
return map;
}
}在瀏覽器輸入http://localhost:8080/swagger-ui.html,可查看是否整合成功
至此我們的額api在線文檔整合完成了,下面是效果圖

以上就是Spring boot整合Springfox生成restful的在線api文檔的詳細(xì)內(nèi)容,更多關(guān)于Spring boot整合Springfox生成restful在線api的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot無法解析parameter參數(shù)問題的解決方法
使用最新版的 Springboot 3.2.1(我使用3.2.0)搭建開發(fā)環(huán)境進(jìn)行開發(fā),調(diào)用接口時(shí)出現(xiàn)奇怪的錯(cuò),本文小編給大家介紹了SpringBoot無法解析parameter參數(shù)問題的原因及解決方法,需要的朋友可以參考下2024-04-04
Spring中的EurekaServer啟動(dòng)詳解
這篇文章主要介紹了Spring中的EurekaServer啟動(dòng)詳解,初始化eureka,包含eureka集群的同步和發(fā)布注冊,這個(gè)方法時(shí)重寫ServletContextListener#contextInitialized,是eureka啟動(dòng)的入口了,需要的朋友可以參考下2023-11-11
SpringBoot實(shí)現(xiàn)國際化i18n詳解
國際化(Internationalization,簡稱i18n)是指在軟件應(yīng)用中支持多種語言和文化的能力,本文將介紹如何在Spring?Boot應(yīng)用中實(shí)現(xiàn)國際化,需要的可以參考下2024-12-12
SpringBoot@DeleteMapping(/xxx/{id})請求報(bào)405的解決
這篇文章主要介紹了SpringBoot@DeleteMapping(/xxx/{id})請求報(bào)405的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

