Deploy WebJars

Gradle setup

dependencies {

    // for the example...
    implementation("org.webjars:swagger-ui", version = "3.43.0")

WebJars is a library to ship pre-packaged Web assets on your classpath by just adding the dependency. The assets are rehoused under the META-INF directory and end up with URLs such as:


http4k integrates this functionality into the core library and ships with the webJars() router plugin to activate. As the plugin is just an HttpHandler, the simplest example is just to launch WebJars directly as a Server:


... or a more standard use-case is to mix it into your application routing as in the example below:


package guide.howto.deploy_webjars

import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.routing.webJars
import org.http4k.server.SunHttp
import org.http4k.server.asServer

fun main() {
    // mix the WebJars routing into your app...
    val app = routes(
        "/myGreatRoute" bind GET to { _: Request -> Response(OK) },


    // then browse to: http://localhost:8080/webjars/swagger-ui/5.1.3/index.html