Quarkus/Camel应用报错:找不到RestApiConsumerFactory类型的servlet Bean
Quarkus/Camel Rest应用报错解决方案
问题描述
搭建Quarkus/Camel应用时遇到两个关联报错:
- 配置
.component("servlet")后启动报错:
No bean could be found in the registry for: servlet of type: org.apache.camel.spi.RestApiConsumerFactory
- 移除
.component("servlet")后,新报错:
java.lang.IllegalStateException: Cannot find RestApiProcessorFactory in Registry or classpath (such as the camel-openapi-java component)
原因分析
- 第一个错误:使用
servlet作为Rest组件,但未引入对应的Quarkus Camel Servlet扩展依赖,导致容器找不到servlet类型的RestApiConsumerFactory实现类。 - 第二个错误:路由中配置了
apiContextPath("/api-doc")用于生成OpenAPI文档,但缺少提供RestApiProcessorFactory的依赖(即camel-openapi-java相关扩展)。
解决方案
1. 添加缺失的依赖
在你的pom.xml中补充以下两个依赖:
<!-- 提供servlet类型的Rest组件支持 --> <dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-servlet</artifactId> </dependency> <!-- 提供OpenAPI文档生成所需的RestApiProcessorFactory --> <dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-openapi-java</artifactId> </dependency>
2. 验证路由配置
你的现有路由配置无需修改,补充依赖后,.component("servlet")和apiContextPath("/api-doc")的配置都能正常生效:
public class RestRoute extends RouteBuilder { @Override public void configure() throws Exception { restConfiguration() .component("servlet") .bindingMode(RestBindingMode.json) .dataFormatProperty("prettyPrint", "true") .apiContextPath("/api-doc") .apiProperty("api.title", "REST API").apiProperty("api.version", "1.0") .apiProperty("cors", "true"); rest("/") .consumes("application/json") .produces("application/json") .get() .outType(TestObj.class) .to("direct:getTestObj"); from("direct:getTestObj") .transform() .constant( TestObj.builder().fieldOne("Hello").fieldTwo("World").build() ); } }
3. 重启应用
添加依赖后,重新构建并启动Quarkus应用,两个报错都会被解决,你可以访问/api-doc查看生成的OpenAPI文档,同时REST接口也能正常提供服务。
内容的提问来源于stack exchange,提问作者Snappawapa
相关产品推荐
相关产品推荐

