如何将Swagger文档的@ApiModelProperty数据类型设置为String [英] How to set @ApiModelProperty dataType to String for Swagger documentation

查看:1193
本文介绍了如何将Swagger文档的@ApiModelProperty数据类型设置为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring MVC(通过Spring Boot),并使用swagger-spring-mvc库集成了Swagger API文档.

I am using Spring MVC (via Spring Boot) and have integrated Swagger API documentation using the swagger-spring-mvc library.

我有一堂课,看起来像这样:

I have a class that looks something like this:

@ApiModel
public class CartItem {
    ...
    private Money listPrice; // joda money class

    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(required = true, dataType = "java.lang.String")
    public Money getListPrice() {
        return listPrice;
    }
    ...
}

由于我为此字段使用ToStringSerializer,因此它将在JSON中返回listPrice.toString,换句话说:

Since I'm using the ToStringSerializer for this field, it's returning listPrice.toString in the JSON, in other words:

{
    "listPrice": "USD 10.50"
}

但是,swagger文档并不支持dataType ="java.lang.String".响应模型显示为:

However, the swagger documentation is not honoring the dataType = "java.lang.String". It shows the response model as:

"CartItem": {
    "description": "",
    "id": "CartItem",
    "properties": {
        "listPrice": {
            "required": false,
            "type": "Money"
        }
    }
}

我尝试将@ApiModelProperty批注以及该方法添加到该字段上,在两种情况下,都尊重required字段,但忽略dataType字段.我也曾尝试对数据类型使用字符串",字符串"和"java.lang.String",但这些都不起作用.

I have tried putting the @ApiModelProperty annotation on the field as well as the method, and in both cases the required field is respected, but the dataType field is ignored. I have also tried using "String", "string", and "java.lang.String" for the dataType but none of those have worked.

我错过了什么吗,还是仅仅是swagger-spring-mvc库中的错误?

Am I missing something, or is this just a bug in the swagger-spring-mvc library?

推荐答案

事实证明,在当前版本的Swagger Spring MVC库中,完全忽略了dataType.我在这里找到了简短的讨论:

Turns out that dataType is completely ignored in the current version of the Swagger Spring MVC library. I found a short discussion on it here:

https://github.com/springfox/springfox/issues/602

看起来一旦发布,它就可以包含在版本2中.

Looks like it could be included in version 2 once that is out.

尽管版本2表示它支持dataType,但目前似乎无法正常工作.我需要的一种更好的方法是使用直接模型替换来配置文档设置,如下所示:

Although version 2 says it supports dataType, it doesn't appear to be working at this time. A better approach for my needs is to configure the documentation settings with a direct model substitution like this:

@Bean
public Docket swaggerSpringMvcPlugin() {
    return new Docket(DocumentationType.SWAGGER_2)
            .directModelSubstitute(Money.class, String.class);
}

这篇关于如何将Swagger文档的@ApiModelProperty数据类型设置为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆