使用 Apache Camel 公开 Rest API - 收到 404

问题描述 投票:0回答:2

我尝试了非常简单的路线:

    restConfiguration()
            .component("servlet");

    rest().get("/hello")
            .to("direct:hello");

    from("direct:hello")
            .log(LoggingLevel.INFO, "Hello World")
            .transform().simple("Hello World");

事实上,在使用我的 Spring Boot 应用程序时,我可以看到该路由正在监听它: [在此输入图像描述][1]

2021-04-14 15:03:31.759 INFO 25248 --- [ main] o.a.c.i.e.InternalRouteStartupManager :路由:route1 启动并消耗自: direct://hello 2021-04-14 15:03:31.760 INFO 25248 --- [ main] o.a.c.i.e.InternalRouteStartupManager : 路由:route2 启动并消耗自:servlet:/hello 2021-04-14 15:03:31.768 INFO 25248 --- [ main] o.a.c.impl.engine.AbstractCamelContext :总共 2 条路线,其中 2 条已启动 2021-04-14 15:03:31.768 INFO 25248 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.7.1 (camel-1) 在 220ms 内启动

但是,当我尝试访问此地址时,收到 404 错误。

{ "时间戳": "2021-04-14T12:17:11.975+00:00", “状态”:404, “错误”:“未找到”, “信息”: ””, “路径”:“/你好” }

也在我的控制台中: 2021-04-14 15:17:11.973 警告 25248 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound : 没有 GET /hello 的映射

知道如何解决吗? 我想说的是,我在谷歌上搜索了这个问题和解决方案,例如使用“camel.component.servlet.mapping.context-path=/* ”或使用“camel”前缀对我来说不起作用。

谢谢!

apache-camel
2个回答
0
投票

创建一个像任何配置类一样的bean

  @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @Bean
        public ServletRegistrationBean camelServletRegistrationBean() {
            ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), "/*");
            registration.setName("CamelServlet");
            return registration;
        }}

例如骆驼休息和弹簧靴:https://github.com/erayerdem/camelrest


0
投票

我的问题是因为我是从 Camel 2 迁移过来的。 旧的配置是:camel.component.servlet.mapping.context-path 从 Camel 3 开始,应该是这样的:camel.servlet.mapping.context-path

© www.soinside.com 2019 - 2024. All rights reserved.