Spring cloud 引入Feign后,Model Schema为空,Exception calculating properties for model

项目使用Spring cloud 集成了swagger,原来一切正常,在引入了Feign后,发现swagger的Model Schema为空,虽然不影响开发,但测试起来还是很麻烦的,查看启动日志,发现springfox.documentation.schema.property.CachingModelPropertiesProvider Line:66 - Exception calculating properties for model(com.amygo.common.BaseResult) -> ModelContext{type=com.amygo.common.BaseResult, isReturnType=true}. java.lang.NullPointerException

github上已有相关issue:
CachingModelPropertiesProvider - NullPointerException
github的地址

What i found was, the NPE is coming for OptimizedModelPropertiesProvider because the objectMapper is null.
When a Feign client is added to the application, spring-cloud creates a child application context for feign beans. Check spring cloud’s NamedContextFactory for details on how they create the context. Once this context is refreshed, it signals a ContextRefreshedEvent which is picked up by DocumentationPluginsBootstrapper. The problem is the main application context initialization is still not complete. RequestMappingHandlerAdapter is not even created at this time and so ObjectMapperConfigurer does not fire ObjectMapperConfigured. This is the reason why OptimizedModelPropertiesProvider has a null object mapper.
When RequestMappingHandlerAdapter is created at a later time by the main application context, all the events are fired, but its too late. DocumentationPluginsBootstrapper has already finished its model evaluation.
A possible fix is to wait for the main application’s ContextRefreshedEvent, and skip the ContextRefreshedEvent published by spring-cloud. But i am not sure how to do it.
看了之后,直接从代码层面还是很困难的,想到自己使用的swagger的版本是2.4.0,猜想可能是版本问题,所以把版本改成2.5.0,代码如下:

io.springfox
springfox-swagger2
2.5.0


io.springfox
springfox-swagger-ui
2.5.0

重新启动,一切正常!

你可能感兴趣的:(Spring,cloud)