Spring5.0整合Swagger后在本地跑正常,但更新服务器后报注解异常问题:
{resin-198}<ERROR>[20221223 165754.892]org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:534) Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'swagger2ControllerMapping' defined in class path resource [springfox/documentation/swagger2/configuration/Swagger2DocumentationWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'swagger2ControllerMapping' parameter 2; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
最后发现是spring5.0的新特性META-INF/spring.components问题。
Spring打包时会生成 META-INF/spring.components ,为注解加索引,但只加了当前项目的,Swagger里注解加上,导致上服务器后,注解只扫描当前项目的,Swagger的注解没扫描到。
解决方法:
在当前项目加上spring.properties设定:
# 禁用索引功能.
spring.index.ignore=true