简体   繁体   English

如何在Camel Restlet中设置请求正文?

[英]How to set request body in Camel Restlet?

I have a simple Camel get method and all I want to do is set the body to the result of a XSL transformation. 我有一个简单的Camel get方法,我要做的就是将主体设置为XSL转换的结果。 How do I do this? 我该怎么做呢? The following code does not compile, but it shows what I want to achieve: 以下代码无法编译,但是显示了我想要实现的目标:

rest("/api")
    .get("/booksByAuthor/{author}")
    .route()
    .setBody(
        from("file:/conf.xml")
        .setHeader("author",simple("${header.author}"))
        .to("xslt:/transformers/booksByAuthor.xsl")
    );

You could use a processor to set the body to be the xml file, then pass it to your xslt. 您可以使用处理器将主体设置为xml文件,然后将其传递给xslt。 You don't need to have the file contents in the message body, just a handle to the file is enough for "xslt:". 您不需要在消息正文中包含文件内容,只需文件的句柄就足以用于“ xslt:”。 Something like 就像是

    rest("/api")
        .get("/booksByAuthor/{author}")
        .route()
        .process(exchange -> exchange.getIn().setBody(new File("/conf.xml")))
        .to("xslt:/transformers/booksByAuthor.xsl");

The author will already be in the message header, so you won't need to set it and you'll be able to access it in your xslt with 作者已经在消息标题中,因此您无需进行设置,就可以在xslt中使用

    <xsl:param name="author"/>
    <xsl:value-of select="$author"/>

I've just written the processor as a Java 8 lambda, but you could always use a separate class if you prefer. 我刚刚将处理器编写为Java 8 lambda,但是如果愿意,可以始终使用单独的类。

If you want to get the source of your xml file into the message, rather than use the file handle, you could use the pollEnrich to read the file. 如果要将XML文件的源获取到消息中,而不是使用文件句柄,则可以使用pollEnrich读取文件。 You'll then need to use an aggregation strategy to ensure you keep the headers from the original message. 然后,您需要使用一种聚合策略来确保您保留原始消息的标头。 Easiest way is probably just copy the body from the message with the xml to the original. 最简单的方法可能只是将带有xml的邮件正文复制到原始邮件。 Here's an example of how to do this. 这是如何执行此操作的示例。

    rest("/api")
        .get("/booksByAuthor/{author}")
        .route()
        .pollEnrich("file:/?fileName=conf.xml&noop=true", (original, xml) -> {
                original.getIn().setBody(xml.getIn().getBody());
                return original;})
        .to("xslt:/transformers/booksByAuthor.xsl");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM