Javalin 1 to 2 migration guide
Package structure
Javalin 2 has some changes to the package structure:
io.javalin.embeddedserver.jetty.websocket->io.javalin.websocketio.javalin.embeddedserver.Location->io.javalin.staticfiles.Locationio.javalin.translator.json.JavalinJsonPlugin->io.javalin.json.JavalinJsonio.javalin.translator.json.JavalinJacksonPlugin->io.javalin.json.JavalinJacksonio.javalin.translator.template.JavalinXyzPlugin->io.javalin.rendering.JavalinXyzio.javalin.security.Role.roles->io.javalin.security.SecurityUtil.rolesio.javalin.ApiBuilder->io.javalin.apibuilder.ApiBuilderio.javalin.ApiBuilder.EndpointGrooup->io.javalin.apibuilder.EndpointGrooup
Server customization/defaults
app.embeddedServer(new EmbeddedJettyFactory(() -> new Server())) // v1
app.server(() -> new Server()) // v2
- The static method
Javalin.start(port)has been removed.Javalin.create().start(0);is now required. - Dynamic gzip is now enabled by default, turn it off with
disableDynamicGzip() - Request-caching is now limited to 4kb by default
- Server now has a
LowResourceMonitorattached by default defaultCharset()method has been removed- URLs are now case-insensitive by default, meaning Javalin will treat
/pathand/Pathas the same URL. This can be disabled withapp.enableCaseSensitiveUrls().
WebSockets
It was possible to defined WebSockets using Jetty annotations in v1 of Javalin. These Jetty WebSockets have limited functionality compared to the Javalin lambda WebSockets, which is why they have been removed.
AccessManager
- Use
Setinstead ofList - The AccessManager now runs for every single request, but the default-implementation does nothing. This might break some implementations that relied on un-managed routes.
Context
- The
CookieBuilderclass has been removed, useCookiedirectly. ctx.uri()has been removed, it was a duplicate ofctx.path()- Things that used to return
Array<T>now returnList<T> - Things that used to return nullable collections now return empty collections instead
ctx.param()is nowctx.pathParam()ctx.xyzOrDefault("key")methods have been change intoctx.xyz("key", "default")ctx.next()has been removed- Kotlin users can now do
ctx.body<MyClass>()to deserialize json ctx.request()is nowctx.reqctx.response()is nowctx.res- All
ctx.renderXyzmethods are now justctx.render()(correct engine is chosen based on extension) ctx.charset(charset)has been removed
Events
- Event handlers no longer take
Eventas an argument (they now take nothing) io.javalin.event.EventTypeis nowio.javalin.JavalinEvent
Misc
- Reverse routing has been removed (will come back, but better)
HaltExceptionhas been removed and replaced withHttpResponseException. Some common responses are included:- RedirectResponse
- BadRequestResponse
- UnauthorizedResponse
- ForbiddenResponse
- NotFoundResponse
- MethodNotAllowedResponse
- InternalServerErrorResponse
- ServiceUnavailableResponse