swagger 提供的定义在呈现openapi时未指定有效的版本字段

neekobn8  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(657)

我正在将OpenAPI与我的项目集成,当我访问url时:http://127.0.0.1:11014/swagger-ui/index.html,显示如下错误:

Unable to render this definition
The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

这是OpenAPI配置:

package misc.config.openapi;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * https://springdoc.org/
 * https://github.com/springdoc/springdoc-openapi
 */
@Configuration
public class OpenApiConfig {

    @Bean
    public GroupedOpenApi fortuneApi() {
        GroupedOpenApi.Builder builder = GroupedOpenApi.builder()
                .pathsToMatch("/fortune/**")
                .group("dddd");
        GroupedOpenApi groupedOpenApi = builder.build();
        return groupedOpenApi;
    }

    @Bean
    public OpenAPI fortuneAPI() {
        return new OpenAPI()
                .info(new Info().title("Fortune API")
                        .description("Spring shop sample application")
                        .version("v0.0.1")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));
    }

}

我已经阅读了问题Swagger..Unable to render this definition The provided definition does not specify a valid version field并尝试了答案,两者都不起作用。我应该做什么来指定版本?这是依赖关系看起来像:

api "org.springdoc:springdoc-openapi-ui:1.6.9"

我做了一个最小的重现,发现正常的响应是json对象,但问题响应返回字符串.这是正确的响应:

{
    "openapi": "3.0.1",
    "info": {
        "title": "Fortune API",
        "description": "Spring shop sample application",
        "license": {
            "name": "Apache 2.0",
            "url": "http://springdoc.org"
        },
        "version": "v0.0.1"
    },
    "externalDocs": {
        "description": "SpringShop Wiki Documentation",
        "url": "https://springshop.wiki.github.org/docs"
    },
    "servers": [
        {
            "url": "http://127.0.0.1:11018",
            "description": "Generated server url"
        }
    ],
    "paths": {},
    "components": {}
}

这是我的项目中的问题响应:

"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Fortune API\",\"description\":\"Spri......
mkshixfv

mkshixfv1#

最后我发现是我换了json转换器导致这个问题,这是一个解决的办法:

@EnableWebMvc
@Configuration
public class WebConvertConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        converters.add(new StringHttpMessageConverter());
    }
}

这个StringHttpMessageConverter应该是第一个添加到转换器中的。我认为这是openapi的设计问题,使它很难使用。更多信息来自这里:

相关问题