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
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With