本文整理匯總了Java中io.vertx.ext.web.Router類的典型用法代碼示例。如果您正苦於以下問題:Java Router類的具體用法?Java Router怎麽用?Java Router使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Router類屬於io.vertx.ext.web包,在下文中一共展示了Router類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
int serverPort = Config.getIntValue("serverPort");
router.route().handler(BodyHandler.create().setUploadsDirectory("files"));
router.route().handler(CookieHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
router.route().handler(CORSHandler.create());
router.route().handler(LogHandler.create());
Routing.route(router, "com.planb.restful");
router.route().handler(StaticHandler.create());
Log.info("Server Started At : " + serverPort);
vertx.createHttpServer().requestHandler(router::accept).listen(serverPort);
}
示例2: handleRequest
import io.vertx.ext.web.Router; //導入依賴的package包/類
/**
* This is a handler method called by the AWS Lambda runtime
*/
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
// Lambda function is allowed write access to /tmp only
System.setProperty("vertx.cacheDirBase", "/tmp/.vertx");
Vertx vertx = Vertx.vertx();
router = Router.router(vertx);
router.route().handler(rc -> {
LocalDateTime now = LocalDateTime.now();
rc.response().putHeader("content-type", "text/html").end("Hello from Lambda at " + now);
});
// create a LambdaServer which will process a single HTTP request
LambdaServer server = new LambdaServer(vertx, context, input, output);
// trigger the HTTP request processing
server.requestHandler(this::handleRequest).listen();
// block the main thread until the request has been fully processed
waitForResponseEnd();
}
示例3: extendedContentTypeTest
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Test
public void extendedContentTypeTest(TestContext context) {
RestRouter.getReaders().register(Dummy.class, DummyBodyReader.class);
TestReaderRest testRest = new TestReaderRest();
Router router = RestRouter.register(vertx, testRest);
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(PORT);
final Async async = context.async();
client.post("/read/normal/dummy", response -> {
context.assertEquals(200, response.statusCode());
response.handler(body -> {
context.assertEquals("one=dummy", body.toString()); // returns sorted list of unique words
async.complete();
});
}).putHeader("Content-Type", "application/json;charset=UTF-8")
.end(JsonUtils.toJson(new Dummy("one", "dummy")));
}
示例4: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Override
public void start(Future<Void> startFuture) throws Exception {
super.start();
// 如果本地未配置地址,則表示不必監聽,隻需要作為客戶端使用即可
if (endpointObject == null) {
LOGGER.warn("rest listen address is not configured, will not start.");
startFuture.complete();
return;
}
Router mainRouter = Router.router(vertx);
mountAccessLogHandler(mainRouter);
initDispatcher(mainRouter);
HttpServer httpServer = createHttpServer();
httpServer.requestHandler(mainRouter::accept);
startListen(httpServer, startFuture);
}
示例5: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Override
public void start(final Future<Void> startFuture) throws Exception {
HttpServer http = vertx.createHttpServer();
Router router = Router.router(vertx);
router.get("/hello").handler(ctx -> ctx.response().end("World " + System.currentTimeMillis()));
http.requestHandler(router::accept).listen(11011, result -> {
if(result.succeeded()){
System.out.println("Listening on port 11011");
} else {
throw new RuntimeException("Server start failed");
}
});
}
示例6: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Before
public void start(TestContext context) {
super.before(context);
Router router = new RestBuilder(vertx)
.register(TestMultiProducesRest.class)
.writer(MediaType.APPLICATION_XML, TestXmlResponseWriter.class)
.writer(MediaType.APPLICATION_JSON, TestJsonResponseWriter.class)
.build();
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(PORT);
}
示例7: dynamicPages
import io.vertx.ext.web.Router; //導入依賴的package包/類
private void dynamicPages(Router router) {
HandlebarsTemplateEngine hbsEngine = new ClasspathAwareHandlebarsTemplateEngine();
hbsEngine.setMaxCacheSize(applicationConfiguration.getViewTemplateCacheSize());
TemplateHandler templateHandler = TemplateHandler.create(hbsEngine);
if (applicationConfiguration.isAuthenticationEnabled()) {
router.get(WebServerUtils.withApplicationName("dataset/*")).handler(this::fromSession);
router.get(WebServerUtils.withApplicationName("management/*")).handler(this::fromSession);
}
router
.getWithRegex(".+\\.hbs")
.handler(
context -> {
final Session session = context.session();
WebServerUtils.assignUserToRoutingContext(context);
context.next();
});
router.getWithRegex(".+\\.hbs").handler(templateHandler);
}
示例8: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Before
public void start(TestContext context) {
super.before(context);
TestContextRest testRest = new TestContextRest();
Router router = RestRouter.register(vertx, testRest);
//RestRouter.provide(router, TokenProvider.class);
RestRouter.addProvider(Dummy.class, request -> new Dummy("test", "name"));
RestRouter.addProvider(TokenProvider.class);
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(PORT);
}
示例9: testCustomInput_2
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Test
public void testCustomInput_2(TestContext context) {
RestRouter.getReaders()
.register(List.class,
CustomWordListReader.class); // all arguments that are List<> go through this reader ... (reader returns List<String> as output)
Router router = RestRouter.register(vertx, TestReaderRest.class);
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(PORT);
// call and check response
final Async async = context.async();
client.post("/read/registered", response -> {
context.assertEquals(200, response.statusCode());
response.handler(body -> {
context.assertEquals("brown,dog,fox,jumps,over,quick,red,the", body.toString()); // returns sorted list of unique words
async.complete();
});
}).end("The quick brown fox jumps over the red dog!");
}
示例10: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Override
public void start(final Future<Void> startFuture) throws Exception {
final Router router = router();
new Endpoints(vertx, router).initialize();
final int port = config().getInteger("http.port", 8080);
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(port,
result -> {
if (result.succeeded()) {
LOG.info("Started API on port " + port);
startFuture.complete();
} else {
startFuture.fail(result.cause());
}
});
}
示例11: initialize
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Override
protected void initialize() {
discovery = discoveryHolder.getServiceDiscovery();
circuitBreaker = circuitBreakerHolder.getCircuitBreaker();
Router router = router();
enableHeartbeatCheck(router);
String address = getAddress();
Integer port = getApiPort();
setup();
HttpServerBuilder.create(vertx).router(router).httpServerOptions(httpServerOptions()).build().listen(port, address,
this::onHttpServer);
}
示例12: setUp
import io.vertx.ext.web.Router; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
vertx = Vertx.vertx();
JsonObject config = vertx.getOrCreateContext().config();
Router router = Router.router(vertx);
configuration = new VertxConfiguration(config);
configuration.put(SSL_CONFIGURATION_KEY, FALSE);
configuration.put(SSL_JKS_PATH, TEST_JKS_PATH);
configuration.put(SSL_JKS_SECRET, TEST_JKS_SECRET);
configuration.put(HTTPS_PORT, DEFAULT_HTTPS_PORT);
options = new HttpServerOptions();
context = VertxContext.VertxContextBuilder.vertx(vertx)
.router(router)
.serverConfiguration(configuration)
.vertxServiceDiscovery(new VertxServiceDiscovery(vertx)).build();
}
示例13: start
import io.vertx.ext.web.Router; //導入依賴的package包/類
public void start() {
sdk = null;
config = null;
if (server == null) {
Vertx vertx = Vertx
.vertx(new VertxOptions().setWorkerPoolSize(40).setBlockedThreadCheckInterval(1000L * 60L * 10L)
.setMaxWorkerExecuteTime(1000L * 1000L * 1000L * 60L * 10L));
HttpServerOptions options = new HttpServerOptions();
options.setMaxInitialLineLength(HttpServerOptions.DEFAULT_MAX_INITIAL_LINE_LENGTH * 2);
server = vertx.createHttpServer(options);
router = Router.router(vertx);
}
retrieveConfig();
if (config == null || config.isApiKeyEmpty()) {
logError("Unable to find " + String.valueOf(ENVVAR_MBED_CLOUD_API_KEY) + " environment variable");
System.exit(1);
}
defineInitialisationRoute();
defineModuleMethodTestRoute();
logInfo("Starting Java SDK test server on port " + String.valueOf(port) + "...");
server.requestHandler(router::accept).listen(port);
}
示例14: register
import io.vertx.ext.web.Router; //導入依賴的package包/類
/**
* Register this but also register the {@link CorsHandler}. The
* {@link CorsHandler} will deal with the normal CORS headers after it has been
* processed initially by this handler. {@inheritDoc}
*/
@Override
public void register(final Router router) {
router.route().handler(this);
router.route().handler(CorsHandler.create(".+")
.maxAgeSeconds(600)
.allowedMethod(HttpMethod.GET)
.allowedMethod(HttpMethod.POST)
.allowedMethod(HttpMethod.PUT)
.allowedMethod(HttpMethod.DELETE)
.allowedMethod(HttpMethod.OPTIONS)
.allowedHeader("Content-Type")
.allowedHeader("Accept")
.allowedHeader("Accept-Language")
.allowedHeader("Authorization"));
}
示例15: startHttpServer
import io.vertx.ext.web.Router; //導入依賴的package包/類
private Future<Void> startHttpServer() {
Future<Void> future = Future.future();
HttpServer server = vertx.createHttpServer(); // <1>
Router router = Router.router(vertx); // <2>
router.get("/").handler(this::indexHandler);
router.get("/wiki/:page").handler(this::pageRenderingHandler); // <3>
router.post().handler(BodyHandler.create()); // <4>
router.post("/save").handler(this::pageUpdateHandler);
router.post("/create").handler(this::pageCreateHandler);
router.post("/delete").handler(this::pageDeletionHandler);
server
.requestHandler(router::accept) // <5>
.listen(8080, ar -> { // <6>
if (ar.succeeded()) {
LOGGER.info("HTTP server running on port 8080");
future.complete();
} else {
LOGGER.error("Could not start a HTTP server", ar.cause());
future.fail(ar.cause());
}
});
return future;
}