Search code examples
spark-java

What's the difference between response.body() and object returned by the callback?


From Spark Java documentation:

response.body("Hello");        // sets content to Hello

And from Route's JavaDoc:

@return The content to be set in the response

So what's the difference? Could someone explain to me pls?


Solution

  • An actual difference however is that response.body() accepts only String, while in the callback you can return any object that can be serialized to String and most importantly streams.

    response.body() should be mostly used in exception handlers and after filters, and the callback return in normal routes.