2016-10-22 13 views
0

私はfaviconといくつかのフォントを提供しようとしています。 FaviconHandlerfavicon.icoと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に行くとき 私のファビコンが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$8.handle(Lion.kt:90) 
    at Lion$main$8.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$1(HttpServerImpl.java:712) 
    at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(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$5.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ファイルがあるの削除に位置しています "Internal Server Error" を引き起こして例外をスローしますサービスはうまく機能していますが、フォントの配信に失敗しています。

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に行くと、ブラウザはフォントを通常のファイルとしてダウンロードしようとしています。

これは潜在的な解決策だと思われますが、私のfaviconではないものがあります。歪んだラインのある16×16の正方形で、フォントはまだブラウザコンソールでエラーをダウンロードしようとしています。

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が正しいMIMEタイプを正しく.woff.icoファイルを提供し、それはそれらのファイルをダウンロードしていない作るのですか?

ソリューション

私が持っていた最初の問題は、Mavenのシェードが何らかの理由でジャーにすべてのフォントファイルをコピーしていなかったということでした。

明示的にビルドでフォントファイルをコピーし、フォントのリソースフィルタを無効にすると、フォントの問題が修正されたようです。

<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> 

は、次にフォントディレクトリがそれを固定する代わりに、リソースディレクトリからフォントを提供

enter image description here

(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")) 

答えて

3

のfavicon.icoは:パスとファイル名を渡す代わりに、あなたのFaviconHandlerを変更

/src/main/resources/favicon.ico 

単に直接resourcesフォルダにレベルをあなたのfavicon.icoファイルを移動します。

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

コードでは、FaviconHandlerもをStaticHandlerに設定しているとみなしていますが、そうではありません。これは純粋にStaticHandlerのプロパティなので、FaviconHandlerは、相対パスを指定しない限り、resources/のリソースを探します。そのリソースが見つからない場合、nullであり、クラッシュするit uses the result of getResourceAsStream("favicon.ico")です。

FaviconHandlerImplの単体テストを見ると、webrootの代わりにthey place the favicon.ico file in the root of resourcesが表示されます。


WOFFファイル:あなたは、問題のために間違った場所で探しているあなたのフォントファイルについては

。 MIMEタイプとは関係ありません。

偶然にWOFFファイルが破損している可能性があります。これはGITでコミットしたときに起こり、テキストファイルであると判断し、行末を壊して破損させた可能性があります。または、Maven Filterプラグインを使用していて、同じことをして、それらを壊しました。または、FTP経由でテキストファイルとしてアップロード/ダウンロードしました。同じ問題です。あなたのファビコンのパスを指定しないhttps://stackoverflow.com/a/33792610/3679676

+0

Mavenは本当にそれを責めていましたが、何らかの理由ですべてのフォントファイルをコピーしていなかったので、リソースフィルタリングもそれを破損していました。フォントディレクトリから提供されていないため、フォントをリソースにコピーして固定しました。私の質問に私の更新されたソリューションを参照してください。 –

1

、そうVERTXはデフォルト1を検索しよう:

はこれについての他の記事を参照してください

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

あなたのファビコンは、resources/webroot/favicon.icoに位置していますが、 Vertxはそれを探しますresources/favicon.ico

FaviconHandler.create("webroot/favicon.ico")を指定するか、1つ上のディレクトリに移動します。

WOFFファイルについては、WOFFがapplication/x-font-woffと返されるため、問題は再現できませんでした。これは正しいと思われます。

+0

あなたは私の5分後に私と同じ答えを投稿しました。 –

+1

申し訳ありませんJayson、投稿する時間がかかりました。それでも、どこでも動かすことなくファビコンに仕える方法を提供したので、怒る理由はないと思います。 –

関連する問題