在 Vert.x 3.9 中使用 Thymeleaf TemplateEngine

Using Thymeleaf TemplateEngine with Vert.x 3.9

我是 Vertx 的新手。我正在尝试在我的应用程序中创建 thymeleaf 模板引擎,但它引发了这个错误

java: incompatible types: io.vertx.core.Vertx cannot be converted to io.vertx.reactivex.core.Vertx

如果我将 vertx 转换为 io.vertx.reactive.core.Vertx 然后我得到这个错误

java.lang.ClassCastException: io.vertx.core.impl.VertxImpl cannot be cast to io.vertx.reactivex.core.Vertx

除了数据库连接成功之外,如果我删除 TemplateEngine 行代码运行顺利。

package com.firstapp.main;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.ext.web.common.template.TemplateEngine;
import io.vertx.reactivex.ext.web.handler.TemplateHandler;
import io.vertx.reactivex.ext.web.templ.thymeleaf.ThymeleafTemplateEngine;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnection;

public class MainVerticle extends AbstractVerticle {

  private int server_port = 8080;

  @Override
  public void start(Promise<Void> startPromise) throws Exception {

    Future<Void> handshake = prepareDatabase().compose(estb -> startServer());
    handshake.onComplete(startPromise);

  }

  private Future<Void> startServer(){
    Promise<Void> promise = Promise.promise();

    Router router = Router.router(vertx);
    HttpServer server = vertx.createHttpServer();
TemplateEngine engine = ThymeleafTemplateEngine.create((Vertx) vertx);
TemplateHandler handler = TemplateHandler.create(engine);
    router.route("/").handler(routingContext -> {
      HttpServerResponse response = routingContext.response();
      response.putHeader("content-type", "text/html").end("<h1>First App</h1>");
    });

    server.requestHandler(router).listen(config().getInteger("http.port", server_port), result -> {
      if(result.succeeded()){
        System.out.println("Server Started at Port '"+server_port+"'");
        promise.complete();
      }
      else{
        System.out.println("Failed to Start Server");
        promise.fail(result.cause());
      }
    });

    return promise.future();
  }

  private Future<Void> prepareDatabase(){
    Promise<Void> promise = Promise.promise();

    MySQLConnectOptions connectOptions = new MySQLConnectOptions()
      .setPort(3306)
      .setHost("localhost")
      .setDatabase("vertx_db")
      .setUser("root")
      .setPassword("");

    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);

    client.getConnection(connect -> {
      if (connect.failed()){
        System.out.println("Failed to Connect to Database.");
        promise.fail(connect.cause());
      }
      else{
        SqlConnection conn = connect.result();
        conn.close();
        System.out.println("Database connection successful.");
        promise.complete();
      }
    });

    return promise.future();
  }
}

如果您知道如何修复,请指导我。谢谢

您从哪里导入以下 ...reactivex... class 内容? (即哪个 JAR?):

import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.ext.web.common.template.TemplateEngine;
import io.vertx.reactivex.ext.web.handler.TemplateHandler;
import io.vertx.reactivex.ext.web.templ.thymeleaf.ThymeleafTemplateEngine;

看起来 JAR 包含一个 class io.vertx.reactivex.core.Vertx,它与您需要使用的 Vertex 冲突:io.vertx.core.Vertx.

尝试从您的项目中删除该 JAR(或者只是从那个 class 中删除,如果您在其他地方需要它)然后改用以下内容(显示为 Maven 依赖项):

<dependency>
    <groupId>io.vertx</groupId>
    <artifactId>vertx-core</artifactId>
    <version>3.9.0</version>
</dependency>
<dependency>
    <groupId>io.vertx</groupId>
    <artifactId>vertx-web</artifactId>
    <version>3.9.0</version>
</dependency>
<dependency>
    <groupId>io.vertx</groupId>
    <artifactId>vertx-web-templ-thymeleaf</artifactId>
    <version>3.9.0</version>
</dependency>
<dependency>
    [others, as needed - e.g. your SQL dependencies...]
</dependency>

那么,你应该只需要做这样的事情来支持 Thymeleaf 模板:

import io.vertx.ext.web.templ.thymeleaf.ThymeleafTemplateEngine;

...

final ThymeleafTemplateEngine engine = ThymeleafTemplateEngine.create(vertx);

然后使用 engine.render() 使用模型数据呈现您选择的模板。

GitHub here. The example also depends on the Runner class which is here.

上有一个例子