如何为Camel HTTP组件添加URL末尾斜杠?
Camel REST调用末尾斜杠缺失导致404的解决方法
问题场景
调用部署在https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1/verteileauftrag/的REST POST服务时,通过VS Code等客户端带末尾斜杠的请求可正常执行,但使用Camel路由调用时返回404错误,原因是实际请求URL缺失末尾斜杠。
原路由配置:
restConfiguration().host("https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1").component("http").bindingMode(RestBindingMode.json); // 调用代码 .to("rest:post:verteileauftrag?outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType")
尝试在路径后添加斜杠(verteileauftrag/)未生效,使用环境为Red Hat Fuse 7_10_2。
解决方案
方案1:改用HTTP组件直接调用,精确控制URL
放弃rest组件的路径解析,直接用http组件指定完整带斜杠的URL,确保路径完全匹配:
from("...") .to("https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1/verteileauftrag/?bridgeEndpoint=true&bindingMode=json&outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType");
若需复用配置,可拆分host和上下文路径:
restConfiguration() .host("csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch") .contextPath("/api/v1") .scheme("https") .component("http") .bindingMode(RestBindingMode.json); from("...") .to("http://${restConfiguration.host}${restConfiguration.contextPath}/verteileauftrag/?bridgeEndpoint=true&outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType");
方案2:通过Exchange头动态设置路径
利用Exchange.HTTP_PATH头指定带末尾斜杠的路径,绕过rest组件的自动路径修剪:
restConfiguration() .host("https://csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch/api/v1") .component("http") .bindingMode(RestBindingMode.json); from("...") .setHeader(Exchange.HTTP_PATH, constant("verteileauftrag/")) .to("rest:post:?outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType");
方案3:调整restConfiguration的contextPath
将contextPath设置为包含末尾斜杠的完整路径,调用时留空路径部分:
restConfiguration() .host("csp-verteileauftrag-camunda-v1-csp-ims-dev-az.apcn.osp4-preprod.hel.kko.ch") .contextPath("/api/v1/verteileauftrag/") .scheme("https") .component("http") .bindingMode(RestBindingMode.json); from("...") .to("rest:post:?outType=ch.helsana.csp.verteileauftrag.rest.model.apiadapter.ResponseType");
内容的提问来源于stack exchange,提问作者Michel Riondel
相关产品推荐
相关产品推荐

