简体   繁体   中英

Accessing Data from JSON body inside Apache Camel

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.

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}");

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:

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]

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

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.

Thanks in advance

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(....);

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM