CamelRoutes-如何将主体响应返回为xml



首先,我是Spring Boot的新手。我不确定这是否可能,但我想从外部url返回xml响应。

我有这个代码:

@GetMapping("/myPage")
public void myPage() {
restConfiguration().host("localhost").port(8080);
from("timer://runOnce?repeatCount=1&delay=0")
.to("rest:get:/external-page")
.to("stream:out");
}

myPage((是返回一个XML(没关系(。所以,现在我想在返回时返回相同的XML:

curl http://localhost/myPage

我不确定是否必须使用.to("stream:out"(,但curl返回了一个空结果。

有人能帮我吗?提前谢谢。

我找到了解决方案,这就是如何获得响应。

CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
restConfiguration().host(sHost).port(iPort);
from("direct:start")
.setHeader(Exchange.HTTP_METHOD,simple("GET"))
.to("rest:get:/external-page");
}
});
context.start();
ProducerTemplate template = context.createProducerTemplate();
String headerValue = "application/xml";
Map<String, Object> headers = new HashMap<String,Object>();
headers.put("Content-Type", headerValue);
Object result = template.requestBodyAndHeaders("direct:start", null, headers, String.class);
Exchange exchange = new DefaultExchange(context);
String response = ExchangeHelper.convertToType(exchange, String.class, result);
context.stop();
return response;

最新更新