Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

java - Camel SpringBoot rest servlet connection refused (camel 2.22.0)

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.
question from:https://stackoverflow.com/questions/65870125/camel-springboot-rest-servlet-connection-refused-camel-2-22-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...