[英]Camel: Unmarshaling json array to pojo object
I'm working with camel-spring, I have Json array as a response from an api as below:我正在使用骆驼弹簧,我有 Json 数组作为来自 api 的响应,如下所示:
[{"userName":"name","email":"email"}]
my object like this:我的 object 像这样:
public class Response{
private String userName;
private String email;
setters & getters
}
my route builder:我的路线建设者:
from("seda:rout")
.routeId("distinaation")
.unmarshal()
.json(JsonLibrary.Jackson, Request.class)
.bean(Bean.class, "processRequest")
.to(destination)
.unmarshal()
.json(JsonLibrary.Jackson, Response.class)
.bean(Bean.class, "processResponse")
error:错误:
Can not desserialize instance of com.liena.Response out of START_ARRAY token
is there any way to unmarshall json array directly to my obj??有没有办法将 json 数组直接解组到我的 obj 中?
I solved the problem by using object mapper in my processor.我通过在我的处理器中使用 object 映射器解决了这个问题。
my route builder:我的路线建设者:
from("seda:rout")
.routeId("distinaation")
.unmarshal()
.json(JsonLibrary.Jackson, Request.class)
.bean(Bean.class, "processRequest")
.to(destination)
.bean(Bean.class, "processResponse")
processor:处理器:
public Response processResponse(Exchange exchange) throws IOException {
String responseStr = exchange.getIn().getBody().toString();
ObjectMapper mapper = new ObjectMapper();
List<Response> list = mapper.readValue(responseStr, new TypeReference<List<Response>>({});
Response response = list.get(0);
...}
Another short way to do this will be: in route builder:另一个简短的方法是:在路由构建器中:
from("seda:rout")
.routeId("distinaation")
.unmarshal()
.json(JsonLibrary.Jackson, Request.class)
.bean(Bean.class, "processRequest")
.to(destination)
.unmarshal(new ListJacksonDataFormat(Response.class))
.bean(Bean.class, "processResponse")
processor:处理器:
public Response processResponse(List<Response> list {
Response response = list.get(0);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.