io.vertx.core.AbstractVerticle
sınıfındaki ilk Vertx
örneğini belirtir. io.vertx.core.Vertx
, io.vertx.rxjava.core.Vertx
'a değil. Yani io.vertx.rxjava.ext.web.Router
yerine io.vertx.ext.web.Router
almanız gerekiyor, yoksa eşleşmiyor. Rota yöntemi
Router router = Router.router(vertx);
Ve sonra çağırmak:
İkincisi, Route#route
yöntem bir Yönlendirici örneğini oluşturmak için gereken birinci statik bir yöntem değildir
router.route("/Test1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.write("Test1 \n");
routingContext.vertx().setTimer(1000, tid -> routingContext.next());
});
Bildirimi o route
yöntem mutlak URL'yi kabul etmez, böylece mutlak URL'yi geçmeniz gerekmez. Doğrudan /Test1
URL yolunu geçirin ve host ve port, createHttpServer olduğunda yapılandırılabilir.
Başka bildirim doğrudan yanıt bayt yazmak isterseniz, size response.setChunked(true)
eklemek gerekir veya bir istisna atılmış olacağını yani: (end
ile wnd daima)
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.
Ya da bu daha iyidir:
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080, "127.0.0.1", res -> {
if (res.failed())
res.cause().printStackTrace();
});
:
router.route("/Test1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "text/html");
response.end("<h1>Test1</h1>");
});
basit bir rota kurduk artık
nedenle bir sonraki adım bir HTTPServer oluşturmaktır
Bağlantı noktası ve ana makine, listen
yöntemine geçirilebilir. Yani bu sadece basit bir örnek
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();
});
}
}
:
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());
});
}
}
çalıştırın bundan Dikey dağıtmak:
Yani bu değişti doğru kodudur. Ve bir RESTful Hizmeti oluşturmak istiyorsanız, bu makalede yardımcı olabilir: Some Rest with Vert.x
Ve burada Vert kullanarak bir RESTful Hizmeti örneğim.de yardımcı olabilir x Web: Bunları sana :) yardımcı olabilir umut https://github.com/sczyh30/todo-backend-vert.x
@ senin Explications için sczyh30, çok yararlı teşekkürler :) Ben olsam bile (Gradle kullanarak kodumu yeniden inşa etmek için çalıştı Bununla aşina değil, ama yeni şeyler keşfetmek güzeldir. Ama normal bir proje gibi görünmüyor, demek istediğim konfigürasyonu çalıştırmak ve benimkini ayarlamak, yanlışsam beni düzeltmek. Saygılar. – zackzulg