Camel SpringBoot rest servlet connection refused (camel 2.22.0)

Viewed 815

I'm having troubles getting any of the simplest springboot servlet rest api examples to work on my machine. I'm just trying to create the simplest possible test api to practice the framework. I have the following code in my Routes RouteBuilder class:

@Component
public class Routes extends RouteBuilder {
@Override
public void configure() {
    restConfiguration()//Bind the api servlet to the localhost port 8080
        .component("servlet").host("localhost").port(8080)
        .bindingMode(RestBindingMode.auto);

    rest("/api")//Log any get requests
    .get()
    .route().to("log:DEBUG?showBody=true&showHeaders=true");

}
}

However, when I try to invoke this code with curl, I get the following error:

curl -X GET http://localhost:8080/api
curl: (7) Failed to connect to localhost port 8080: Connection refused

I'm using camel 2.22.0 and SpringBoot 2.0.4.RELEASE. I'm running this on Ubuntu 20.04 LTS.

EDIT: I did the changes suggested, but I still get the same Connection refused by curl. My code now looks like this:

restConfiguration()//Bind the api servlet to the localhost port 8080
    .component("servlet").host("localhost").port(8080)//Use camel default context path
    .bindingMode(RestBindingMode.auto);

    rest("/api")//Log any get requests
    .get()
    .route().to("log:DEBUG?showBody=true&showHeaders=true");

curl -X GET http://localhost:8080/camel/api -> Connection refused

I also now have the following in my application.yml:

server:
  port: 8080 #Specify port for camel servlet
  max-http-header-size: 32768 # Maximum size in bytes of the HTTP message header.
1 Answers

By default, Camel uses the context path /camel/*.

So, your curl command should look like this:

curl -X GET http://localhost:8080/camel/api

You can control the context path in the following ways.

With restConfiguration

restConfiguration()//Bind the api servlet to the localhost port 8080
   .component("servlet").host("localhost").port(8080)
    .contextPath("/test/*")
    .bindingMode(RestBindingMode.auto);

With application.properties

camel.component.servlet.mapping.context-path=/test/*

For me, only the latter works.

Something worth mentioning here.

You are using the servlet component in your rest definition. In this case, Camel ignores the port configuration and uses the underlying servlet component. As you are using spring-boot, the tomcat port is being used, which by default happens to be 8080.

If for some reason, you change the tomcat port, your rest service port will change.

For example, if you change the server port in the application.properties.

server.port=8180

Your rest service uses that port, ignoring the definition in the restConfiguration.

curl -X GET http://localhost:8180/camel/api

Rest DSL docs

Related