Apache 骆驼动态路由

Apache camel dynamic routing

我有以下 Apache camel 休息服务 (/sales),它在内部调用另一个休息服务 (/getOrders) 并获取对象列表。能够在处理器中打印 JSON 响应,但在尝试从邮递员那里获得 java 对象作为响应。谁能帮我解决这个问题。附加参考的响应日志..

@Component
public class ApplicationResource extends RouteBuilder {

    @Autowired
    private OrderService service;

    @BeanInject
    private OrderProcessor processor;

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").port(9090).host("localhost");

        rest().get("/getOrders").produces(MediaType.APPLICATION_JSON_VALUE).route().setBody(() -> service.getOrders());


        rest().get("/sales").produces(MediaType.APPLICATION_JSON_VALUE).route()
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .toD("http://localhost:9090/getOrders?bridgeEndpoint=true").convertBodyTo(String.class).marshal()
                .json(JsonLibrary.Jackson, Order.class).to("log:foo?showHeaders=true");;
        ;

    }

}

您应该删除 "direct:bye" 路线上的最后一个 .endRest() 。 我想你在调用你的处理器之前得到了其余的响应。

这对我有用。

首先,我需要在restConfiguration.

中将bindingMode设置为RestBindingMode.json

其次,而不是marshal(),你需要使用unmarshal().

Third,因为您返回的是订单列表,.json(JsonLibrary.Jackson, Order.class) 不足以解组订单列表。您需要使用能够将订单列表解组为 json 数组的自定义格式。这需要使用 JacksonDataFormat format = new ListJacksonDataFormat(Order.class);

    @Override
    public void configure() {

        JacksonDataFormat format = new ListJacksonDataFormat(Order.class);
        restConfiguration().component("servlet").port(9090).host(localhost).bindingMode(RestBindingMode.json);

        rest()
            .get("/getOrders")
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .route()
                .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getMessage().setBody(service.getOrders());
                }})
            .to("log:getOrders?showHeaders=true&showBody=true");

        rest()
            .get("/sales")
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .route()
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .toD("http://localhost:9090/getOrders?bridgeEndpoint=true")
                .unmarshal(format)
                .to("log:sales?showHeaders=true&showBody=true");
    }
Solvedddd !!! i did two things as follows,May be use full for some one

1,bindingMode(RestBindingMode.auto) - RestBindingMode 更改为自动 来自 json
2,在main中添加了这个 服务(/getOrders).marshal().json(JsonLibrary.Jackson);

@Component
public class ApplicationResource extends RouteBuilder {

    @Autowired
    private OrderService service;

    @BeanInject
    private OrderProcessor processor;

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").port(9090).host("localhost").bindingMode(RestBindingMode.auto);

        rest().get("/getOrders").produces(MediaType.APPLICATION_JSON_VALUE).route().setBody(() -> service.getOrders())
                .marshal().json(JsonLibrary.Jackson);

        rest().get("/sales").produces(MediaType.APPLICATION_JSON_VALUE).route()
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .toD("http://localhost:9090/getOrders?bridgeEndpoint=true").convertBodyTo(String.class)
                .log("body = ${body}");
        ;
        ;

    }

}