情况如下:
当使用推文作为正文调用对spring boot rout'/ tweet'的发布请求时,将启动一个路由:
@RequestMapping(value="/tweet", method = RequestMethod.POST)
@ResponseBody
public void storeTweet(@RequestBody TwitterBean twitterBean) {
System.out.println("camel: " + twitterBean.getText());
producerTemplate.sendBody("direct:storeTweet", twitterBean);
}
这有效,我可以在路线的日志中访问正文:
@Component
public class CamelRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:firstRoute")
.log("Camel body: ${body}");
from("direct:storeTweet")
.log("Store tweet: $(body)");
}
}
我真正想做的是将该主体发送到其他API,就像我在此代码段中所做的那样:
public void executePostWithCamel(TwitterBean tweet) throws Exception {
try {
camelContext.addRoutes(new RoutesBuilder() {
@Override
public void configure() {
from("timer://runOnce?repeatCount=1")
.id("tweetToDB")
.process(exchange -> exchange.getIn().setBody(tweet))
.marshal().json(JsonLibrary.Jackson)
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http4://localhost:8085/twitterAPI/tweet")
.process(exchange -> log.info("The response code is: {}", exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE)));
}
});
camelContext.start();
} finally {
//camelContext.stop();
}
}
但是这次我似乎无法弄清楚如何访问我的推文。
修改
我用以下路线解决了我的问题:
from("direct:storeTweet")
.process(exchange -> exchange.getIn().setBody(exchange.getIn().getBody()))
.marshal().json(JsonLibrary.Jackson)
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http4://ptsv2.com/t/5f02k-1519979843/post")
.log("Store tweet: ${body}");
谢谢!