使用 VertX 服务 favicon.ico 和其他静态文件

Serve favicon.ico and other static files with VertX

我正在尝试提供网站图标和一些字体。

object Lion : AbstractVerticle() {

    @JvmStatic
    @Throws(IOException::class)
    fun main(args: Array<String>) {

    val vertx = Vertx.vertx()
    val router = Router.router(vertx)

    router.route().handler(CorsHandler.create("*")
        .allowedMethod(HttpMethod.GET)
        .allowedMethod(HttpMethod.POST)
        .allowedMethod(HttpMethod.OPTIONS)
        .allowedHeader("X-PINGARUNER")
        .allowedHeader("Content-Type"))

    // some json GET / POST routes here

    router.route().handler(FaviconHandler.create());
    router.route().handler(StaticHandler.create())

    vertx.createHttpServer().requestHandler { router.accept(it) }.listen(9090)
}

当我转到 http://localhost:9090/favicon.ico 时,FaviconHandler 抛出异常导致 "Internal Server Error" 我的图标位于 src/main/resources/webroot/favicon.ico

Oct 22, 2016 11:16:42 PM io.vertx.ext.web.impl.RoutingContextImplBase
SEVERE: Unexpected exception in route
java.lang.RuntimeException: java.lang.NullPointerException
    at io.vertx.ext.web.handler.impl.FaviconHandlerImpl.init(FaviconHandlerImpl.java:148)
    at io.vertx.ext.web.handler.impl.FaviconHandlerImpl.handle(FaviconHandlerImpl.java:155)
    at io.vertx.ext.web.handler.impl.FaviconHandlerImpl.handle(FaviconHandlerImpl.java:33)
    at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:215)
    at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:78)
    at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:94)
    at io.vertx.ext.web.handler.impl.CorsHandlerImpl.handle(CorsHandlerImpl.java:121)
    at io.vertx.ext.web.handler.impl.CorsHandlerImpl.handle(CorsHandlerImpl.java:38)
    at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:215)
    at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:78)
    at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:94)
    at io.vertx.ext.web.impl.RouterImpl.accept(RouterImpl.java:79)
    at Lion$main.handle(Lion.kt:90)
    at Lion$main.handle(Lion.kt:43)
    at io.vertx.core.http.impl.ServerConnection.handleRequest(ServerConnection.java:286)
    at io.vertx.core.http.impl.ServerConnection.processMessage(ServerConnection.java:412)
    at io.vertx.core.http.impl.ServerConnection.handleMessage(ServerConnection.java:139)
    at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.lambda$createConnAndHandle(HttpServerImpl.java:712)
    at io.vertx.core.impl.ContextImpl.lambda$wrapTask(ContextImpl.java:314)
    at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:190)
    at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.createConnAndHandle(HttpServerImpl.java:706)
    at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:570)
    at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:522)
    at io.vertx.core.http.impl.VertxHttpHandler.channelRead(VertxHttpHandler.java:76)
    at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:122)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
    at io.vertx.core.http.impl.HttpServerImpl$Http1xOrHttp2Handler.http1(HttpServerImpl.java:1019)
    at io.vertx.core.http.impl.HttpServerImpl$Http1xOrHttp2Handler.channelRead(HttpServerImpl.java:990)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:610)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:551)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:465)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:437)
    at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:873)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at io.vertx.ext.web.handler.impl.FaviconHandlerImpl$Icon.<init>(FaviconHandlerImpl.java:61)
    at io.vertx.ext.web.handler.impl.FaviconHandlerImpl$Icon.<init>(FaviconHandlerImpl.java:40)
    at io.vertx.ext.web.handler.impl.FaviconHandlerImpl.init(FaviconHandlerImpl.java:143)
    ... 48 more

删除 FaviconHandler 和普通 htmljscss 文件可以正常使用,但无法使用字体。

Failed to decode downloaded font: http://localhost:9090/fonts/glyphicons-halflings-regular.woff
/#/tcr:1 OTS parsing error: incorrect file size in WOFF header

当我直接转到 font-url 时,浏览器正在尝试将字体下载为常规文件。

这似乎是一个潜在的解决方案,但我看到的不是我的收藏夹图标,只是一个 16x16 的正方形,里面有扭曲的线条,字体仍在尝试下载,但仍在浏览器控制台中显示错误.

    router.route("/favicon.ico").handler {
        it.response().putHeader("Content-Type", "image/x-icon").sendFile("webroot/favicon.ico")
    }

    router.route("/fonts/glyphicons-halflings-regular.woff").handler { 
         it.response().putHeader("Content-Type", "application/font-woff").sendFile("webroot/fonts/glyphicons-halflings-regular.woff")
    }

总而言之,如何使 StaticHandler 使用正确的 MimeTypes 正确地提供 .woff.ico 文件,而不是让它下载这些文件?

解决方法:

我遇到的第一个问题是 maven shade 出于某种原因没有将所有字体文件复制到 jar 中。

在构建时明确复制字体文件并禁用字体资源过滤似乎已经解决了字体问题。

<build>
    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources/fonts</directory>
            <filtering>false</filtering>
        </resource>
    </resources>

然后从资源目录而不是字体目录提供字体修复它(VertX 出于某种原因不会从字体目录提供服务,即使文件在那里)

   router.route("/fonts/glyphicons-halflings-regular.woff2").handler {
            it.response().sendFile("glyphicons-halflings-regular.woff2")
        }
        router.route("/fonts/glyphicons-halflings-regular.woff").handler {
            it.response().sendFile("glyphicons-halflings-regular.woff")
        }
        router.route("/fonts/glyphicons-halflings-regular.ttf").handler {
            it.response().sendFile("glyphicons-halflings-regular.ttf")
        }
        router.route().handler(StaticHandler.create().setCachingEnabled(true));

可能正在缓存 Favicon,仍在调查中,目前修复 Favicon 并不是那么重要。

router.route("/favicon.ico").handler(FaviconHandler.create("favicon.ico"))

Favicon.ico:

只需将您的 favicon.ico 文件直接向上移动到 resources 文件夹中:

/src/main/resources/favicon.ico

或者更改您的 FaviconHandler 以传入路径和文件名:

router.route().handler(FaviconHandler.create(FaviconHandler.create("webroot/favicon.ico")))

您的代码假定 FaviconHandler 也遵守 StaticHandler 中设置的 webroot,但事实并非如此。这纯粹是 StaticHandler 的 属性,因此 FaviconHandlerresources/ 中查找资源,除非您指定相对路径。当找不到该资源时,it uses the result of getResourceAsStream("favicon.ico")null 并崩溃。

如果您查看 FaviconHandlerImpl 的单元测试,您会看到 they place the favicon.ico file in the root of resources 而不是 webroot


WOFF 文件:

至于你的字体文件,你在错误的地方寻找问题。它与 MIME 类型无关。

更有可能是您不小心损坏了 WOFF 文件。当您使用 GIT 提交它们并且它认为它们是文本文件并且它打破了行尾从而破坏了它们时,可能会发生这种情况。或者你使用了 Maven Filter 插件,它做了同样的事情,破坏了它们。或者你 uploaded/downloaded 他们通过 FTP 作为文本文件,同样的问题。

查看有关此的其他帖子:

您没有指定您的网站图标路径,所以 Vertx 会尝试找到默认路径:

 if (path == null) {
    icon = new Icon(Utils.readResourceToBuffer("favicon.ico"));
 }

您的网站图标位于 resources/webroot/favicon.ico,但 Vertx 在 resources/favicon.ico

查找它

因此您可以指定 FaviconHandler.create("webroot/favicon.ico") 或将其向上移动一个目录。

关于 WOFF 文件,我无法重现问题,因为 WOFF returns 和 application/x-font-woff,这似乎是正确的。