英文:
JAVA: Parsing OPEN API with swagger.v3.parser.OpenAPI
问题
我正在尝试解析一个OPEN API URL,以下是相关的代码:
OpenAPI openAPI = new OpenAPIV3Parser().read("https://petstore3.swagger.io/api/v3/openapi.json");
这段代码出现了以下异常:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactoryBuilder
我正在使用的库是:
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-v3</artifactId>
<version>2.0.19</version>
</dependency>
是否有其他库可以用于解析 OPEN API 3.0?
英文:
I'm trying to parse an OPEN API URL this is the following code for it
OpenAPI openAPI = new OpenAPIV3Parser().read("https://petstore3.swagger.io/api/v3/openapi.json");
which is giving me the following exception
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactoryBuilder
The library which i'm using is
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser-v3</artifactId>
<version>2.0.19</version>
</dependency>
Is there any other library which i can use for parsing OPEN API 3.0?
答案1
得分: 2
如果您查看 OpenAPI.read() 函数,它接受以下两种参数形式:
- (String location)
- (String location, List
auths, ParseOptions resolve)
但是您正在尝试使用的是 "String URL",相反地,请尝试使用以下依赖项并提供 (String "filePath")。它将正常工作。
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.0.26</version>
</dependency>
注意:
- 对于 OpenAPI 3.0,使用以下代码:
OpenAPI openAPI = new OpenAPIV3Parser().read(filePath)
- 对于 Swagger 2.0,使用以下代码:
Swagger swagger = new SwaggerParser().read(filePath)
以下是一个有关 read() 函数的示例代码,以帮助您更清楚地理解:
您可以使用 Swagger 编辑器将 JSON 文件转换为 YAML 文件类型:
- 打开 Swagger 编辑器
- 进入 "Edit" 菜单
- 选择 "Convert to YAML"
- 下载转换后的文件,并将其移动到 filePath
英文:
If you check OpenAPI.read() it accepts either
- (String location) or
- (String location, List<AuthorizationValue> auths, ParseOptions resolve)
But what you are trying with is "String URL", instead try giving (String "filePath") with below dependency. It will work like charm.
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.0.26</version>
</dependency>
Note:
OpenAPI openAPI = new OpenAPIV3Parser().read(filePath) for OpenAPI3.0
Swagger swagger = new SwaggerParser().read(filePath) for Swagger2.0
Check out below read() snippet handy for your clarity.
You can use swagger editor to switch json file type to yaml.
Swagger editor >> Edit >> Convert to YAML >> downloaded file move it to filePath
答案2
得分: 1
使用以下依赖项:
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.0.25</version>
</dependency>
这对于解析 OpenAPI 和 Swagger 规范对我来说有效。
英文:
Use the dependency:
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.0.25</version>
</dependency>
This works for me parsing OpenAPI and Swagger Specifications
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论