IntelliJ 和 Vertx:如何 运行 org.vertx.java.deploy.impl.cli.Starter?
IntelliJ and Vertx: How to run org.vertx.java.deploy.impl.cli.Starter ?
我在 Maven 项目中有以下代码:
package hello;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.deploy.Verticle;
public class Server extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest req) {
System.out.println("Got request: " + req.uri);
System.out.println("Headers are: ");
for (String key : req.headers().keySet()) {
System.out.println(key + ":" + req.headers().get(key));
}
req.response.headers().put("Content-Type", "text/html; charset-UTF-8");
req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}
}).listen(4000);
}
}
我在pom.xml中也有以下依赖:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.vert-x</groupId>
<artifactId>vertx-platform</artifactId>
<version>1.2.3.final</version>
</dependency>
我尝试使用以下配置在 IntelliJ 中 运行 Java 应用程序:
我遇到很多错误:
Error:(7, 8) java: hello.Server is not abstract and does not override
abstract method stop(io.vertx.core.Future) in
io.vertx.core.Verticle Error:(12, 57) java: cannot find symbol
symbol: variable uri location: variable req of type
io.vertx.core.http.HttpServerRequest Error:(14, 48) java: cannot find
symbol symbol: method keySet() location: interface
io.vertx.core.MultiMap Error:(17, 20) java: cannot find symbol
symbol: variable response location: variable req of type
io.vertx.core.http.HttpServerRequest Error:(18, 20) java: cannot find
symbol symbol: variable response location: variable req of type
io.vertx.core.http.HttpServerRequest Error:(9, 9) java: cannot find
symbol symbol: variable vertx location: class hello.Server
然而,当我从这里下载 vert.x jar 文件时:
http://vertx.io/download/
并将它们放入项目结构中,然后相同的代码编译成功。
可能我需要 pom.xml 中的另一个依赖项,但我不知道它应该是什么。
你不应该有依赖:
<dependency>
<groupId>org.vert-x</groupId>
<artifactId>vertx-platform</artifactId>
<version>1.2.3.final</version>
</dependency>
因为它与 Vert.x 1.2 和您的应用程序有关 3.x。在这种情况下,您的主要 class 应该是:
io.vertx.core.Launcher
或者您可以添加一个 main
方法并从那里进行调试,例如:
public class Server extends Verticle {
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new Server());
}
}
现在您无需担心添加启动器配置文件,只需右键单击并 run/debug。
我在 Maven 项目中有以下代码:
package hello;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.deploy.Verticle;
public class Server extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest req) {
System.out.println("Got request: " + req.uri);
System.out.println("Headers are: ");
for (String key : req.headers().keySet()) {
System.out.println(key + ":" + req.headers().get(key));
}
req.response.headers().put("Content-Type", "text/html; charset-UTF-8");
req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}
}).listen(4000);
}
}
我在pom.xml中也有以下依赖:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.vert-x</groupId>
<artifactId>vertx-platform</artifactId>
<version>1.2.3.final</version>
</dependency>
我尝试使用以下配置在 IntelliJ 中 运行 Java 应用程序:
我遇到很多错误:
Error:(7, 8) java: hello.Server is not abstract and does not override abstract method stop(io.vertx.core.Future) in io.vertx.core.Verticle Error:(12, 57) java: cannot find symbol
symbol: variable uri location: variable req of type io.vertx.core.http.HttpServerRequest Error:(14, 48) java: cannot find symbol symbol: method keySet() location: interface io.vertx.core.MultiMap Error:(17, 20) java: cannot find symbol
symbol: variable response location: variable req of type io.vertx.core.http.HttpServerRequest Error:(18, 20) java: cannot find symbol symbol: variable response location: variable req of type io.vertx.core.http.HttpServerRequest Error:(9, 9) java: cannot find symbol symbol: variable vertx location: class hello.Server
然而,当我从这里下载 vert.x jar 文件时: http://vertx.io/download/
并将它们放入项目结构中,然后相同的代码编译成功。
可能我需要 pom.xml 中的另一个依赖项,但我不知道它应该是什么。
你不应该有依赖:
<dependency>
<groupId>org.vert-x</groupId>
<artifactId>vertx-platform</artifactId>
<version>1.2.3.final</version>
</dependency>
因为它与 Vert.x 1.2 和您的应用程序有关 3.x。在这种情况下,您的主要 class 应该是:
io.vertx.core.Launcher
或者您可以添加一个 main
方法并从那里进行调试,例如:
public class Server extends Verticle {
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new Server());
}
}
现在您无需担心添加启动器配置文件,只需右键单击并 run/debug。