1

我关注 Apache 骆驼休息服务(/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");;
        ;

    }

}

在此处输入图像描述

4

3 回答 3

0

您应该删除“direct:bye”路线上的最后一个 .endRest()。我认为您在致电处理器之前会得到其余的响应。

于 2020-05-13T08:44:27.673 回答
0

这对我有用。

首先,我需要bindingModeRestBindingMode.json.restConfiguration

其次,代替marshal(),你需要使用unmarshal()

第三,由于您要返回订单列表,.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");
    }
于 2020-05-13T12:00:50.303 回答
0
Solvedddd !!! i did two things as follows,May be use full for some one

1、bindingMode(RestBindingMode.auto) - RestBindingMode 从 json 更改为 auto
2、在主服务中添加了这个(/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}");
        ;
        ;

    }

}
于 2020-05-13T12:24:50.113 回答