我有一个使用camel-rest-openapi组件来调用Petstore OpenAPI规范中定义的操作的路由。当尝试运行导致 HTTP 404 响应的路由时,组件会多次附加 basePath。
例如:petstore3.swagger.io /api/v3/api/v3 /pet/findByStatus?status=可用
下面是使用 Yaml DSL 的路由定义和camel jbang 的输出。
路线:
- from:
uri: "timer:fetchPets?repeatCount=1"
description: "Periodically fetches list of pets"
steps:
- to: "rest-openapi:https://petstore3.swagger.io/api/v3/openapi.json#findPetsByStatus?status=available&componentName=https"
- log: "Received pets list: ${body}"
输出日志:
jake@localhost ~ % camel run sandbox.yaml
[jbang] Resolving dependencies...
[jbang] org.apache.camel:camel-bom:4.5.0@pom
[jbang] org.apache.camel:camel-jbang-core:4.5.0
[jbang] org.apache.camel.kamelets:camel-kamelets:4.4.1
[jbang] Dependencies resolved
[jbang] Building jar for CamelJBang.java...
Note: Annotation processing is enabled because one or more processors were found
on the class path. A future release of javac may disable annotation processing
unless at least one processor is specified by name (-processor), or a search
path is specified (--processor-path, --processor-module-path), or annotation
processing is enabled explicitly (-proc:only, -proc:full).
Use -Xlint:-options to suppress this message.
Use -proc:none to disable annotation processing.
2024-03-28 10:38:46.875 INFO 78257 --- [ main] org.apache.camel.main.MainSupport : Apache Camel (JBang) 4.5.0 is starting
2024-03-28 10:38:46.973 INFO 78257 --- [ main] org.apache.camel.main.MainSupport : Using Java 21.0.1 with PID 78257. Started by jake in /Users/jake
2024-03-28 10:38:47.036 INFO 78257 --- [ main] apache.camel.main.ProfileConfigurer : The application is starting with profile: dev
2024-03-28 10:38:47.127 INFO 78257 --- [ main] mel.cli.connector.LocalCliConnector : Management from Camel JBang enabled
2024-03-28 10:38:47.829 INFO 78257 --- [ main] .download.MavenDependencyDownloader : Downloaded: org.apache.camel:camel-rest-openapi:4.5.0 (took: 679ms) from: central@https://repo1.maven.org/maven2
2024-03-28 10:38:47.848 INFO 78257 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.5.0 (sandbox) is starting
2024-03-28 10:38:48.830 INFO 78257 --- [ main] .download.MavenDependencyDownloader : Downloaded: org.apache.camel:camel-http:4.5.0 (took: 569ms) from: central@https://repo1.maven.org/maven2
2024-03-28 10:38:48.904 INFO 78257 --- [ main] el.impl.engine.AbstractCamelContext : Routes startup (total:1)
2024-03-28 10:38:48.905 INFO 78257 --- [ main] el.impl.engine.AbstractCamelContext : Started route1 (timer://fetchPets)
2024-03-28 10:38:48.905 INFO 78257 --- [ main] el.impl.engine.AbstractCamelContext : Apache Camel 4.5.0 (sandbox) started in 1s56ms (build:0ms init:0ms start:1s56ms)
2024-03-28 10:38:50.136 ERROR 78257 --- [mer://fetchPets] or.errorhandler.DefaultErrorHandler : Failed delivery for (MessageId: D038A8A058C3548-0000000000000000 on ExchangeId: D038A8A058C3548-0000000000000000). Exhausted after delivery attempt: 1 caught: org.apache.camel.http.base.HttpOperationFailedException: HTTP operation failed invoking https://petstore3.swagger.io/api/v3/api/v3/pet/findByStatus?status=available with statusCode: 404
尝试使用 Petstore 规范的 v2 和 v3。还尝试通过在 application.properties 中设置basePath属性并将其作为camel-rest-openapi组件上的查询参数来覆盖规范中的basePath。
openapi.json 包含
"servers":[{"url":"/api/v3"}]
,它不是完整的 URL,可能会导致一些问题。
在文档中 https://camel.apache.org/components/4.4.x/rest-openapi-component.html 他们设置了参数
host=https://petstore3.swagger.io
,无需使用 basePath
参数就足够了。