简体   繁体   中英

REST endpoint not found when invoked and was created using Apache Camel

I'm using Apache Camel and SpringBoot to programmatically create a REST endpoint, but getting an error when invoking the endpoint using Postman.

build.gradle.kts :

dependencies {

  implementation("org.springframework.boot:spring-boot-starter-web")

  implementation("org.apache.camel:camel-core:3.14.0")

  implementation("org.apache.camel.springboot:camel-spring-boot-starter:3.14.0")

  implementation("org.apache.camel.springboot:camel-servlet-starter:3.14.0")

  implementation("com.sun.activation:javax.activation:1.2.0")
}

App.java :

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

BatchFileService.java :

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;

@Component
public class BatchFileService extends RouteBuilder {

  @Override
  public void configure() throws Exception {

    restConfiguration()
        .component("servlet")
        .bindingMode(RestBindingMode.auto);

    rest("/batchFile")
        .consumes("application/json")
        .produces("application/json")
        .post("/routeStart")
        .to("direct:startRoute");
  }
}

HttpRouteBuilder.java :

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HttpRouteBuilder  extends RouteBuilder{

  @Autowired
  private StartRouteProcessor startRouteProcessor;

  @Override
  public void configure() throws Exception {
    from("direct:startRoute").log("Inside StartRoute")
        .process(startRouteProcessor);
  }
}

StartRouteProcessor.java :

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.stereotype.Component;

@Component("startRouteProcessor")
public class StartRouteProcessor implements Processor {

  public void process(Exchange exchange) throws Exception {
    String message = exchange.getIn().getBody(String.class);
    System.out.println(message);
  }
}

And see the following in the logs on app startup:

o.a.c.impl.engine.AbstractCamelContext   : Routes startup (total:2 started:2)
o.a.c.impl.engine.AbstractCamelContext   :     Started route1 (rest://post:/batchFile:/routeStart)
o.a.c.impl.engine.AbstractCamelContext   :     Started route2 (direct://startRoute)
o.a.c.impl.engine.AbstractCamelContext   : Apache Camel 3.14.0 (camel-1) started in 238ms (build:48ms init:176ms start:14ms)
o.a.c.c.s.CamelHttpTransportServlet      : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

In postman, I exercise the uri using: POST http://localhost:8080/batchFile/routeStart/

{
 "title" : "test title",
 "singer" : "some singer"
}

But why do I get a 404 error?

{
    "timestamp": "2022-01-06T22:41:47.714+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/batchFile/routeStart/"
}

I know this is old question but the solution is in the application properties, the rigth endpoint is http://localhost:8080/camel/batchFile/routeStart/ the /camel/ is a property used in servlet, but you can change it with this in your applicationsProperties camel.servlet.mapping.context-path for example:

camel.servlet.mapping.context-path =/youcanchangethis/*

the * is for use the next parameters inside your rest route

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM