Apache Camel:我无法从体内取出物体并对其进行转换 [英] Apache Camel: I can't get an object out of the body and transform it
问题描述
如果是,请在这里: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对象进程,则可以将其添加为编组- This option works great, but if you do this: body -> user, it doesn't work. User always null. For clarity: Console output: If so: 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屋! .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);
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)))