2.1.6 引入swagger-ui
pom.xml
io.springfox
springfox-swagger-ui
2.8.0
2.1.7 啟動項目訪問basePath/swagger-ui.html即可
在這一步,我本地出現以下問題,嘗試了幾種解決方式,但是暫時未解決,以後解決了會補上解決方式
在此暫時通過其他服務器部署好的swagger-ui來訪問當前項目生成的數據
通過點擊圖上Try it out可以訪問並測試接口功能,至此就完成了swagger的嵌入
2.2 第二種方式再介紹一下第二種方式
2.2.1 同理構建一個spring-web項目
2.2.2 pom引入
pom.xml
com.mangofactory
swagger-springmvc
1.0.2
com.mangofactory
swagger-models
1.0.2
com.wordnik
swagger-annotations
1.3.11
com.google.guava
guava
15.0
com.fasterxml.jackson.core
jackson-annotations
2.4.4
com.fasterxml.jackson.core
jackson-databind
2.4.4
com.fasterxml.jackson.core
jackson-core
2.4.4
com.fasterxml
classmate
1.1.0 2.2.3 SwaggerConfig 配置swagger
SwaggerConfigpackage com.swagger.config;
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableSwagger
@ComponentScan("com.swagger.controller") //啟用組件掃描
public class SwaggerConfig {
private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.includePatterns(".*?");
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"Swagger測試接口",
"這是一個Swagger生成API測試",
"My Apps API terms of service",
"My Apps API Licence Type",
"My Apps API License URL");
return apiInfo;
}
}
2.2.4 其他Controller和Entity同理,構建完成啟動即可
有些還不是很明白的是這種方式生成的地址是basUrl/api-docs,應該是對這個還不是很熟悉,之後如果有機會深入會繼續記錄分享給大家
將該接口放至開始服務器無法讀取接口信息
所以嘗試使用原始方式搭建一個swagger-ui
2.2.5 spring.xml
2.2.6 搭建swagger-ui
下載 swagger-ui
https://github.com/swagger-api/swagger-ui/tree/v2.2.10
解壓下載後的文件,將裡面dist文件夾下內容放置項目中
2.2.7 修改index.html內容
2.2.8 訪問swagger-ui
訪問項目下swagger中index.html,發現也可以達到效果
3.總結
總結下這次初步學習swagger,確實踩了很多坑,有些問題從網上找到的解決方法在自己這裡也不管用。總結一下其實如果項目多,單獨搭建一個提供查看接口文檔的swagger-ui項目,其他項目主要負責生成對應數據格式文件,還是很方便的。有些小夥伴項目嵌入swagger可能會遇到一些小問題,比如生成的文檔格式數據、或者原始方式引用swagger-ui被過濾器攔截導致接口生成數據無法讀取成功等等。總結一下學習技術還是要靜下心來,心越亂離成功越遠。下面列一些學習過程中看到的不錯的博客推薦給大家,分享給大家一起學習,這應該也是博主寫博客的其中一個目的吧。
相關博客推薦:
[Swagger使用](https://blog.csdn.net/blucelee2/article/details/51140587)
[swagger的使用](https://blog.csdn.net/gyb_csdn/article/details/75123575)
[swagger編寫規範](https://blog.csdn.net/xxoo00xx00/article/details/77278510)
[Swagger框架學習分享](https://blog.csdn.net/u010827436/article/details/44417637)
[swagger2常用註解說明](https://blog.csdn.net/u014231523/article/details/76522486)
[使用springfox+swagger2書寫API文檔](https://blog.csdn.net/u012476983/article/details/54090423)
[ SpringMVC+Swagger UI生成可視圖的API文檔(詳細圖解)](https://blog.csdn.net/u011499992/article/details/53455144)
[Restful形式接口文檔生成之Swagger與SpringMVC整合手記](https://blog.csdn.net/linlzk/article/details/50728264)
[SSM三大框架整合Springfox(Swagger2)步驟以及遇到的一些問題](https://blog.csdn.net/twomr/article/details/77101092)
閱讀更多 丶灣裡小白龍 的文章