Spring boot 2.x with Camel 2.25 : Spring specific endpoints not working

1.3k views Asked by At

I have a project with spring-boot 2.x and camel 2.25. It has different camel routes along with few REST consumer routes. Everything is good till this point.

Now I added few normal spring-boot @RestController classes with some endpoints. But these are not working (throwing 404).

When I investigated I found, that every request is coming to CamelServlet which is totally unaware of spring based normal @RestController endpoints (but knows only Camel REST consumer route endpoints). Hence throwing this error for only @RestController endpoints whereas Camel REST endpoints are still working.

Below is my configuration,

spring:
 application:
  name: gateway
 main:
  web-application-type: SERVLET 


server:
 servlet:
  context-path: /gateway
 port: 8080

camel:
 springboot:
  name: gateway
 component:
  servlet:
   mapping:
    enabled: true
    context-path: /*
  mail:
   basic-property-binding: true

Below is my POM

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-servlet-starter</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-mail-starter</artifactId>
    </dependency>

Is there anything I am doing wrong? Any suggestion? Thanks in advance.

1

There are 1 answers

3
Bhushan Uniyal On BEST ANSWER

This is because of you set context-path: /* pattern means camel is going intercept(because this path is register with camel) it, before spring servlet dispatcher gonna handle it, so if you want to handle @Restcontroller then you need to define a separate context path for camel, example: context-path: camel-api/* pattern, now camel will register camel-api base route, and if the pattern is different from camel-api URL, it will handle by spring-boot

@Bean
ServletRegistrationBean servletRegistrationBean() {
    ServletRegistrationBean servlet = new ServletRegistrationBean
      (new CamelHttpTransportServlet(), "camel-api/*");
    servlet.setName("CamelServlet");
    return servlet;
}

or configure using properties.