[Vert.X] Problème avec l'utilisation de HandlebarsTemplateEngine
Bonjour,
depuis peu, je fais joujou avec Vert.X, çà tournait plutôt bien, jusqu'à ce que je tente d'incorporer le moteur de Template HandleBars qui d'après la doc fonctionne très bien, mais les exemples sont peu nombreux et incomplets.
Dans mon cas, mon soucis semble venir du path de mon fichier .hbs (format de fichier template handleBars)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
package com.vertxtest.test_vertx_01;
import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.handler.TemplateHandler;
import io.vertx.ext.web.templ.HandlebarsTemplateEngine;
import io.vertx.ext.web.templ.TemplateEngine;
public class HelloWorld extends AbstractVerticle {
public static final String APPLICATION_JSON = "application/json";
final Router router = Router.router(vertx);
@Override
public void start() throws Exception {
// In order to use a template we first need to create an engine
final TemplateEngine engine = HandlebarsTemplateEngine.create();
TemplateHandler handler = TemplateHandler.create(engine);
router.get("/dynamic/").handler(handler);
// Route all GET requests for resource ending in .hbs to the template
// handler
router.getWithRegex(".+\\.hbs").handler(context -> {
final Session session=context.session();
context.data().put("userLogin",session.get("login"));
context.data().put("accessToken",session.get("accessToken"));
context.next();
});
router.get("/hello/").handler(
req -> {
req.response().putHeader("content-type", "text/html")
.end("<html><body><h1>Hello World</h1></body></html>");
System.out.println(req.request().getParam("toto").trim());
});
vertx.createHttpServer().requestHandler(router::accept).listen(8989, res -> {
if (res.succeeded()) {
System.out.println("success: " + res.toString());
} else {
System.out.println("failed : " + res.cause());
}
});
}
} |
Lorsque je tente d'accèder à l'url http://localhost:8989/dynamic, j'obtiens l'erreur suivante, qui résulte d'un problème lors de la tentative de lecture du fichier hbs depuis la méthode vert.X readFileToString :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
juil. 13, 2016 5:42:47 PM io.vertx.core.impl.BlockedThreadChecker
AVERTISSEMENT: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 720 ms, time limit is 0
juil. 13, 2016 5:42:48 PM io.vertx.core.impl.BlockedThreadChecker
AVERTISSEMENT: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 1720 ms, time limit is 0
juil. 13, 2016 5:42:49 PM io.vertx.core.impl.BlockedThreadChecker
AVERTISSEMENT: Thread Thread[vert.x-eventloop-thread-2,5,main] has been blocked for 2720 ms, time limit is 0
juil. 13, 2016 5:42:50 PM io.vertx.ext.web.impl.RoutingContextImplBase
GRAVE: Unexpected exception in route
io.vertx.core.VertxException: java.lang.NullPointerException
at io.vertx.ext.web.impl.Utils.readFileToString(Utils.java:156)
at io.vertx.ext.web.templ.impl.HandlebarsTemplateEngineImpl$Loader.sourceAt(HandlebarsTemplateEngineImpl.java:91)
at com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:411)
at com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:397)
at io.vertx.ext.web.templ.impl.HandlebarsTemplateEngineImpl.render(HandlebarsTemplateEngineImpl.java:64)
at io.vertx.ext.web.handler.impl.TemplateHandlerImpl.handle(TemplateHandlerImpl.java:61)
at io.vertx.ext.web.handler.impl.TemplateHandlerImpl.handle(TemplateHandlerImpl.java:45)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:218)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:67)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:96)
at io.vertx.ext.web.impl.RouterImpl.accept(RouterImpl.java:61)
at io.vertx.core.http.impl.ServerConnection.handleRequest(ServerConnection.java:276)
at io.vertx.core.http.impl.ServerConnection.processMessage(ServerConnection.java:391)
at io.vertx.core.http.impl.ServerConnection.handleMessage(ServerConnection.java:137)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.lambda$createConnAndHandle$24(HttpServerImpl.java:539)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:312)
at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:217)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.createConnAndHandle(HttpServerImpl.java:537)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:474)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:425)
at io.vertx.core.http.impl.VertxHttpHandler.channelRead(VertxHttpHandler.java:85)
at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:124)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:244)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at io.vertx.ext.web.impl.Utils.readFileToString(Utils.java:153)
... 34 more |
Quelqu'un sait il comment fonctionne le path dans un projet Vert.x/Maven ?