亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決swagger2中@ApiResponse的response不起作用

 更新時(shí)間:2022年06月14日 09:34:26   作者:左直拳  
這篇文章主要介紹了解決swagger2中@ApiResponse的response不起作用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)文章

最新評(píng)論