Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 659
malteser Avatar asked Jun 25 '18 08:06

malteser


People also ask

How do you log the body in Apache Camel?

Logging message body with streaming The log DSL have overloaded methods to set the logging level and/or name as well. from("direct:start") . log(LoggingLevel. DEBUG, "com.

How do I read a JSON file in REST API?

To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.

What is marshal and Unmarshal in Apache Camel?

Marshal - Transforms the message body (such as Java object) into a binary or textual format, ready to be wired over the network. Unmarshal - Transforms data in some binary or textual format (such as received over the network) into a Java object; or some other representation according to the data format being used.


1 Answers

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>
like image 192
pvpkiran Avatar answered Oct 25 '22 16:10

pvpkiran