springboot整合gson打开swagger报错:Unable to render this definition。The provided definition does not specify a valid version field.

 

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).

 

springboot整合gson之后打开swagger会报错,

 

 

将gson获取方式改下

 

import springfox.documentation.spring.web.json.Json;

 

private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter())
            .create();


    public static Gson getGson() {
        return gson;
    }

    private static class SpringfoxJsonToGsonAdapter implements JsonSerializer<Json> {
        @Override
        public JsonElement serialize(Json json, Type type, JsonSerializationContext context) {
            final JsonParser parser = new JsonParser();
            return parser.parse(json.value());
        }
    }

 

相关依赖,已经引入了就不需要了

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-web</artifactId>
            <version>3.0.0</version>
            <scope>compile</scope>
        </dependency>

 

posted @ 2022-06-29 09:42  yvioo  阅读(2877)  评论(0编辑  收藏  举报