2019-04-16|SwaggerUI界面model schema不生成问题

今天同事遇到一个问题,swagger-ui界面每个接口的response的model schema 都没有生成,如下图所示:


image.png

用的swagger版本:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.4.0</version>
    </dependency>

本地跑了下,发现启动就有告警信息:

18:09:33.596 [main] WARN  Exception calculating properties for model(com.xxx.xxx.mobile.client.dto.resp.CarBrandResp) -> ModelContext{type=com.xxx.xxx.mobile.client.dto.resp.CarBrandResp, isReturnType=true}. java.lang.NullPointerException

Debug了下发现OptimizedModelPropertiesProviderobjectMapper为空导致的NPE.详细堆栈信息记录如下:

      at springfox.documentation.schema.property.OptimizedModelPropertiesProvider.beanDescription(OptimizedModelPropertiesProvider.java:350)
      at springfox.documentation.schema.property.OptimizedModelPropertiesProvider.propertiesFor(OptimizedModelPropertiesProvider.java:117)
      at springfox.documentation.schema.property.CachingModelPropertiesProvider$1.load(CachingModelPropertiesProvider.java:56)
      at springfox.documentation.schema.property.CachingModelPropertiesProvider$1.load(CachingModelPropertiesProvider.java:54)
      at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
      at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
      at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
      - locked <0x1a49> (a com.google.common.cache.LocalCache$StrongAccessWriteEntry)
      at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
      at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
      at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
      at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
      at springfox.documentation.schema.property.CachingModelPropertiesProvider.propertiesFor(CachingModelPropertiesProvider.java:64)
      at springfox.documentation.schema.DefaultModelProvider.properties(DefaultModelProvider.java:151)
      at springfox.documentation.schema.DefaultModelProvider.modelFor(DefaultModelProvider.java:84)
      at springfox.documentation.schema.CachingModelProvider$1.load(CachingModelProvider.java:51)
      at springfox.documentation.schema.CachingModelProvider$1.load(CachingModelProvider.java:49)
      at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527)
      at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319)
      at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282)
      - locked <0x1a74> (a com.google.common.cache.LocalCache$StrongAccessWriteEntry)
      at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197)
      at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
      at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
      at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)
      at springfox.documentation.schema.CachingModelProvider.modelFor(CachingModelProvider.java:59)
      at springfox.documentation.spring.web.scanners.ApiModelReader.read(ApiModelReader.java:67)
      at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:88)
      at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:69)
      at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:105)
      at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.onApplicationEvent(DocumentationPluginsBootstrapper.java:91)
      at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.onApplicationEvent(DocumentationPluginsBootstrapper.java:53)
      at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
      at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
      at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
      at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
      at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:399)
      at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
      at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
      - locked <0x1a75> (a java.lang.Object)
      at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:116)
      at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:85)
      - locked <0x1a76> (a java.util.concurrent.ConcurrentHashMap)
      at org.springframework.cloud.context.named.NamedContextFactory.getInstance(NamedContextFactory.java:121)
      at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.get(FeignClientFactoryBean.java:193)
      at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.feign(FeignClientFactoryBean.java:84)
      at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:221)
      at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
      at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
      - locked <0x1a77> (a java.util.concurrent.ConcurrentHashMap)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1316)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1282)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
      - locked <0x1a78> (a java.lang.Object)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
      at com.xxx.xxx.mobile.MobileApplication.main(MobileApplication.java:21)

github上已有相关issue:
CachingModelPropertiesProvider - NullPointerException
【praveen12bnitt】解释了原因:

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.

issue中解释的还是很清楚的,结合堆栈信息:
当引入Feign客户端时,SpringCloud创建了一个子应用程序上下文FeignContext;创建过程中会调用refresh方法,这个方法里会派发ContextRefreshedEvent事件;而DocumentationPluginsBootstrapper会监听这个事件,然后开始扫描API构建Documentation;这里的NPE正是在构建Model的过程中报错的;
NPE是因为:此时主应用程序上下文初始化仍未完成,主应用程序上下文在配置类WebMvcAutoConfiguration中会创建RequestMappingHandlerAdapter实例;
配置类SpringfoxWebMvcConfiguration中会创建ObjectMapperConfigurer实例,这个类实现了BeanPostProcessor接口,会在任何bean初始化之前调用postProcessBeforeInitialization方法,这个方法内部判断了如果bean是RequestMappingHandlerAdapter,那么会派发一个ObjectMapperConfigured事件;
OptimizedModelPropertiesProvider监听了这个事件,从而设置objectMapper;

如果我们没有使用FeignClient,那么一切还是很完美的;但是引入FeignClient之后,DocumentationPluginsBootstrapper过早的监听到了springcloud派发的事件;实际上应该监听的是主应用程序上下文派发的事件。

issue中给出了解决方法:让DocumentationPluginsBootstrapper实现SmartLifecycle接口,这个是在所有bean加载并初始化完成之后才执行的,其实就是让DocumentationPluginsBootstrapper延迟执行构建Documentation的功能。

这个在后面的版本2.5.0中得到了解决,遂升级了版本...

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,047评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,807评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,501评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,839评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,951评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,117评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,188评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,929评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,372评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,679评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,837评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,536评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,168评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,886评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,129评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,665评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,739评论 2 351

推荐阅读更多精彩内容