Doug Saus
Doug Saus

Reputation: 49

Use existing http server in spring boot as camel endpoint

I have a spring boot application that uses the spring boot starter web. This creates a running Tomcat instance and sets up the http server running on a port. Within my camel route, I want to use this http server as the component for http requests, but I can't figure out how to utilize it. I see many many examples of configuring a jetty instance and consuming from it, but then wouldn't I in effect have two http servers running? I only want to have one. I assume the http server is already autowired up since I can consume from it with other spring code (such as a RestController) and I can see it started in my spring boot logs as well.

@Component
public class ExampleRoute extends RouteBuilder
{
    @Override
    public void configure() throws Exception
    {

        //@formatter:off

        from( <want to take in an http request here> )
            .log( LoggingLevel.INFO, log, "Hello World!" );

        //@formatter:on

    }
}

Upvotes: 1

Views: 4430

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55545

There is an example here: https://github.com/camelinaction/camelinaction2/tree/master/chapter7/springboot-camel

You can to register a ServletRegistrationBean that setup the Camel Servlet with Spring Boot.

@Bean
ServletRegistrationBean camelServlet() {
    // use a @Bean to register the Camel servlet which we need to do
    // because we want to use the camel-servlet component for the Camel REST service
    ServletRegistrationBean mapping = new ServletRegistrationBean();
    mapping.setName("CamelServlet");
    mapping.setLoadOnStartup(1);
    // CamelHttpTransportServlet is the name of the Camel servlet to use
    mapping.setServlet(new CamelHttpTransportServlet());
    mapping.addUrlMappings("/camel/*");
    return mapping;
}

However for Camel 2.19 we plan on make this simpler and OOTB: https://issues.apache.org/jira/browse/CAMEL-10416

And then you can do

from("servlet:foo")
  .to("bean:foo");

Where the HTTP url to call that Camel route will be http:localhost:8080/camel/foo

Upvotes: 2

Related Questions