在VertX中路由静态内容

我正在使用Kotlin使用Vert.X( 通过org.jetbrains.kotlinx:vertx3-lang-kotlin库 ),我试图构建一个自包含在jar中的单页应用程序。

在maven方面,这些是我的依赖:

<properties> ... <vertx.version>3.3.2</vertx.version> <kotlin.version>1.0.3</kotlin.version> <main.class>Bob</main.class> </properties> 

 <!-- Vertx Dependencies --> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>${vertx.version}</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> <version>${vertx.version}</version> </dependency> <!-- Kotlin Dependencies --> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> <!-- Kotlin Vertx Binding Dependency --> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>vertx3-lang-kotlin</artifactId> <version>[0.0.4,0.1.0)</version> </dependency> 

在我的主课,我有以下几点:

 object Bob { val log = LoggerFactory.getLogger(javaClass) val port = 9999 @JvmStatic fun main(args: Array<String>) { DefaultVertx { httpServer(port = Bob.port, block = Route { GET("/") { request -> headers().add("Author", "Re@PeR") contentType("text/html") sendFile("html/index.html") } otherwise { setStatus(404, "Resource not found") body { write("The requested resource was not found\n") } } }); } } 

localhost:9999 index.html服务成功。 我现在希望能够请求css / js文件并为它们服务

 <!doctype html> <html> <head> <link rel="stylesheet" href="css/bootstrap.min.css" > <link rel="stylesheet" href="css/bootstrap-theme.min.css" > <script src="js/bootstrap.min.js"></script> 

对于每个资源,浏览器按照预期返回错误404。

我现在正在尝试使用CSS来服务

  GET("/css/*") { request -> println("Serving CSS") contentType("text/css") sendFile("css/${request.path()}") } 

但是我没有看到它进入这个块,它继续服务错误404s。

什么是使用org.jetbrains.kotlinx在Vert.X中提供静态文件的正确方法:vertx3-lang-kotlin库?

这简单得多。
在你的例子中可能是:

 router.route("/css/*").handler(StaticHandler.create()); 

但实际上,你应该把所有东西放在/静态或/公用文件夹和服务器,就像这样:

 router.route("/static/*").handler(StaticHandler.create()); 

它通常是我的Kotlin应用程序:

 val router = Router.router(vertx) router.route().handler(StaticHandler.create()) // Other routes here... 

以这种方式创建StaticHandler时,默认情况下它将从/resources/webroot目录服务器。