早上好,
我从Vertx for java开始,我想创建路由示例,所以我复制过去的行,但似乎在Router.router(顶点)中出现了问题:
The type io.vertx.rxjava.core.Vertx cannot be resolved. It is indirectly referenced from
required .class files
- The method router(Vertx) from the type Router refers to the missing type Vertx
这是我的代码:
import io.vertx.core.*;
import io.vertx.rxjava.ext.web.*;
import io.vertx.core.http.HttpHeaders;
public class Test extends AbstractVerticle{
@Override
public void start() throws Exception {
Vertx vertx=Vertx.vertx();
Router router = Router.router(vertx);
Route route1 = router.route("localhost:8080/Orsys/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("Orsys\n");
routingContext.vertx().setTimer(5000, tid -> routingContext.next());
});
解决方法:
首先,io.vertx.core.AbstractVerticle类中的Vertx实例引用io.vertx.core.Vertx,而不是io.vertx.rxjava.core.Vertx.所以你应该导入io.vertx.ext.web.Router而不是io.vertx.rxjava.ext.web.Router,否则它将不匹配.
其次,Route#route方法不是静态方法,所以首先需要创建一个Router实例:
Router router = Router.router(vertx);
然后调用route方法:
router.route("/Test1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("Test1 \n");
routingContext.vertx().setTimer(1000, tid -> routingContext.next());
});
请注意,route方法不接受绝对URL,因此您无需传递绝对URL.直接传递/ Test1 URL路径,并在createHttpServer时配置主机和端口.
另一个注意事项是,如果要将字节直接写入响应,则应添加response.setChunked(true)或抛出异常:
java.lang.IllegalStateException: You must set the Content-Length header to be the total size of the message body BEFORE sending any data if you are not using HTTP chunked encoding.
或者这更好(总是与结束):
router.route("/Test1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "text/html");
response.end("<h1>Test1</h1>");
});
到目前为止,您已经建立了一个简单的路由,因此下一步是创建一个HttpServer:
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080, "127.0.0.1", res -> {
if (res.failed())
res.cause().printStackTrace();
});
好吧,端口和主机可以传递给listen方法.
所以这是更改的正确代码:
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;
public class TestVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
Router router = Router.router(vertx);
router.route("/Test1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("Test1 \n");
routingContext.vertx().setTimer(1000, tid -> routingContext.next());
});
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080, "127.0.0.1", res -> {
if (res.succeeded())
startFuture.complete();
else
startFuture.fail(res.cause());
});
}
}
通过部署Verticle来运行它:
public class Application {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
TestVerticle verticle = new TestVerticle();
vertx.deployVerticle(verticle, res -> {
if (res.failed())
res.cause().printStackTrace();
});
}
}
所以这只是一个简单的例子.如果您想构建RESTful服务,本文可能会有所帮助:Some Rest with Vert.x
这是我使用Vert.x Web的RESTful服务的示例,它也可能有所帮助:https://github.com/sczyh30/todo-backend-vert.x
我希望这些可以帮到你:)