Home > OS >  Javalin Migration from 3 to 4
Javalin Migration from 3 to 4

Time:12-29

We are migrating the Javalin from 3 to 4 in our current kotlin project. the dynamicGzip has been deprecated and replaced with compression strategy. The pom.xml part will look like below.

<properties>      
  <javalin.version>4.1.1</javalin.version>
  <jackson.version>2.13.0</jackson.version>
</properties>

The code part of kotlin is as follows

import io.javalin.Javalin
import io.javalin.apibuilder.ApiBuilder.*
import io.javalin.http.BadRequestResponse
import io.javalin.http.NotFoundResponse
import io.javalin.http.staticfiles.Location
import io.javalin.plugin.json.JavalinJackson
import io.javalin.core.compression.*

val app = Javalin.create { config ->
        config.defaultContentType = "application/json"
        config.enableWebjars()
        config.addStaticFiles("", Location.CLASSPATH)
        config.enableCorsForAllgOrigins()
        //it.dynamicGzip = true // deprecated method which was used in 3.12.0
        config.compressionStrategy(Gzip(6))
    }

We are using the migrating document from this link https://javalin.io/migration-guide-javalin-3-to-4

When we try to build the project in intelij Idea with this change, ended with the below error.

 D:\app\src\main\kotlin\app\app.kt:78:40
 Kotlin: Unresolved reference: Gzip

What is that we are missing here?

Also it will be helpfull if config.addStaticFiles syntax is also added wrt javalin 4

CodePudding user response:

Compression

The compressionStrategy method of the JavalinConfig class takes two parameters:

void compressionStrategy(Brotli brotli, Gzip gzip)

See the JavaDoc here.

The related classes are found in Javalin here:

import io.javalin.core.compression.Brotli;
import io.javalin.core.compression.Gzip;

So, you can do something like this in your set-up (my example is Java not Kotlin):

// my Java example:
config.compressionStrategy(new Brotli(6), new Gzip(6));

Static Files

You can use something like this (again, a Java example not Kotlin):

// my Java example:
config.addStaticFiles("/public", Location.CLASSPATH);

In this case, because I want my files to be on the runtime classpath, I have also created a public directory in my application's resources directory. Your specific implementation may differ.

You can also use Location.EXTERNAL if you prefer, to place the files somewhere else in your filesystem (outside the application).


Note also there is a small typo in config.enableCorsForAllgOrigins(). It should be:

config.enableCorsForAllOrigins()
  • Related