Apache Camel:我无法从体内取出物体并对其进行转换 [英] Apache Camel: I can't get an object out of the body and transform it

查看:64
本文介绍了Apache Camel:我无法从体内取出物体并对其进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是,请在这里:body->字符串->用户

     .to("direct:httpClient")
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws JsonProcessingException {
            String body = exchange.getIn().getBody(String.class);
            User[] users = jacksonDataFormat.getObjectMapper().readValue(body, User[].class);
        }
    })
 

此选项很好用,但是如果您这样做:

 User [] body = exchange.getIn().getBody(User[].class);
 

body->用户,它不起作用.用户始终为空.

为清楚起见:

from("timer://test?period=2000")
                .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .convertBodyTo(User[].class)
                .to("http://localhost:8085/api/user")
                .process(exchange -> System.out.println(exchange.getIn().getBody(String.class)))

控制台输出:

[
   {
      "name":"BLA"
   },
   {
      "name":"BLA"
   },
   {
      "name":"BLA"
   }
]

如果是这样:

from("timer://test?period=2000")
                .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .convertBodyTo(User[].class)
                .to("http://localhost:8085/api/user")
                .process(exchange -> System.out.println(exchange.getIn().getBody(User[].class)))

控制台输出:null

原因是什么?我该如何解决?

解决方案

如果希望Camel将JSON字符串处理为Java对象进程,则可以将其添加为编组- .to("direct:httpClient") .process(new Processor() { @Override public void process(Exchange exchange) throws JsonProcessingException { String body = exchange.getIn().getBody(String.class); User[] users = jacksonDataFormat.getObjectMapper().readValue(body, User[].class); } })

This option works great, but if you do this:

User [] body = exchange.getIn().getBody(User[].class);

body -> user, it doesn't work. User always null.

For clarity:

from("timer://test?period=2000")
                .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .convertBodyTo(User[].class)
                .to("http://localhost:8085/api/user")
                .process(exchange -> System.out.println(exchange.getIn().getBody(String.class)))

Console output:

[
   {
      "name":"BLA"
   },
   {
      "name":"BLA"
   },
   {
      "name":"BLA"
   }
]

If so:

from("timer://test?period=2000")
                .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .convertBodyTo(User[].class)
                .to("http://localhost:8085/api/user")
                .process(exchange -> System.out.println(exchange.getIn().getBody(User[].class)))

Console output: null

What is the reason? how can I fix this?

解决方案

If you want Camel to handle the JSON string to Java object process, you can add it as a marshaller - https://camel.apache.org/components/latest/dataformats/jacksonxml-dataformat.html

这篇关于Apache Camel:我无法从体内取出物体并对其进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆