Javascript Swagger..无法呈现此定义提供的定义未指定有效的版本字段
无法呈现此定义提供的定义不存在 指定有效的版本字段 请指明有效的Swagger或OpenAPI版本字段。支持 版本字段是招摇的:“2.0”和那些匹配openapi:3.0.n的字段 (例如,openapi:3.0.0) 我需要在何处插入正确的版本以停止下面的错误。 Swagger编辑器工作正常,但在启动特定项目时,我收到此错误。第一次使用Swagger。 非常感谢您缺少OpenAPI/Swagger版本号,在本例中,Javascript Swagger..无法呈现此定义提供的定义未指定有效的版本字段,javascript,api,swagger,Javascript,Api,Swagger,无法呈现此定义提供的定义不存在 指定有效的版本字段 请指明有效的Swagger或OpenAPI版本字段。支持 版本字段是招摇的:“2.0”和那些匹配openapi:3.0.n的字段 (例如,openapi:3.0.0) 我需要在何处插入正确的版本以停止下面的错误。 Swagger编辑器工作正常,但在启动特定项目时,我收到此错误。第一次使用Swagger。 非常感谢您缺少OpenAPI/Swagger版本号,在本例中,“Swagger”:“2.0”。在开头添加它,如下所示: { "swag
“Swagger”:“2.0”
。在开头添加它,如下所示:
{
"swagger": "2.0",
"title" : "Music API Documentation",
...
我今天遇到了同样的问题。在我的例子中,是
Gson
配置导致了错误。如果您还将Gson
与SpringMVC
一起使用,可能您可以尝试以下方法:
我正在使用SpringBoot2.2.4和SpringFoxSwagger 2.9.2建立一个RESTAPI项目。因为我对Gson
比Jackson
更熟悉,所以我替换了默认的MessageConverter:
@配置
@EnableWebMvc
公共类MVCConfigure实现WebMVCConfiguer{
私有最终Gson Gson=新Gson();
// ......
@凌驾
public void configureMessageConverters(List这里是一个针对这个问题的解决方案,它使用的是OpenAPI V3,它对我有用。
我认为这也可能与其他版本相关
我尝试使用Gson而不是Jackson作为Spring的默认序列化程序。
Gson生成的/v3/api文档用双引号括起来。
因此/swagger ui/index.html显示了该线程中处理的错误
回到杰克逊帮我解决了这个问题。
如果您必须使用Gson,也许上面编写的@Lyux解决方案适合您。我也遇到了这个问题,在我的情况下,/v3/api doc
被LoginIntercept
截取,我的解决方案是:
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getRequestURI().startsWith("/swagger") || "/v3/api-docs".equals(request.getRequestURI())) {
return true;
}
}
遇到类似的情况,pom上使用的swagger库是
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
最后在basePackage下创建控制器
希望它对您有用。我在一个.net核心项目中遇到过两次这个问题。(请随时告诉我去问我自己的问题)
在Startup.cs
的Configure
方法中,我需要验证为UI呈现的json路径。这应该类似于:
app.UseSwaggerUI(c => { c.SwaggerEndpoint("./v1/swagger.json", "MyServiceAPI"); });
至少下次遇到这个问题时,我不会那么困惑。就我而言,我使用的是
const openapiSpecification = swaggerJsdoc(options);
当我真的
“但在启动特定项目时”-哪个项目?如何启动?您可以发布此项目中的OpenAPI YAML/JSON文件吗?详细信息越多,其他人就越容易找出问题所在以及如何解决问题。使用npm start启动。项目为git+。我没有看到OpenAPI YAML/JSON。是否认为这可能是问题?请确认是否/v2/api文档
可访问。我必须将v2规范传递给查询表单。由于“为“/v3/api文档”映射的ambigous处理程序方法。例如,我使用的是springfox boot starter 3.0.0。我在第1行有openapi:3.0.0
,但我得到了this@shorif2000问题可能在其他地方。请发布您的OpenAPI文件。谢谢!它解决了我的问题。顺便说一句,parser.parse(json.value())
似乎已被弃用,因此我使用了静态方法JsonParser.parseString(json.value())
取而代之。干杯!没问题,这是我最不想找的东西,为我解决了,Thanks!!!!这对我真的很有帮助!!谢谢!!谢谢,它对我有用..!这个答案对我有用。Swagger在我的本地机器上工作得很好,但在我做出此更改之前不会在服务器上工作(与OP相同的错误消息)。谢谢!
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created by nyakundid
*/
@EnableScheduling
@EnableAsync
@SpringBootApplication
@EnableSwagger2
public class Application implements CommandLineRunner {
public Log log = LogFactory.getLog(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.package.controller")).build();
}
}
app.UseSwaggerUI(c => { c.SwaggerEndpoint("./v1/swagger.json", "MyServiceAPI"); });
const openapiSpecification = swaggerJsdoc(options);
const openapiSpecification = await swaggerJsdoc(options);