解決swagger2中@ApiResponse的response不起作用
swagger可以生成比較友好的在線API說明文檔
友好的API說明重要性不言而喻,因?yàn)樗^API,肯定就是被用來調(diào)用的,其中涉及到不同群體的工作,比如前端后端,本公司與第三方公司,等等。以往,制訂數(shù)據(jù)接口,要正正經(jīng)經(jīng)地寫一份正式的文檔,名曰集成規(guī)范,大家對(duì)照著來。但現(xiàn)在有了swagger框架,就方便許多了,直接利用代碼生成在線的接口說明文檔。
swagger要產(chǎn)生比較實(shí)用的API說明文檔,需要加一些標(biāo)注。但是,這兩天在實(shí)際應(yīng)用過程中,卻遇到一個(gè)問題,即無法生成響應(yīng)數(shù)據(jù)的實(shí)體類說明。說明部分空空如也。
這樣子的話,那么這個(gè)API說明文檔意義就不大了。因?yàn)榉祷氐臄?shù)據(jù)中,有許多字段需要加上中文注釋,否則根本不知道什么意思。
我們用的是swagger2
pom.xml
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.5</version> </dependency>
API所在控制器
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api/work/production/total") @Api(tags="產(chǎn)量產(chǎn)值") public class WorkProductionChangeController { @Resource private WorkProductionChangeService workProductionChangeService; @PostMapping(path = "/all") @ApiOperation(value = "獲取所有年份的總產(chǎn)量產(chǎn)值") @ApiResponses(value = { @ApiResponse(code = 200, message = "返回所有年份的總產(chǎn)量產(chǎn)值",response = WorkProductionChange.class) }) public JSONObject getAll() { return 。。。 } }
實(shí)體類WorkProductionChange
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel public class WorkProductionChange implements Serializable { private static final long serialVersionUID = -64757122210615988L; private Long id; private Integer year; @ApiModelProperty(value = "總產(chǎn)量(噸)") private Double totalWeight; @ApiModelProperty(value = "總產(chǎn)值(萬元)") private Double totalMoney; 。。。 }
按道理,response = WorkProductionChange.class,那么實(shí)體WorkProductionChange的信息應(yīng)該出現(xiàn)在說明文檔上,但從效果看,并沒有。
狂搜索。后來終于看到有鬼佬說了這么一句:
Springfox 3.0 uses v3 models by default, but source.getResponses() gives wrong type. To workaround it for now, add:
springfox.documentation.swagger.use-model-v3=false in your application.properties.
英文爛,勉強(qiáng)看意思就是說,Springfox3.0默認(rèn)用swagger v3來返回信息,但有個(gè)地方又出毛病了。為了避免愚蠢的系統(tǒng)犯錯(cuò),你要在配置文件application.properties里加上一句:
application.properties
springfox.documentation.swagger.use-model-v3=false
如果是yml,就是
springfox: documentation: swagger: use-model-v3: false
太陽出來了。這才是我想要的。
參考文章:
https://github.com/springfox/springfox/issues/3503
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
手把手教你怎么創(chuàng)建spring項(xiàng)目
今天教大家怎么寫spring項(xiàng)目,文中有非常詳細(xì)的圖文示例及介紹,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06關(guān)于Springboot2.x集成lettuce連接redis集群報(bào)超時(shí)異常Command timed out afte
這篇文章主要介紹了Springboot2.x集成lettuce連接redis集群報(bào)超時(shí)異常Command timed out after 6 second(s),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-03-03Java使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏
我們在使用手機(jī)銀行的時(shí)候經(jīng)常能看到APP上會(huì)將銀行卡的卡號(hào)中間部分給隱藏掉使用 ***** 來代替,在某些網(wǎng)站上查看一些業(yè)務(wù)密碼時(shí)(例如簽到密碼等)也會(huì)使用 ***** 來隱藏掉真正的密碼,那么這種方式是如何實(shí)現(xiàn)的呢,本文將給大家介紹使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏2023-09-09解決spring.thymeleaf.cache=false不起作用的問題
這篇文章主要介紹了解決spring.thymeleaf.cache=false不起作用的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Java兩個(gè)變量的互換(不借助第3個(gè)變量)具體實(shí)現(xiàn)方法
這篇文章主要介紹了Java兩個(gè)變量的互換(不借助第3個(gè)變量)具體實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02Java中的clone方法詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
clone顧名思義就是復(fù)制, 在Java語言中, clone方法被對(duì)象調(diào)用,所以會(huì)復(fù)制對(duì)象。下面通過本文給大家介紹java中的clone方法,感興趣的朋友一起看看吧2017-06-06基于apache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄赼pache poi根據(jù)模板導(dǎo)出excel的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06