Accessing Data from JSON body inside Apache Camel

依然范特西╮ 提交于 2019-12-04 13:12:50

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!