集成Camel-CXFRS时,如何让Apache Camel忽略OpenAPI相关URL?
问题描述
我想基于CXF服务生成OpenAPI文档,参考了官方的OpenApiFeature配置。当不通过camel-cxfrs启动服务时,openapi.json能正常生成,swagger-ui也可以正常工作;但使用camel-cxfrs集成后,Apache Camel会拦截OpenAPI相关的URL并尝试处理,导致无法正常访问。
我的Blueprint配置如下:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" xmlns:cxf="http://cxf.apache.org/blueprint/core" xmlns:jaxrs="http://cxf.apache.org/blueprint/jaxrs" xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd "> <!-- JAXRS providers --> <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" /> <!-- CXF OpenApiFeature --> <bean id="openApiFeature" class="org.apache.cxf.jaxrs.openapi.OpenApiFeature"/> <camelcxf:rsServer id="myRsServer" address="/service" serviceClass="foo.MyService"> <camelcxf:providers> <ref component-id="jsonProvider" /> </camelcxf:providers> <camelcxf:features> <ref component-id="openApiFeature" /> </camelcxf:features> </camelcxf:rsServer> <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="cxfrs:bean:myRsServer"/> .... </route> </camelContext> </blueprint>
使用的技术栈:
- karaf 4.2.4
- apache camel 2.23.2
- cxf 3.2.7
- cxf-rs-description-openapi-v3(karaf feature)
- org.webjars/swagger-ui/3.23.8(bundle)
请问有没有办法让Apache Camel忽略特定URL(比如cxf/service/openapi.json)?
解决方案
有两种可行方式解决这个问题:
方法1:配置CXF RS Server跳过指定路径
在camelcxf:rsServer节点中添加skipPaths属性,指定需要跳过的OpenAPI相关路径,让这类请求直接由CXF处理,不进入Camel路由:
<camelcxf:rsServer id="myRsServer" address="/service" serviceClass="foo.MyService" skipPaths="/openapi.json,/swagger-ui/**"> <!-- 原有providers、features配置保持不变 --> </camelcxf:rsServer>
skipPaths支持多个路径,用逗号分隔,还可使用通配符(例如/swagger-ui/**匹配swagger-ui的所有资源请求)。
方法2:在Camel路由中添加条件判断
若不想修改CXF配置,可在Camel路由内添加逻辑判断,当请求路径匹配OpenAPI相关URL时,直接将请求交回CXF处理,不执行后续路由逻辑:
<camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="cxfrs:bean:myRsServer"/> <!-- 判断请求路径是否为OpenAPI相关 --> <choice> <when> <simple>${header.CamelHttpPath} regex '^/openapi.json$' or ${header.CamelHttpPath} regex '^/swagger-ui/.*'</simple> <!-- 转发回CXF处理该请求 --> <to uri="cxfrs:bean:myRsServer?bridgeEndpoint=true"/> </when> <otherwise> <!-- 原有业务路由逻辑 --> .... </otherwise> </choice> </route> </camelContext>
这里利用Camel的CamelHttpPath头信息匹配请求路径,匹配成功后通过bridgeEndpoint=true将请求转发回CXF,避免Camel拦截处理。
内容的提问来源于stack exchange,提问作者tim08
相关产品推荐
相关产品推荐

