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:
Javalin and SLF4J Simple dependency are as follows:
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>io.javalin</groupId> <artifactId>javalin</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.26</version> </dependency> |
To handle the response, we will use the result() method of the Context object to do this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.javalin; import io.javalin.Context; import io.javalin.Handler; import io.javalin.Javalin; public class JavalinExample { public static void main(String[] args) { Javalin javalin = Javalin.create().start(8080); javalin.get("/example", new Handler() { public void handle(Context ctx) throws Exception { ctx.result("Hello World"); } }); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.huongdanjava.javalin; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; import io.javalin.Context; import io.javalin.Handler; import io.javalin.Javalin; public class JavalinExample { public static void main(String[] args) { Javalin javalin = Javalin.create().start(8080); javalin.get("/example", new Handler() { @Override public void handle(Context ctx) throws Exception { CompletableFuture<String> f = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { return "Hello World"; } }); f.thenAccept(result -> { System.out.println(result); }); ctx.result(f); } }); } } |
Result:
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:
1 2 3 4 5 |
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> |
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava.javalin; import io.javalin.Context; import io.javalin.Handler; import io.javalin.Javalin; public class JavalinExample { public static void main(String[] args) { Javalin javalin = Javalin.create().start(8080); javalin.get("/example", new Handler() { @Override public void handle(Context ctx) throws Exception { ctx.json("Hello World"); } }); } } |
Result:
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.javalin; import org.eclipse.jetty.http.HttpStatus; import io.javalin.Context; import io.javalin.Handler; import io.javalin.Javalin; public class JavalinExample { public static void main(String[] args) { Javalin javalin = Javalin.create().start(8080); javalin.get("/example", new Handler() { @Override public void handle(Context ctx) throws Exception { ctx.json("Hello World"); ctx.status(HttpStatus.INTERNAL_SERVER_ERROR_500); } }); } } |
Result: