简体   繁体   English

如果添加带有 WebMvcConfigurationSupport 的转换器,Swagger OpenAPI 3 不会显示在 springboot 2 上

[英]Swagger OpenAPI 3 not display on springboot 2 if add Converter with a WebMvcConfigurationSupport

I have a springboot application with swagger file.我有一个带有 swagger 文件的 springboot 应用程序。 I use any swagger Maven plugin and it works.我使用任何 swagger Maven 插件并且它可以工作。

在此处输入图像描述

My pom.xml file:我的 pom.xml 文件:

    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-core</artifactId>
        <version>2.0.9</version>
    </dependency>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.0.9</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.5.23</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.1.45</version>
    </dependency>

I use an java enum :我使用 java enum

public enum TempReadingSource {
  COLDROOM("coldroom"),
    LOCAL("local"),
    OVEN("oven");

  private String value;

  TempReadingSource(String value) {
    this.value = value;
  }

  @Override
  @JsonValue
  public String toString() {
    return String.valueOf(value);
  }

  @JsonCreator
  public static TempReadingSource fromValue(String text) {
    for (TempReadingSource b : TempReadingSource.values()) {
      if (String.valueOf(b.value).equals(text)) {
        return b;
      }
    }
    return null;
  }
}

and I have this error:我有这个错误:

"Failed to convert value of type 'java.lang.String' to required type 'com.foo.TempReadingSource';

So I add a converter in my Springboot2 configuration:所以我在我的 Springboot2 配置中添加了一个转换器:

@Configuration
public class StrubConfig extends WebMvcConfigurationSupport {

    @Override
    public FormattingConversionService mvcConversionService() {
        FormattingConversionService f = super.mvcConversionService();
        f.addConverter(new TempReadingSourceConverter());
        return f;
    }

}

and:和:

public class TempReadingSourceConverter implements Converter<String, TempReadingSource> {
    @Override
    public TempReadingSource convert(String source) {
       try {
          return TempReadingSource.fromValue(source);
       } catch(Exception e) {
          return null;
       }
    }
}

This Converter solve my API (via postman) but now swagger-ui is not found:这个Converter解决了我的 API(通过邮递员),但现在找不到swagger-ui

在此处输入图像描述

Solution is:解决方案是:

@Configuration
public class StrubConfig {

    @Autowired
    private FormattingConversionService conversionService;

    @PostConstruct
    public void registerCustomConverter() {
        conversionService.addConverter(new TempReadingSourceConverter());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenApi (Swagger) 枚举显示名称 - OpenApi (Swagger) Enum display name 如何使用Openapi Generator从Swagger yaml生成SpringBoot模型 - How to generate SpringBoot models from Swagger yaml with Openapi Generator 是否有任何选项可以为 /swagger-ui 路径添加前缀? 开放API - Is there any option to add a prefix for /swagger-ui path? OpenApi 从带有 Swagger/OpenAPI 注释的 Springboot REST 资源返回二进制 PDF 的正确方法是什么? - What is the right way to return a binary PDF from Springboot REST resources with Swagger/OpenAPI annotations? 如何在 OpenApi3 Springboot 应用程序中为 Swagger Url 自定义上下文路径 - How to have custom context Path for Swagger Url in OpenApi3 Springboot Application Swagger 3 / OpenApi 如何为同一个状态码添加两个描述 - Swagger 3 / OpenApi How to add two descriptions to the same status code 当我们获得bean时,SpringBoot WebMvcConfigurationSupport没有采用configureMessageConverters - SpringBoot WebMvcConfigurationSupport didn't take configureMessageConverters when we get bean in 存在 WebMvcConfigurationSupport 时不会生成 Swagger UI - Swagger UI does not get generated when WebMvcConfigurationSupport is present OpenAPI、Swagger 和 Lombok - OpenAPI, Swagger and Lombok OpenAPI (Swagger) 配置 Quarkus - OpenAPI (Swagger) Configuration Quarkus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM