'Apache Camel not returning http response after JSLT Transformation

I am trying to get a http response out of the JSLT Transformer within Apache Camel.

Like in the given code sample

rest("/transform")
.post("/start")
.route()
.to("jslt:transform.jslt").transform().body

I want to return the body after the transformation.

But I always get an exception like

2022/03/23 14:51:09,842 [ERROR] [CamelHttpTransportServlet] - Error processing request
java.lang.IllegalStateException: getWriter() has already been called for this response

After adding a logger I can confirm that the jslt transformation into the body is working fine.

Plain responses like

rest("/say")
.get("/hello").route().transform().constant("Hello World");

work. I think it has to do with the implementation of the JSLT Transform.



Solution 1:[1]

It depends on your actual use case but assuming that you post a json payload to your endpoint then what you want to achieve can be done as next assuming that you use camel-undertow:

// configure rest-dsl
restConfiguration()
    // to use undertow component and run on port 8080
    .component("undertow").port(8080);
// The endpoint /transform/start calls the route `direct:jslt`
rest("/transform")
    .post("/start").consumes("application/json").to("direct:jslt");
from("direct:jslt")
    // Convert the payload to a String as only String or InputStream 
    // are supported by the component jslt
    .convertBodyTo(String.class)
    // Transform the body using the template transform.jslt available from 
    // the classpath
    .to("jslt:transform.jslt");

The result is directly set in the body of your message so it is what the rest endpoint will return back to the client in its body.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1