問題描述
近期了解了swagger,通過swagger進行可視化的編輯和定義UI。
Swagger可以幫助開發者簡單的管理REST風格的API,並且支持多種框架下的管理
本文主要介紹通過SpringMVC進行集成Swagger的經歷
采用的是SpringFox的開源實現,組件可以自動掃描spring的controller, 並且自動生成查詢接口目錄
如果引入tomcat的話,可以自動開放http://[ip]:[port]/[servicename]/[api-docs]的rest接口
通過swagger-ui可以生成漂亮的界面
中文介紹參見:酸嘢的博客
參考博客: 劉新宇的博客
集成參考
集成中遇到的坑
- jar包引入,通過maven管理的話:pom文件應該增加
<!--springfox依賴-->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<!--jackson依賴-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
<!--靜態頁面依賴的webjar-->
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.1.8-M1</version>
</dependency>
- 配置方式沖突問題,錯誤日志顯示的模棱兩可,問題描述如下
No qualifying bean of type [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] found for dependency
經過網上調查結果, 發現是配置沖突的問題
上述文章中配置方式都是全部注解的,也許沒有什么大問題
但是世界項目中是注解和xml文件配置都有,所以可能產生沖突
查看了下面的兩個問題回復
[stringfox issue 462],[stringfox issue 160]
I was having the same problem and am using the XML configuration.
I removed the @Configuration attribute from my SwaggerConfig and it worked fine.
最終看到了,按照SpringMVC項目接入Springfox進行集成的話
如果出現了上述問題,將@Configuration
配置干掉,而是采用bean的方式進行配置
問題集合
[stringfox issue 462]: https://github.com/springfox/springfox/issues/462
[stringfox issue 160]: https://github.com/springfox/springfox/issues/160
[競態條件深入分析]: http://forum.spring.io/forum/spring-projects/web/112154-unable-to-autowire-requestmappinghandlermapping-in-controller