Contents
Javalin
A small and simple web framework: routes, handlers, JSON and REST APIs in a few lines.
1.Getting started
2.Routes and data
- Routes and handlersHow to declare routes for GET, POST, PUT and DELETE, and what a handler and the ctx context are.
- Path and query parametersHow to get an id from the address with pathParam and the values after the question mark with queryParam.
- JSON in and outHow to return Java objects as JSON with ctx.json and read JSON from the request body with bodyAsClass.
- Status codes and errorsHow to choose a response code, throw a ready-made exception like NotFoundResponse and catch your own exceptions with exception and error.
- Validating dataHow to check the request body with bodyValidator and parameters with queryParamAsClass and check rules.
- Before and after handlersHow to run shared code before and after every request: timing, headers, checks for a group of addresses.
3.Structure and pages
- Controllers and apiBuilderHow to move handlers into a controller class and group routes with apiBuilder and path.
- Static filesHow to serve HTML, CSS, images and scripts from the src/main/resources/public folder with config.staticFiles.
- HTML templatesHow to build an HTML page from data with the Mustache template engine and the ctx.render method.
- HTML formsHow to accept data from an HTML form with formParam, show errors in the template and redirect the browser after POST.
- File uploadsHow to accept a file with uploadedFile, save it to disk safely, limit its size and serve it back by a link.
4.Database and access
- A database with JDBCHow to store posts in an H2 database: a DataSource, a table, a repository class with JDBC queries and wiring it into the controller.
- Login and rolesHow to protect routes with roles: RouteRole, a check in beforeMatched, Basic authentication and the 401 and 403 responses.
- CORS for a frontendWhy CORS is needed, how to allow requests from another address with the bundled plugin and how to check the headers.
- Cookies and sessionsHow to set and remove cookies with ctx.cookie, keep login data in a session with sessionAttribute and build sign-in and sign-out for the blog pages.
- API documentation: OpenAPI and Swagger UIHow to describe routes with the @OpenApi annotation, get an OpenAPI specification from the javalin-openapi plugin and open it in Swagger UI.
5.Real time, tests and packaging
- WebSocket: a blog chatHow to open a permanent connection with the browser using config.routes.ws and broadcast messages to everyone connected.
- SSE: notifications about new postsHow to send events to the browser over one open HTTP connection with config.routes.sse and SseClient.
- Tests with javalin-testtoolsHow to check the API automatically: move app creation into a method, start it in a test with JavalinTest and send requests.
- Packaging a single jarHow to build the application with all its dependencies into one executable jar with maven-shade-plugin and run it on a server.
- Settings, logging and async handlersHow to read settings from environment variables, log every request with requestLogger, which Javalin options are useful on a server and how ctx.future works.