从Apache Camel内的JSON主体访问数据 [英] Accessing Data from JSON body inside Apache Camel
问题描述
我正在使用一个API,该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的代码.我试图用元帅来 将返回的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}");
哪个数据返回给我:
{
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
}]
但我收到此错误:
名称为:在bean上找不到对象的方法:{objects = [{name = file1.csv,type = file},{name = dir1,type = directory,},{name = dir2,type = directory},类型为:java.util.LinkedHashMap的{name = dir3,type = directory},{name = dir4,type = directory}]}.交换[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]
我使用了不正确的编组后是否正在访问返回的MAP?如果是这样,我必须使用什么正确的语法?
Am I accessing the returned MAP after using unmarshall incorrectly? What is the correct syntax I must I use if so?
我看过其他解组邮件的例子...但是我无法完全理解.我注意到许多示例使用具有JSON结构的类.这有必要吗?如果我的身体当前是类型: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 Structure匹配的类.
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主体访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!