Handle request in Javalin

I introduced you to Javalin, in this tutorial we will learn more about how we will handle requests in the Javalin framework!

I also created a Maven project as an example:

Handle request in Javalin

Javalin and SLF4J Simple dependency are as follows:

I also created a class with the main() method that initializes a web server with the Javalin object as follows:

As I said, to handle request in Javalin, we will use its Functional Interface Handler. The Handler class only has a handle() method with a parameter of a Context object, and this Context object will keep all the request and response information for a request.

I implement the Handler object for GET request “/example” as follows:

We will use queryParam() method to get information about request parameters from the user, for example:

Result:

Handle request in Javalin

One of the queryParam() overload method also allows us to validate the data we pass on. The parameters of this method include the key as request parameter and the class is the data type of that parameter’s value. For example, if I now edit the above code:

then when you request the URL http://localhost:8080/example?id=Khanh, you will see the following error:

Handle request in Javalin

We can define the request with the path parameter as follows:

In the above code, I have defined a path parameter name, to get this value, we will use pathParam() method of the Context object.

Handle request in Javalin

Similar to the queryParam() method, we can also validate the user’s request data using pathParam() overload method.

If your request has a body part, you can use the body() method or bodyAsClass() method to get data in this body section. Examples are as follows:

Result:

Handle request in Javalin

With the body part, we can validate data using the bodyValidator() method!

After validating with the queryParam(), pathParam() or bodyValidator() methods, you can also add more conditions to check the data sent by the user using the check() method of TypedValidator object as follows:

Result:

Handle request in Javalin

Usually, when implementing RESTful Web Service applications, we often implement CRUD APIs  (Create, Read, Update, Delete) and these APIs often start with the same context path, “/user” for example.

For simplicity and increased readable capability, Javalin supports groups handler so that we can group to handle the requests as follows:

To use the above code, you will need to implement the Handler interface for the post(), get(), delete(), patch() methods in another class and in addition you need to import these static methods from the class io.javalin.apibuilder.ApiBuilder!

One more thing of Javalin in the handling request that you need to know is that we can use Javalin before() and after() methods to add processing before handle request and before return response for any request or all requests.

For example, if I want before handling any requests for the above example, the application will print the words “Start processing …” then I will fix the code again as follows:

Result:

Handle request in Javalin

Add Comment