How to set @ApiModelProperty dataType to String for Swagger documentation

后端 未结 3 729
[愿得一人]
[愿得一人] 2021-01-17 09:40

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

相关标签:
3条回答
  • 2021-01-17 09:57
    @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;
    
    }
    
    0 讨论(0)
  • 2021-01-17 10:05

    For OpenApi (Swagger 3.0) and SpringDoc the following global configuration could be used.

    static {
         SpringDocUtils.getConfig().replaceWithSchema(Money.class, new StringSchema());
    }
    
    0 讨论(0)
  • 2021-01-17 10:14

    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

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

    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:

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .directModelSubstitute(Money.class, String.class);
    }
    
    0 讨论(0)
提交回复
热议问题