在 Apache Camel 中从 Json 检索对象

Retrieve Object from Json in Apache Camel

我正在使用 Apache CamelSpring Boot 构建 servlet 服务。我有一个 REST 端点,我在其中接收 json 个对象。我想读取 json 并将其映射到我的代码中的 POJO。这是我的 RouteBuilder:

public class MyRouteBuilder extends RouteBuilder{

    @Override
    public void configure() throws Exception {

    restConfiguration()
     .component("servlet")
     .host("localhost")
     .port("8080")
     .bindingMode(RestBindingMode.auto);

    rest("/say").post("/json").type(User.class).to("direct:json");
    from("direct:json").someUsefulMethod();
    }
}

这是我的用户class

public class User {
    private String id;
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

我关注了the Apache documentation。 我想要做的是将一个 json 像 {"id":"123", "name":"elly", "age":"29"} 注入一个我的用户 class 的实例。如何从 REST 请求中获取数据?
谢谢大家。

回答后编辑: 为了完整性,这是 MyRouteBuilder class:

中的最终示例代码
    from("direct:json").process(new Processor() {
       public void process(Exchange exchange) throws Exception {
          User body = exchange.getIn().getBody(User.class);
          System.out.println("Input object: " + body.getName() + ", " + body.getAge());
          body.setAge("35");
          exchange.getIn().setBody(body);
          System.out.println("Output object: " + body.getName() + ", " + body.getAge());
       }
});

将 camel-jackson 添加到类路径中,以便它可以 json 绑定到 pojo。

查看其他一些示例: https://github.com/apache/camel-examples/tree/master/examples

您可以在 POJO 上使用 jackson 注释来微调绑定