javascript Swagger..Unable to render this definition 提供的定义没有指定有效的版本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51951641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swagger..Unable to render this definition The provided definition does not specify a valid version field
提问by Paddy Popeye
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).
无法呈现此定义 提供的定义未指定有效的版本字段。
请指明有效的 Swagger 或 OpenAPI 版本字段。支持的版本字段是 swagger: "2.0" 和匹配 openapi: 3.0.n 的字段(例如 openapi: 3.0.0)。
Where do I need to insert the correct version to the stop the error below. Swagger editor works ok, but when launching a particular project I receive this error.First time using Swagger. Many Thanks
我需要在哪里插入正确的版本来阻止下面的错误。Swagger 编辑器工作正常,但在启动特定项目时收到此错误。第一次使用 Swagger。非常感谢
采纳答案by Helen
Your API definitionis missing the OpenAPI/Swagger version number, in this case "swagger": "2.0". Add it at the beginning, like so:
您的API 定义缺少 OpenAPI/Swagger 版本号,在本例中为"swagger": "2.0"。在开头添加它,如下所示:
{
"swagger": "2.0",
"title" : "Music API Documentation",
...
回答by Lyux
I encountered this same problem today. In my case it is the Gsonconfiguration which lead to the error. If you are also using Gsonwith SpringMVC, may be you could try this out:
我今天遇到了同样的问题。在我的情况下,是Gson导致错误的配置。如果您也使用Gsonwith SpringMVC,也许您可以试试这个:
I am setting up a rest-api project using spring boot 2.2.4 and spring fox swagger 2.9.2 . Since I am more familiar with Gsonthan Hymanson, I replaced the default MessageConverter:
我正在使用 spring boot 2.2.4 和 spring fox swagger 2.9.2 设置一个 rest-api 项目。由于我Gson比更熟悉Hymanson,我替换了默认的 MessageConverter:
@Configuration
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
private final Gson gson = new Gson();
// ......
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
gsonHttpMessageConverter.setGson(gson);
converters.add(gsonHttpMessageConverter);
}
// ......
}
After that the "http://localhost:8080/swagger-ui.html" breaks like the question described. Then I typed "http://localhost:8080/v2/api-docs" in browser and noticed the swagger doc was wrapped into another layer, with an unnecessary "value" field like this:
之后,“ http://localhost:8080/swagger-ui.html”像描述的问题一样中断。然后我在浏览器中输入“ http://localhost:8080/v2/api-docs”并注意到swagger doc被包装到另一层,有一个不必要的“值”字段,如下所示:
{
value: "{"swagger":"2.0","info":{"description": ......}}"
}
No doubt swagger cannot find the "swagger":"2.0"field if api-docs yields something like that. The value of "value" field is the actual swagger doc.
毫无疑问,"swagger":"2.0"如果 api-docs 产生类似的东西,swagger 就找不到这个领域。“value”字段的值是实际的 swagger 文档。
After some search I found that swagger doc will be incorrectly serialized by Gson if you leave it be. The solution is simple - register a serializer for swagger to your Gson bean:
经过一番搜索后,我发现如果不这样做,Gson 会错误地对 swagger 文档进行序列化。解决方案很简单 - 为您的 Gson bean 注册一个用于 swagger 的序列化程序:
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter())
.create();
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());
}
}
where Jsonis a class provided by springfox:
其中Json是通过提供springfox一类:
import springfox.documentation.spring.web.json.Json;
Hope this would help.
希望这会有所帮助。
回答by Boris
Here is a solution for this problem with OpenAPI V3 that worked for me. I assume this can be related for other versions as well.
这是 OpenAPI V3 这个问题的解决方案,它对我有用。我认为这也可能与其他版本相关。
I experimented with Gson instead of Hymanson as Spring's default serializer. The /v3/api-docs that Gson generates is wrapped in double quotes. Thus the /swagger-ui/index.html shows the error addressed in this thread.
我尝试使用 Gson 而不是 Hymanson 作为 Spring 的默认序列化程序。Gson 生成的 /v3/api-docs 用双引号括起来。因此 /swagger-ui/index.html 显示了此线程中解决的错误。
Reverting to Hymanson solved this for me. If you must use Gson, maybe the solution @Lyux wrote above can suit you.
回到Hyman逊为我解决了这个问题。如果您必须使用 Gson,也许上面@Lyux 写的解决方案可以适合您。

