簡體   English   中英

Camel SpringBoot rest servlet連接被拒絕(camel 2.22.0)

[英]Camel SpringBoot rest servlet connection refused (camel 2.22.0)

我無法讓任何最簡單的 springboot servlet rest api 示例在我的機器上工作。 我只是想創建最簡單的測試 api 來練習框架。 我的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");

}
}

但是,當我嘗試使用 curl 調用此代碼時,出現以下錯誤:

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

我正在使用駱駝 2.22.0 和 SpringBoot 2.0.4.RELEASE。 我在 Ubuntu 20.04 LTS 上運行它。

編輯:我做了建議的更改,但我仍然得到 curl Connection refused 我的代碼現在看起來像這樣:

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

我的 application.yml 現在也有以下內容:

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

默認情況下,Camel 使用上下文路徑/camel/*

因此,您的curl命令應如下所示:

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

您可以通過以下方式控制上下文路徑。

restConfiguration

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

使用application.properties

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

對我來說,只有后者有效。

這里有一點值得一提。

您正在 rest 定義中使用servlet組件。 在這種情況下,Camel 會忽略port配置並使用底層 servlet 組件。 當您使用 spring-boot 時,正在使用 tomcat 端口,默認情況下恰好是 8080。

如果由於某種原因,您更改了 tomcat 端口,您的 rest 服務端口將發生變化。

例如,如果您更改application.properties中的服務器端口。

server.port=8180

您的 rest 服務使用該端口,忽略restConfiguration中的定義。

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

Rest DSL文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM