Search code examples
javaapache-camelapache-camel-k

How to convert InputStream to Json in Camel K


I am trying to parse the response body of a http request in camel k. The response is json of the form {"weather":{...}, foo:{...},...} and I am only interested in the weather part. I tried the following approach:

// camel-k: language=java

import org.apache.camel.builder.RouteBuilder;

public class Weather extends RouteBuilder {
  @Override
  public void configure() throws Exception {

    from("timer:test?period=5000")
      .to("ahc:{{weather.uri}}")
      .log("${body.weather}");

  }
}

This leads to the following exception: org.apache.camel.component.bean.MethodNotFoundException: Method with name: weather not found on bean: java.io.ByteArrayInputStream@3584bf09 of type: java.io.ByteArrayInputStream on the exchange: Exchange[C8E32A97D83EBF3-000000000000001B]

I am assuming that I first have to convert the ByteArrayInputStream to some more convenient data format like json to then access the individual fields.

What is the right approach to do this using camel k?


Solution

  • I found it out myself.

    // camel-k: language=java
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class Weather extends RouteBuilder {
      @Override
      public void configure() throws Exception {
    
        from("timer:test?period=5000")
          .to("ahc:{{weather.uri}}")
          .unmarshal().json()
          .transform(simple("${body[weather]}"))
          .log("${body}");
    
      }
    }