如何使用 Apache Camel 路由从文件中获取 JSON 对象并将其映射到 Java class?

How to get a JSON object from a file and map it to a Java class using Apache Camel routes?

我创建了一个扩展 RouteBuilder:

的 RequestRoute
@Component
public class RequestRoute extends RouteBuilder {

@Override
public void configure() throws Exception {
    from("file:input?noop=true").to(User.class);
}

并且在主要 class 中,我已将上面创建的路由添加到 Camel 上下文中:

  @SpringBootApplication
  public class DemoApplication {

  @SneakyThrows
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);

    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RequestRoute());
  }

}

如何从文件夹中的文件中获取 JSON object 并使用 Camel 路由将其转换为 Class?

您需要解组。

from("file:input?noop=true")
.unmarshal().json(JsonLibrary.Jackson, User.class)