从基本 Spring Boot Camel REST DSL 请求中获取 404 "Not Found" 错误
Getting 404 "Not Found" error from basic Spring Boot Camel REST DSL request
过去几天我一直在研究 Camel 和 REST DSL 的基本演示,但我终于遇到了这个问题。我对 Spring Boot 的魔法还很陌生,所以非常感谢任何反馈!
当我 运行 项目时,对于我的两条路线,一切看起来都正确启动。但是,当我尝试 GET 请求时,我总是收到 404 "Not Found" 消息。
我仅有的两个 classes 在同一个包中,所以我认为可见性不会成为问题。我将项目创建为 Spring 启动项目,并将其 运行 作为 Spring 启动应用程序。日志显示两条路由启动成功,Tomcat正在使用8080端口,context路径在application.properties文件中具体配置
我的主要应用class:
包 com.example.demo;
import org.apache.camel.CamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@Configuration
@ComponentScan("com.example.demo")
public class DemoApplication {
@Autowired
CamelContext camelContext;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
我的路由器class:
package com.example.demo;
import org.springframework.stereotype.Component;
import org.apache.camel.LoggingLevel;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.builder.RouteBuilder;
@Component
public class DemoCamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration().component("servlet")
.bindingMode(RestBindingMode.auto);
rest().get("/hello")
.to("direct:hello");
from("direct:hello")
.log(LoggingLevel.INFO, "Hello World")
.transform().simple("Hello World");
}
}
我的 application.properties 文件:
camel.springboot.main-run-controller=true
server.servlet.context-path=/demo
我正在使用 http://localhost:8080/demo/hello
测试此服务
这是我的 Maven 依赖项。
spring-boot-starter-web (managed:2.1.0.RELEASE)
spring-boot-starter-web-services (managed:2.1.0.RELEASE)
spring-boot-starter-test (managed:2.1.0.RELEASE)
camel-core: 2.22.2
camel-spring-boot-starter: 2.22.2
camel-servlet: 2.22.2
camel-http-common: 2.22.2
javax.servlet-api (managed:4.0.1)
最后,这是日志显示的内容。
2018-11-21 00:58:38.454 INFO 11072 --- [ main] o.a.camel.spring.boot.RoutesCollector : Starting CamelMainRunController to ensure the main thread keeps running
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route2 started and consuming from: direct://hello
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: servlet:/hello?httpMethodRestrict=GET
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Total 2 routes, of which 2 are started
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.22.2 (CamelContext: camel-1) started in 0.236 seconds
2018-11-21 00:58:38.486 INFO 11072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/demo'
2018-11-21 00:58:38.501 INFO 11072 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.882 seconds (JVM running for 6.868)
2018-11-21 01:00:12.445 INFO 11072 --- [nio-8080-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-11-21 01:00:12.445 INFO 11072 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2018-11-21 01:00:12.453 INFO 11072 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
从 Camel 的 2.19 版本开始,CamelServlet 默认设置为“/camel”。所以你的 url 变成:
http://localhost:8080/demo/camel/hello
更新:
您还需要在使用 camel-servlet
:
时定义以下 bean
@Bean
public ServletRegistrationBean<Servlet> servletRegistrationBean() {
ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>(new CamelHttpTransportServlet(), "/camel/*");
registration.setName("CamelServlet");
return registration;
}
或者您应该添加 camel-servlet-starter
依赖项而不是 camel-servlet
依赖项
过去几天我一直在研究 Camel 和 REST DSL 的基本演示,但我终于遇到了这个问题。我对 Spring Boot 的魔法还很陌生,所以非常感谢任何反馈!
当我 运行 项目时,对于我的两条路线,一切看起来都正确启动。但是,当我尝试 GET 请求时,我总是收到 404 "Not Found" 消息。
我仅有的两个 classes 在同一个包中,所以我认为可见性不会成为问题。我将项目创建为 Spring 启动项目,并将其 运行 作为 Spring 启动应用程序。日志显示两条路由启动成功,Tomcat正在使用8080端口,context路径在application.properties文件中具体配置
我的主要应用class:
包 com.example.demo;
import org.apache.camel.CamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@Configuration
@ComponentScan("com.example.demo")
public class DemoApplication {
@Autowired
CamelContext camelContext;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
我的路由器class:
package com.example.demo;
import org.springframework.stereotype.Component;
import org.apache.camel.LoggingLevel;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.builder.RouteBuilder;
@Component
public class DemoCamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration().component("servlet")
.bindingMode(RestBindingMode.auto);
rest().get("/hello")
.to("direct:hello");
from("direct:hello")
.log(LoggingLevel.INFO, "Hello World")
.transform().simple("Hello World");
}
}
我的 application.properties 文件:
camel.springboot.main-run-controller=true
server.servlet.context-path=/demo
我正在使用 http://localhost:8080/demo/hello
测试此服务这是我的 Maven 依赖项。
spring-boot-starter-web (managed:2.1.0.RELEASE)
spring-boot-starter-web-services (managed:2.1.0.RELEASE)
spring-boot-starter-test (managed:2.1.0.RELEASE)
camel-core: 2.22.2
camel-spring-boot-starter: 2.22.2
camel-servlet: 2.22.2
camel-http-common: 2.22.2
javax.servlet-api (managed:4.0.1)
最后,这是日志显示的内容。
2018-11-21 00:58:38.454 INFO 11072 --- [ main] o.a.camel.spring.boot.RoutesCollector : Starting CamelMainRunController to ensure the main thread keeps running
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route2 started and consuming from: direct://hello
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: servlet:/hello?httpMethodRestrict=GET
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Total 2 routes, of which 2 are started
2018-11-21 00:58:38.470 INFO 11072 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.22.2 (CamelContext: camel-1) started in 0.236 seconds
2018-11-21 00:58:38.486 INFO 11072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/demo'
2018-11-21 00:58:38.501 INFO 11072 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.882 seconds (JVM running for 6.868)
2018-11-21 01:00:12.445 INFO 11072 --- [nio-8080-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-11-21 01:00:12.445 INFO 11072 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2018-11-21 01:00:12.453 INFO 11072 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
从 Camel 的 2.19 版本开始,CamelServlet 默认设置为“/camel”。所以你的 url 变成:
http://localhost:8080/demo/camel/hello
更新:
您还需要在使用 camel-servlet
:
@Bean
public ServletRegistrationBean<Servlet> servletRegistrationBean() {
ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>(new CamelHttpTransportServlet(), "/camel/*");
registration.setName("CamelServlet");
return registration;
}
或者您应该添加 camel-servlet-starter
依赖项而不是 camel-servlet
依赖项