spring-boot - Camel Routes - 如何将正文响应返回为 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;
推荐阅读
- sql - 根据范围值比较 2 个表
- python - Docker:ModuleNotFoundError:没有名为“pandas.core.internals.managers”的模块;'pandas.core.internals' 不是一个包
- php - PHPPresentation 中的重复幻灯片
- python-3.x - 如何使用 python 和 PIL 检测图像的特定边框细节并根据它进行裁剪?
- python - 将函数从 R 转换为 Python
- mysql - 在 MySQL 8 中根据多个值过滤掉重复数据
- python - 如何使用 matplotlib.mlab.specgram 获取特定的二维数组?
- java - 如何通过命令行启动 selenium
- flutter - Flutter BLoC 多个 BLoC 相同的小部件
- angular - 如何使用 ionic 4 中的按钮动态添加表单组?