在 apache camel 中将 json 转换为其他格式
converting json to other format in apache camel
我是 apache 骆驼的新手。我能够从 camel 发送获取请求以休息 api。我已经使用了spring boot camel 微服务。现在如何将输出格式转换为任何其他格式并显示它?
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}");
}
}
结果如下所示:
"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"
rest component returns classic java inputstream 首先你必须将 inputstream 转换为 dto 然后你可以转换任何类型在这个例子中它是 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;}
我是 apache 骆驼的新手。我能够从 camel 发送获取请求以休息 api。我已经使用了spring boot camel 微服务。现在如何将输出格式转换为任何其他格式并显示它?
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}");
}
}
结果如下所示:
"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"
rest component returns classic java inputstream 首先你必须将 inputstream 转换为 dto 然后你可以转换任何类型在这个例子中它是 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;}