从 Apache Camel 中的 JSON 正文访问数据

Accessing Data from JSON body inside Apache Camel(从 Apache Camel 中的 JSON 正文访问数据)

本文介绍了从 Apache Camel 中的 JSON 正文访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个基本上允许文件系统导航的 API.我正在尝试从 API 返回的 JSON 中访问数据,以便对其执行功能.

I am working with an API which basically allows for the navigation of a file-system. I am trying to access data from within the returned JSON by the API in order to perform a function on it.

下面是我使用访问 API 的代码.我曾尝试使用 unmarshal 来将返回的 JSON 转换为 Map.

Below is the code I am using the access the API. I have tried to use unmarshal to convert the JSON returned to a Map.

from("timer://foo?fixedRate=true&period=120000")
    .log("Checking for files")
    .setHeader("Authorization", simple(myHttp.getAuth()))
    .setHeader("CamelHttpMethod", constant("GET"))
    .to(myHttp.getFullRequest())
    .unmarshal(new JacksonDataFormat(Map.class)).log("${body}");

这会将这些数据返回给我:

Which returns this data to me:

{
    objects=[
    {
        name=file1.csv,
        type=file
    },
    {
        name=dir1,
        type=directory,
    },
    {
        name=dir2,
        type=directory
    },
    {
        name=dir3,
        type=directory
    },
    {
        name=dir4,
        type=directory
    }]
}

我想访问对象"下的数组,以检查此目录中是否存在任何文件.到目前为止,我只尝试在对象下记录数据,因此我使用了以下代码:

I want to access the array under "objects" in order to check whether any files exist inside this directory. So far, I only tried to log the data under objects and therefore I have used this code:

   .unmarshal(new JacksonDataFormat(Map.class)).log("${body.objects}");

使用 ${body.objects},我仍然无法访问 MAP 内的数据.我希望这样的东西会被退回:

Using ${body.objects}, I'm still unable to access the data inside the MAP. I expected something like this to be returned:

        [{
            name=file1.csv,
            type=file
        },
        {
            name=dir1,
            type=directory,
        },
        {
            name=dir2,
            type=directory
        },
        {
            name=dir3,
            type=directory
        },
        {
            name=dir4,
            type=directory
        }]

但是我得到了这个错误:

but instead I get this error:

方法名称:在 bean 上找不到对象:{objects=[{name=file1.csv,type=file},{name=dir1,type=directory,},{name=dir2,type=directory},{name=dir3,type=directory},{name=dir4,type=directory}]} 类型:java.util.LinkedHashMap.交换[ID-IT1289-1529914067957-0-1]

Method with name: objects not found on bean: {objects=[{name=file1.csv,type=file},{name=dir1,type=directory,},{name=dir2,type=directory},{name=dir3,type=directory},{name=dir4,type=directory}]} of type: java.util.LinkedHashMap. Exchange[ID-IT1289-1529914067957-0-1]

我是否在错误地使用 unmarshall 后访问了返回的 MAP?如果是这样,我必须使用什么正确的语法?

Am I accessing the returned MAP after using unmarshall incorrectly? What is the correct syntax I must I use if so?

我见过其他解组的例子……但我不能完全理解.我注意到许多示例使用具有 JSON 结构的类.这是必要的吗?如果我的 body 当前是 java.util.LinkedHashMap 类型,我希望访问它应该不是问题,但我似乎找不到路.

I have seen other examples of unmarshalling... but I cannot understand completely. I've noticed many examples use a class with the structure of the JSON. Is this necessary? If my body is currently of type: java.util.LinkedHashMap, I expect it shouldn't be a problem to access but I cannot seem to find the way.

提前致谢

推荐答案

最好的方法是创建一个与你的 Json 结构匹配的类.

The best way to do this is to create a class matching your Json Structure.

class RestResponse {
   List<FileNameType> objects;
   //Getters and Setters
}

class FileNameType {
  String name;
  String type;
  //Getters and setters.
}

然后像这样改变你的路线

Then change your route like this

from("timer://foo?fixedRate=true&period=120000")
    .log("Checking for files")
    .setHeader("Authorization", simple(myHttp.getAuth()))
    .setHeader("CamelHttpMethod", constant("GET"))
    .to(myHttp.getFullRequest())
    .unmarshal().json(JsonLibrary.Jackson, RestResponse.class)
    .to(....);

最后一部分我留空了,在这里你可以添加一个处理器来验证你的逻辑.您可以从 exchange.getIn().getBody() 中获取 RestResponse 对象.像这样的事情会做

The last part I have left it blank, here you can add a processor to verify your logic. You can get the RestResponse object from exchange.getIn().getBody(). Some thing like this will do

........
.unmarshal().json(JsonLibrary.Jackson, RestResponse.class)
.process(exchange -> {

              RestResponse response = exchange.getIn().getBody(RestResponse.class);
               // Do your logic here.

  })

您可能需要添加此依赖项

You might need to add this dependency

<dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-jackson</artifactId>
       <version>yourcamelversion</version>
 </dependency>

这篇关于从 Apache Camel 中的 JSON 正文访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 Apache Camel 中的 JSON 正文访问数据