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

重新启动,一切正常!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值