【问题标题】:Camel binding mode for JSON not working, cant unmarshal it to POJOJSON的骆驼绑定模式不起作用,无法将其解组为POJO
【发布时间】:2022-02-18 23:30:13
【问题描述】:

我正在尝试将消息从 REST API 发送到新路由,即使您在我的 REST API 上收到 JSON 格式的请求并且绑定设置为 JSON,当我将其转发到新路由时,它也会显示为 InputStream我必须将它编组为 JSON 才能使用它。

我已经尝试在 RestConfiguration 中使用 streamCaching 和其他组件(consumes、produces、type、dataType)。我也在使用 POM 中的所有依赖项。

public void configure() {
    restConfiguration().component("servlet")
            .bindingMode(RestBindingMode.json)
            .skipBindingOnErrorCode(false);

    rest("/resttest")
       .patch("/t1")
            .id("t1")
            .description("t1")
            .consumes("application/json")
            .produces("application/json")
            .param()
                .name("body")
               .type(RestParamType.body)
               .dataType("json")
                .required(true)
            .endParam()
            .to("direct:test2");

这条路线在其他类别中:

 from("direct:test2").id("test2")

            .marshal().json(JsonLibrary.Jackson,SomePOJO.class)
            .unmarshal().json(JsonLibrary.Jackson, SomePOJO.class)
            .choice()
            .when(simple("${body.getStatus()} =~ 'Closed'"))
            .....

我期待在 test2 路由上得到 JSON 消息,但不知何故我得到了 InputStream,所以我必须先进行编组。任何人都知道如何让 REST API 将我转发到 JSON 格式的路由消息,而不是流?

【问题讨论】:

    标签: json rest apache-camel spring-camel


    【解决方案1】:

    试试:

    .convertBodyTo(String.class)
    

    在你的解组之前。

    【讨论】:

    • 不确定这有什么帮助?但我试过了,它不起作用。我知道问题出在 REST API 中,因为它没有将收到的消息绑定到 JSON,因此 $body 中的消息是 InputStream 格式。
    【解决方案2】:

    我遇到了同样的问题,我必须对传入值应用 marshal 方法,对响应应用 unmarshal 方法。

    这是邮递员的回复:

    我使用的是这段代码:

    rest(service.service)
    .description(ContentCategory.api("Service Test"))           
    .post("/product/{productType}/{enterpriseId}")
    .description("service for saving a product")
    .consumes(MediaType.APPLICATION_JSON)
    .type(Product.class)
    .bindingMode(RestBindingMode.json)
    .produces(MediaType.APPLICATION_JSON)
    .outType(String.class)
    .route()
    .bean("productService", "saveProduct")
    .marshal()
    .json(JsonLibrary.Jackson)
    .setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_JSON))
    .endRest();
    

    在意识到我正在接收 InputStream 后,我刚刚添加了 unmarshall 方法

    rest(service.service)
    .description(ContentCategory.api("Service Test"))           
    .post("/product/{productType}/{enterpriseId}")
    .description("service for saving a product")
    .consumes(MediaType.APPLICATION_JSON)
    .type(Product.class)
    .bindingMode(RestBindingMode.json)
    .produces(MediaType.APPLICATION_JSON)
    .outType(String.class)
    .route()
    .bean("productService", "saveProduct")
    .marshal()
    .json(JsonLibrary.Jackson)
    .unmarshal()
    .json(JsonLibrary.Jackson)
    .setHeader(Exchange.CONTENT_TYPE, constant(MediaType.APPLICATION_JSON))
    .endRest();
    

    其实我只是加了这两行

    .unmarshal()
    .json(JsonLibrary.Jackson)
    

    它开始工作:

    它认为您可能需要对 resttest 服务使用 unmarshal 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      相关资源
      最近更新 更多