krish
krish

Reputation: 3

converting json to other format in apache camel

I am new to apache camel. I was able to send a get request to rest api from camel. I have used spring boot camel microservice .now how can I convert the output format to any other format and display it?

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class RestAPIClientRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        restConfiguration().host("https://jsonplaceholder.typicode.com").port(80);
        from("timer:foo?repeatCount=1").
        to("rest:get:/posts/1")
            .log("${body}");
    }
}

Result looks like this:

"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et 
cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet 
architecto"

Upvotes: 0

Views: 236

Answers (1)

erayerdem
erayerdem

Reputation: 836

rest component returns classic java inputstream firstly u must convert inputstream to a dto after that u can convert any type in this example it is xml

   restConfiguration().host("https://jsonplaceholder.typicode.com").port(80);
from("timer:foo?repeatCount=1").
to("rest:get:/posts/1")
    .unmarshal(new JacksonDataFormat(YourDto.class))
    .unmarshal().jacksonxml().log("${body}")


public class YourDto{
public int userId;
public int id;
public String title;
public String body;}

Upvotes: 0

Related Questions