Working with response in Javalin

We have learned how to handle a request in Javalin, in this tutorial, we will continue to learn how to work with the response in Javalin!

I also created a Maven project as an example, as follows:

Working with response in Javalin

Javalin and SLF4J Simple dependency are as follows:

To handle the response, we will use the result() method of the Context object to do this.

This result() method also has many overload methods that can help us return String, Stream, … And in particular, it also supports asynchronous response with CompletableFuture as follows:

Result:

Working with response in Javalin

If you want to return the response as a JSON string, use the json() method of the Context.

Javalin uses the Jackson library, so in order to use the json() method, you need to declare Jackson dependency for it:

For example:

Result:

Working with response in Javalin

To set the HTTP status code for the response section, we will use the status() method with the parameter as the value of the status code you want to respond to!

Result:

Working with response in Javalin

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment