我有一個(gè)帶有 swagger 文件的 springboot 應(yīng)用程序。我使用任何 swagger Maven 插件并且它可以工作。我的 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>我使用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; }}我有這個(gè)錯(cuò)誤:"Failed to convert value of type 'java.lang.String' to required type 'com.foo.TempReadingSource';所以我在 Springboot2 配置中添加了一個(gè)轉(zhuǎn)換器:@Configurationpublic class StrubConfig extends WebMvcConfigurationSupport { @Override public FormattingConversionService mvcConversionService() { FormattingConversionService f = super.mvcConversionService(); f.addConverter(new TempReadingSourceConverter()); return f; }}和:public class TempReadingSourceConverter implements Converter<String, TempReadingSource> { @Override public TempReadingSource convert(String source) { try { return TempReadingSource.fromValue(source); } catch(Exception e) { return null; } }}這Converter解決了我的 API(通過(guò)郵遞員)但現(xiàn)在swagger-ui找不到:
如果使用 WebMvcConfigurationSupport 添加轉(zhuǎn)換器
寶慕林4294392
2023-10-12 14:46:10