【已完成】bex接口无法在swagger生效(bug定位及优化建议)

bex 无法在swagger生效

环境版本:

<koca.base.version>3.2.0</koca.base.version>

<kace.base.version>3.2.0</kace.base.version>

![|554x203](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps1.jpg)

详细描述:在-Bex.xml写的接口,无法在swagger正常显示。
image

Debug模式下详细信息:

image

java.lang.NullPointerException: null

at java.util.TreeMap.compare(TreeMap.java:1294)

at java.util.TreeMap.put(TreeMap.java:538)

at java.util.TreeSet.add(TreeSet.java:255)

at java.util.AbstractCollection.addAll(AbstractCollection.java:344)

at java.util.TreeSet.addAll(TreeSet.java:312)

at springfox.documentation.builders.OperationBuilder.tags(OperationBuilder.java:308)

at com.szkingdom.koca.bex.swagger.config.BexSwaggerScanner.apply(BexSwaggerScanner.java:133)

at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.additionalListings(DocumentationPluginsManager.java:199)

at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:127)

at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:67)

at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.scanDocumentation(AbstractDocumentationPluginsBootstrapper.java:96)

at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100)

at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)

at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)

at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)

at java.lang.Iterable.forEach(Iterable.java:75)

at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)

at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:445)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)

at com.szkingdom.oms.OmsApplication.main(OmsApplication.java:39)

原因:实际报错位置是L 133行,tags为空,把TreeSet添加null报错。

image

image

来分析bex.xml的 写法如下:

<java-bex method="manageCalendar" type="com.szkingdom.koca.ace.dac.right.system.service.impl.CalendarServiceImpl" name="管理[系统-交易日历]" id="100340" api="100340">

  <request>

    <param type="com.szkingdom.koca.ace.dac.right.core.bean.BexParam">

    <param code="param" type="map">

         <param code="action" name="操作 A-清算 D-非清算" required="true" type="string" length="1"/>

         <param code="markets" name="日历类型" required="true" type="string" length="128"/>

         <param code="phydate" name="日期" required="true" type="string"/> </param>
    </param>


 </request>

</java-bex>

image

CalendarServiceImpl 的写法没问题

image

分析:发现他是根据spring的类型查找,没有写bean的属性,导致apiBexMap中的Bex对象中没有bean属性。

解决方案是:

  • 在Line 93行,对tags进行增强,如果tags为空,截取bean的实际类型赋值给tags,还是为空的时候把他归到“其他”,也就是tags = “orther”;

  • 在Line:124- 133行部分,用try-catch进行处理,这部分由于个别bex接口编写的不规范,但不应该导致整个bex 接口文档的输出,正常接口还得正常输出。

这些都需要koca基础包修复此bug

bex 无法在swagger生效

环境版本:

<koca.base.version>3.2.0</koca.base.version>

<kace.base.version>3.2.0</kace.base.version>

![|554x203](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps1.jpg)

详细描述:在-Bex.xml写的接口,无法在swagger正常显示。

Debug模式下详细信息:

![|553x176](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps2.jpg)

java.lang.NullPointerException: null

at java.util.TreeMap.compare(TreeMap.java:1294)

at java.util.TreeMap.put(TreeMap.java:538)

at java.util.TreeSet.add(TreeSet.java:255)

at java.util.AbstractCollection.addAll(AbstractCollection.java:344)

at java.util.TreeSet.addAll(TreeSet.java:312)

at springfox.documentation.builders.OperationBuilder.tags(OperationBuilder.java:308)

at com.szkingdom.koca.bex.swagger.config.BexSwaggerScanner.apply(BexSwaggerScanner.java:133)

at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.additionalListings(DocumentationPluginsManager.java:199)

at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:127)

at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:67)

at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.scanDocumentation(AbstractDocumentationPluginsBootstrapper.java:96)

at springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper.bootstrapDocumentationPlugins(AbstractDocumentationPluginsBootstrapper.java:82)

at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:100)

at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)

at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)

at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)

at java.lang.Iterable.forEach(Iterable.java:75)

at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)

at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)

at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:445)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)

at com.szkingdom.oms.OmsApplication.main(OmsApplication.java:39)

原因:实际报错位置是L 133行,tags为空,把TreeSet添加null报错。

![|553x260](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps3.jpg)

![|554x198](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps4.jpg)

来分析bex.xml的 写法如下:

<java-bex method=“manageCalendar” type=“com.szkingdom.koca.ace.dac.right.system.service.impl.CalendarServiceImpl”

name=“管理[系统-交易日历]”

id=“100340” api=“100340”>

![|554x208](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps5.jpg)

CalendarServiceImpl 的写法没问题

![|554x215](file:///C:\Users\user\AppData\Local\Temp\ksohtml924\wps6.jpg)

分析:发现他是根据spring的类型查找,没有写bean的属性,导致apiBexMap中的Bex对象中没有bean属性。

解决方案是:

在Line 93行,对tags进行增强,如果tags为空,截取bean的实际类型赋值给tags,还是为空的时候把他归到“其他”,也就是tags = “orther”;

在Line:124- 133行部分,用try-catch进行处理,这部分由于个别bex接口编写的不规范,但不应该导致整个bex 接口文档的输出,正常接口还得正常输出。

bean和type非必填项,但两属性必须至少有一项
填写type,swagger无相关信息
填写bean,swagger有相关信息
建议按照以下这五类填写
id 编号
name 名称
api 访问url
bean bean名称
method 对应方法

实际上在koca 3.2.0, 整合 koca-bex-swagger的时候会出现此问题,后面版本,由于网络的原因,下载不了最新版本,没有做尝试。帖子中的bex的接口是该项目引入其他项目组的包,编写的bex.xml接口是没有写bean属性,TreeSet添加null,导致报错了,截断了,在调用DocumentationPluginsManager.additionalListings里面做了对 apply 方法的try-catch,所以很难看到报错的,最后导致整个工程的bex接口无法生成swagger接口。建议:可以对这个版本的 koca-bex-swagger 进行简单修复

实际上在3.2.0 版本bex 接口 在swagger 是无法生效,应该是bug,不知后续版本有没有修复。图中的bex是引入其他项目组的包,无法修改的