您的位置:首页 > 移动开发

SpringMVC 集成 Swagger【问题】 No qualifying bean of type RequestMappingHandlerMapping found for dependency

2015-11-10 14:23 1361 查看

问题描述

近期了解了swagger,通过swagger进行可视化的编辑和定义UI。

Swagger可以帮助开发者简单的管理REST风格的API,并且支持多种框架下的管理

本文主要介绍通过SpringMVC进行集成Swagger的经历

采用的是SpringFox的开源实现,组件可以自动扫描spring的controller, 并且自动生成查询接口目录

如果引入tomcat的话,可以自动开放http://[ip]:[port]/[servicename]/[api-docs]的rest接口

通过swagger-ui可以生成漂亮的界面

中文介绍参见:酸嘢的博客

参考博客: 刘新宇的博客

集成参考

SpringMVC项目接入Springfox

Swagger框架学习分享

集成中遇到的坑

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: