路由器java灯一直闪_java – Vertx的路由器问题

早上好,

我从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("

Test1

");

});

到目前为止,您已经建立了一个简单的路由,因此下一步是创建一个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 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

我希望这些可以帮到你:)

标签:java,eclipse,routes,vert-x

来源: https://codeday.me/bug/20190527/1167272.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值