提问者:小点点

尝试在apache camel中调用Rest DSL时,postman中出现404错误


我正在尝试一个简单的Rest服务,使用apache camel和get作为超文本传输协议动词。

package com.ApacheCamelTraining.camelmicroservicea.routes.b;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;


@Component
public class MyFileRouter extends RouteBuilder{

    @Override
    public void configure() throws Exception {

        
        restConfiguration().component("servlet")
            .host("localhost").port(8080);
        
        rest("say/hello")
        
            .get().route().transform().constant("Hello World!")
            //.post().to("file:files/sayHello")
    ;
        
        from("file:files/input")
        .log("${body}")
        .to("file:files/output");
        
    }

}

我正在《邮递员》中尝试这一点:<代码>http://localhost:8080/demo/camel/say/hello由于应用程序中的上下文路径,添加了“demo”。属性文件,注意在添加以下内容之前,此文件为空:


camel.springboot.main-run-controller=true
server.servlet.context-path=/demo

“camel”是因为CamelServlet默认设置为“/camel”

请查找以下我的依赖项:camel-core:3.11.1 camel-servlet:3.11.1 camel-service-starter:3.11.1 camel-servlet-starter:3.0.0-RC3 camel-http-common:3.11.1 camel-spring-boot-starter:3.11.1

最后,这是我运行应用程序时控制台显示的内容

2021-09-13 11:02:06.117  INFO 20392 --- [  restartedMain] c.A.c.CamelMicroserviceAApplication      : Starting CamelMicroserviceAApplication using Java 11.0.12 on E210 with PID 20392 (D:\Users\User\MyFiles\Apache Camel github\Apache Camel Youtube Training\camel-microservice-a\target\classes started by User in D:\Users\User\MyFiles\Apache Camel github\Apache Camel Youtube Training\camel-microservice-a)
2021-09-13 11:02:06.117  INFO 20392 --- [  restartedMain] c.A.c.CamelMicroserviceAApplication      : No active profile set, falling back to default profiles: default
2021-09-13 11:02:06.302  INFO 20392 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-09-13 11:02:06.303  INFO 20392 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-09-13 11:02:06.303  INFO 20392 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
2021-09-13 11:02:06.330  INFO 20392 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/demo]   : Initializing Spring embedded WebApplicationContext
2021-09-13 11:02:06.330  INFO 20392 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 212 ms
2021-09-13 11:02:06.422  INFO 20392 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2021-09-13 11:02:06.424  INFO 20392 --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2021-09-13 11:02:06.432  INFO 20392 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/demo'
2021-09-13 11:02:06.455  INFO 20392 --- [  restartedMain] c.s.b.CamelSpringBootApplicationListener : Starting CamelMainRunController to ensure the main thread keeps running
2021-09-13 11:02:06.456  INFO 20392 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   : Routes startup summary (total:2 started:2)
2021-09-13 11:02:06.456  INFO 20392 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   :     Started route32 (file://files/input)
2021-09-13 11:02:06.456  INFO 20392 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   :     Started route33 (rest://get:say/hello)
2021-09-13 11:02:06.456  INFO 20392 --- [  restartedMain] o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.11.1 (camel-15) started in 20ms (build:2ms init:17ms start:1ms)
2021-09-13 11:02:06.456  INFO 20392 --- [  restartedMain] c.A.c.CamelMicroserviceAApplication      : Started CamelMicroserviceAApplication in 0.35 seconds (JVM running for 1966.925)
2021-09-13 11:02:06.457  INFO 20392 --- [  restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
2021-09-13 11:03:34.724  INFO 20392 --- [nio-8080-exec-5] o.a.c.c.C.[Tomcat].[localhost].[/demo]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-09-13 11:03:34.724  INFO 20392 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-09-13 11:03:34.726  INFO 20392 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

这是错误:

{
    "timestamp": "2021-09-13T12:55:34.581+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/demo/say/hello"
}

谢谢你,非常感谢你的帮助!


共1个答案

匿名用户

要设置上下文路径,您必须使用以下属性:

camel.servlet.mapping.context-path

或在yaml中:

camel:
  servlet:
    mapping:
      context-path:

看见https://camel.apache.org/components/3.12.x/servlet-component.html#_sb_option_camel_servlet_mapping_context-路径

我假设你从骆驼2迁移到骆驼3是因为骆驼2的属性

camel.component.servlet.mapping.context-path

事实证明,不同的骆驼示范回购协议对我非常有帮助:

  • 驼色Spring靴:https://github.com/apache/camel-spring-boot-examples

我在这里找到了针对您的具体案例的解决方案:https://github.com/apache/camel-spring-boot-examples/tree/main/rest-swagger