首页 > 解决方案 > 使用 apache camel 将 url 路由到外部 rest 控制器

问题描述

有没有办法将所有请求路由到特定的 URI 到另一个项目的休息控制器?考虑下面的代码:

    @Component
public class CamelSportsRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        restConfiguration()
                .component("servlet")
                .bindingMode(RestBindingMode.auto);

        rest().path("/hello").get().route()
                .toD("localhost:9080/hello");
}
}

我想将所有请求路由/hello 到另一个项目休息控制器端点:localhost:9080/hello但没有 XML 就不可能。

标签: javaspringspring-bootspring-mvcapache-camel

解决方案


请参阅 jetty 和 undertown 组件的 matchOnUriPrefix 和 HTTP 组件的 bridgeEndpoint 选项。

这就是你需要的:

from("undertow:http://localhost:8080/hello?matchOnUriPrefix=true")
.to("http4://localhost:8081/hello?bridgeEndpoint=true");

另请参阅有关更多详细信息的另一个答案https://stackoverflow.com/a/67893371/11052487


推荐阅读