Apache Camel: I can#39;t get an object out of the body and transform it(Apache Camel:我无法从身体中取出物体并对其进行改造)
问题描述
如果这样做的话,这里是:body ->字符串->用户
.to("direct:httpClient").process(新处理器(){@覆盖公共无效进程(交换交换)抛出 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, 常量("application/json")).convertBodyTo(用户[].class).to("http://localhost:8085/api/user").process(exchange -> System.out.println(exchange.getIn().getBody(String.class)))
控制台输出:
<代码>[{名称":BLA";},{名称":BLA";},{名称":BLA";}]
如果是这样:
from("timer://test?period=2000").setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET).setHeader(Exchange.CONTENT_TYPE, 常量("application/json")).convertBodyTo(用户[].class).to("http://localhost:8085/api/user").process(exchange -> System.out.println(exchange.getIn().getBody(User[].class)))
控制台输出:null
是什么原因?我该如何解决这个问题?
如果你想让 Camel 处理 JSON 字符串到 Java 对象的过程,你可以将它添加为 marshaller - https://camel.apache.org/components/latest/dataformats/jacksonxml-dataformat.htmlp>
Here is if do so: body -> string -> user
.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:我无法从身体中取出物体并对其进行改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Apache Camel:我无法从身体中取出物体并对其进行改造
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01