java 如何将 @ApiModelProperty dataType 设置为 Swagger 文档的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29355872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:06:11  来源:igfitidea点击:

How to set @ApiModelProperty dataType to String for Swagger documentation

javaspringspring-mvcswagger

提问by nerdherd

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

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

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;
    }
    ...
}

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

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

{
    "listPrice": "USD 10.50"
}

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

但是,swagger 文档并不尊重 dataType = "java.lang.String"。它将响应模型显示为:

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

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

我曾尝试将 @ApiModelProperty 注释放在字段和方法上,在这两种情况下,该required字段都受到尊重,但该dataType字段被忽略。我也试过对 dataType 使用“String”、“string”和“java.lang.String”,但这些都没有奏效。

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

我是否遗漏了什么,或者这只是 swagger-spring-mvc 库中的一个错误?

采纳答案by nerdherd

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

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

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

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

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

看起来它可以在发布后包含在版本 2 中。

EDIT: 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:

编辑:虽然版本 2 说它支持 dataType,但它此时似乎不起作用。满足我的需求的更好方法是使用像这样的直接模型替换来配置文档设置:

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

回答by Yolo

@ApiModel
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Model {
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonProperty("myDate")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime myDateTime;

}