调用REST端点时未找到该端点,该端点是使用Apache Camel创建的

chhqkbe1  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(160)

我正在使用ApacheCamel和SpringBoot以编程方式创建REST端点,但在使用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);
  }
}

并在应用程序启动时查看日志中的以下内容:

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 ''

在postman中,我使用以下代码练习uri:开机自检http://localhost:8080/batchFile/routeStart/

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

但是为什么我会收到404错误?

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

iaqfqrcu1#

我知道这是一个老问题,但解决方案是在应用程序属性中,正确的端点是http://localhost:8080/camel/batchFile/routeStart//camel/是servlet中使用的属性,但您可以在应用程序属性camel.servlet.mapping.context-path中更改它,例如:

camel.servlet.mapping.context-path =/youcanchangethis/*
  • 号表示在您的休息路线中使用下一个参数

相关问题