我在 Spring Boot 專案中學習 Apache Camel,我嘗試創建一個 Retful Webservice 并且該服務正在啟動,但問題是我在呼叫端點時得到 404。
@Component
@RequiredArgsConstructor
public class RestJavaDsl extends RouteBuilder {
private final WeatherDataProvider weatherDataProvider;
@Override
public void configure() throws Exception {
from("rest:get:javadsl/weather/{city}?produces=application/json")
.outputType(WeatherDto.class)
.process(this::getWeatherData);
}
private void getWeatherData(Exchange exchange) {
String city = exchange.getMessage().getHeader("city", String.class);
WeatherDto currentWeather = weatherDataProvider.getCurrentWeather(city);
Message message = new DefaultMessage(exchange.getContext());
message.setBody(currentWeather);
exchange.setMessage(message);
}
}
我創建了這個類來硬編碼一些資料:
@Component
public class WeatherDataProvider {
private static Map<String, WeatherDto> weatherData = new HashMap<>();
public WeatherDataProvider() {
WeatherDto dto = WeatherDto.builder().city("London").temp("10").unit("C").receivedTime(new Date().toString()).id(1).build();
weatherData.put("LONDON", dto);
}
public WeatherDto getCurrentWeather(String city) {
return weatherData.get(city.toUpperCase());
}
public void setCurrentWeather(WeatherDto dto) {
dto.setReceivedTime(new Date().toString());
weatherData.put(dto.getCity().toUpperCase(), dto);
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class WeatherDto implements Serializable {
static int counter = 1;
private int id = counter ;
private String city;
private String temp;
private String unit;
private String receivedTime;
}
應用程式.yml
camel:
component:
servlet:
mapping:
context-path: /services/*
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dgs</groupId>
<artifactId>camel-rest-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>camel-rest-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<camel.version>3.14.0</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服務正在啟動,這是控制臺日志:
2022-01-24 20:01:40.353 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : Starting CamelRestSpringbootApplication using Java 11.0.5 on pc-PC with PID 15796 (D:\Spring Boot\camel-rest-springboot\target\classes started by pc in D:\Spring Boot\camel-rest-springboot)
2022-01-24 20:01:40.357 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : No active profile set, falling back to default profiles: default
2022-01-24 20:01:43.583 INFO 15796 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-01-24 20:01:43.604 INFO 15796 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-01-24 20:01:43.604 INFO 15796 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-01-24 20:01:43.820 INFO 15796 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-01-24 20:01:43.821 INFO 15796 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3235 ms
2022-01-24 20:01:45.228 INFO 15796 --- [ main] o.a.c.c.s.CamelHttpTransportServlet : Initialized CamelHttpTransportServlet[name=CamelServlet, contextPath=]
2022-01-24 20:01:45.233 INFO 15796 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-01-24 20:01:45.592 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Message DataType is enabled on CamelContext: camel-1
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Routes startup (total:1 started:1)
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Started route1 (rest://get:javadsl/weather/{city%7D)
2022-01-24 20:01:45.607 INFO 15796 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.14.0 (camel-1) started in 370ms (build:83ms init:269ms start:18ms)
2022-01-24 20:01:45.617 INFO 15796 --- [ main] c.d.c.CamelRestSpringbootApplication : Started CamelRestSpringbootApplication in 6.569 seconds (JVM running for 8.087)
但是當我嘗試呼叫端點 http://localhost:8080/services/javadsl/weather/london
看起來這個端點不存在,其余的沒有創建。我使用了除錯器并且沒有呼叫 getWeatherData() 方法。而且我認為這個日志不好:Started route1 (rest://get:javadsl/weather/{city}),它應該是這樣的: route1 從 servlet:/javadsl/weather/{city% 啟動和消費7D 教程來自這里:https ://www.youtube.com/watch?v=spDjbC8mZf0&t= 433s 提前謝謝你!
uj5u.com熱心網友回復:
您可以從使用官方camel-archetype-spring-boot maven 原型創建示例專案開始。您可以使用 IDE 或使用以下 maven 命令從命令列生成基于原型的專案。
mvn archetype:generate -DarchetypeArtifactId="camel-archetype-spring-boot" -DarchetypeGroupId="org.apache.camel.archetypes" -DarchetypeVersion="3.14.0"
現在在 maven 專案檔案pom.xml中將以下塊添加到依賴項
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jetty-starter</artifactId>
</dependency>
請注意,不需要指定版本,因為它由camel-spring-boot-dependenciesBOM(物料清單)在dependencyManagement 部分提供。
之后,您可以創建新檔案 ConfigureCamelContext.java,我們可以在其中配置 CamelContext 并為其提供 RestConfiguration。我們可以通過實作CamelContextConfiguration和@Component注解來做到這一點。
注釋告訴 spring-framework 它應該創建類的實體并將其注入到基于其型別別或介面請求它的物件中。Camel 被配置為自動向 spring-framework 詢問這些 RestConfiguration 實體,它將繼續使用配置 CamelContext。
package com.example;
import org.apache.camel.CamelContext;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spring.boot.CamelContextConfiguration;
import org.springframework.stereotype.Component;
@Component
public class ConfigureCamelContext implements CamelContextConfiguration {
@Override
public void beforeApplicationStart(CamelContext camelContext) {
RestConfiguration restConfiguration = new RestConfiguration();
restConfiguration.setApiComponent("jetty");
restConfiguration.setApiHost("localhost");
restConfiguration.setPort(8081);
camelContext.setRestConfiguration(restConfiguration);
}
@Override
public void afterApplicationStart(CamelContext camelContext) {
}
}
這應該適用于RestDSL和rest-component。
將 MySpringBootRouter.java 編輯為如下內容:
package com.example;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class MySpringBootRouter extends RouteBuilder {
static final String CONTET_TYPE_TEXT = "text/plain";
@Override
public void configure() {
rest()
.get("/hello/")
.route()
.setHeader(Exchange.CONTENT_TYPE, constant(CONTET_TYPE_TEXT))
.setBody().constant("Hello world")
.end()
.endRest()
.get("/hello/{name}")
.route()
.setHeader(Exchange.CONTENT_TYPE, constant(CONTET_TYPE_TEXT))
.setBody().simple("Hello ${headers.name}")
.end()
.endRest();
from("rest:get:test?produces=plain/text")
.setBody().constant("Hello from JavaDSL");
}
}
注釋掉下面的類,src/test/java/<grouId>/因為原型提供的示例測驗不適用于 MySpringBootRouter 并將中斷構建。
要運行專案,您可以使用命令
mvn spring-boot:run
現在您應該可以打開localhost:8081/hello,localhost:8081/hello/<Name>或localhost:8081/test以純文本形式獲得回應。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/420699.html
標籤:
上一篇:AzureSynapse-使用OPENROWSET查詢JSON-FIELDTERMINATOR值0x0b
下一篇:如何進行API呼叫。我是初學者
