當前位置: 首頁>>代碼示例>>Java>>正文


Java Router類代碼示例

本文整理匯總了Java中io.netty.handler.codec.http.router.Router的典型用法代碼示例。如果您正苦於以下問題:Java Router類的具體用法?Java Router怎麽用?Java Router使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Router類屬於io.netty.handler.codec.http.router包,在下文中一共展示了Router類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createResponse

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
private static HttpResponse createResponse(HttpRequest req, Router<String> router) {
    RouteResult<String> routeResult = router.route(req.method(), req.uri());

    String content =
            "router: \n" + router + "\n" +
            "req: " + req + "\n\n" +
            "routeResult: \n" +
            "target: " + routeResult.target() + "\n" +
            "pathParams: " + routeResult.pathParams() + "\n" +
            "queryParams: " + routeResult.queryParams() + "\n\n" +
            "allowedMethods: " + router.allowedMethods(req.uri());

    FullHttpResponse res = new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            Unpooled.copiedBuffer(content, CharsetUtil.UTF_8)
    );

    res.headers().set(HttpHeaderNames.CONTENT_TYPE,   "text/plain");
    res.headers().set(HttpHeaderNames.CONTENT_LENGTH, res.content().readableBytes());

    return res;
}
 
開發者ID:sinetja,項目名稱:netty-router,代碼行數:23,代碼來源:HttpRouterServerHandler.java

示例2: main

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
    // This is an example router, it will be used at HttpRouterServerHandler.
    //
    // For simplicity of this example, route targets are just simple strings.
    // But you can make them classes, and at HttpRouterServerHandler once you
    // get a target class, you can create an instance of it and dispatch the
    // request to the instance etc.
    Router<String> router = new Router<String>()
        .GET("/",             "Index page")
        .GET("/articles/:id", "Article show page")
        .notFound("404 Not Found");
    System.out.println(router);

    NioEventLoopGroup bossGroup   = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
         .childOption(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE)
         .childOption(ChannelOption.SO_KEEPALIVE, java.lang.Boolean.TRUE)
         .channel(NioServerSocketChannel.class)
         .childHandler(new HttpRouterServerInitializer(router));

        Channel ch = b.bind(PORT).sync().channel();
        System.out.println("Server started: http://127.0.0.1:" + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
 
開發者ID:sinetja,項目名稱:netty-router,代碼行數:34,代碼來源:HttpRouterServer.java

示例3: startWeb

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
public final void startWeb() {
    if (httpget.size() + httppost.size() >0) {
        executor.submit(() -> {
            final String addr = BotanUtils.envToOpt("HTTP_IP_ADDR").orElse("0.0.0.0");
            final int port = Integer.valueOf(BotanUtils.envToOpt("HTTP_PORT").orElse("8080"));
            final Router<Route> router = new Router<>();

            httpget.forEach(router::GET);
            httppost.forEach(router::POST);
            final NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
            final NioEventLoopGroup workerGroup = new NioEventLoopGroup();

            try {
                final ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                        .childOption(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE)
                        .childOption(ChannelOption.SO_KEEPALIVE, java.lang.Boolean.TRUE)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new HttpRouterServerInitializer(router));
                log.info("RESTful API: {}:{}", addr, port);
                final Channel ch = b.bind(addr, port).sync().channel();
                ch.closeFuture().sync();
            } catch (final Exception ignore) {
                //
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        });
    }
}
 
開發者ID:masahitojp,項目名稱:botan-core,代碼行數:32,代碼來源:BotanRouter.java

示例4: createResponse

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
private static HttpResponse createResponse(HttpRequest req, Router<Route> router, String body) {
	final RouteResult<Route> routeResult = router.route(req.method(), req.uri());
	if (routeResult != null) {
		final BotanHttpResponse res = new BotanHttpResponse();
		final Object obj = routeResult.target().handle(new BotanHttpRequest(routeResult, body), res);
		final String content;
		final String type;
		final HttpResponseStatus responseStatus;
		if (obj instanceof BotanHttpResponse) {
			content = ((BotanHttpResponse) obj).content();
			type = ((BotanHttpResponse) obj).type();
			responseStatus = HttpResponseStatus.OK;
		} else if (obj instanceof Integer) {
			responseStatus = HttpResponseStatus.valueOf((int) obj);
			return new DefaultFullHttpResponse(
					HttpVersion.HTTP_1_1, responseStatus);
		} else {
			content = obj.toString();
			type = res.type();
			responseStatus = HttpResponseStatus.OK;
		}
		final FullHttpResponse response = new DefaultFullHttpResponse(
				HttpVersion.HTTP_1_1, responseStatus,
				Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));

		response.headers().set(HttpHeaderNames.CONTENT_TYPE, type);
		response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
		return response;
	} else {
		return new DefaultFullHttpResponse(
				HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
	}
}
 
開發者ID:masahitojp,項目名稱:botan-core,代碼行數:34,代碼來源:HttpRouterServerHandler.java

示例5: HttpRouterServerHandler

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
HttpRouterServerHandler(Router<Route> router) {
	this.router = router;
}
 
開發者ID:masahitojp,項目名稱:botan-core,代碼行數:4,代碼來源:HttpRouterServerHandler.java

示例6: HttpRouterServerInitializer

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
HttpRouterServerInitializer(final Router<Route> router) {
	handler = new HttpRouterServerHandler(router);
}
 
開發者ID:masahitojp,項目名稱:botan-core,代碼行數:4,代碼來源:HttpRouterServerInitializer.java

示例7: HttpRouterServerHandler

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
HttpRouterServerHandler(Router<String> router) {
    this.router = router;
}
 
開發者ID:sinetja,項目名稱:netty-router,代碼行數:4,代碼來源:HttpRouterServerHandler.java

示例8: HttpRouterServerInitializer

import io.netty.handler.codec.http.router.Router; //導入依賴的package包/類
HttpRouterServerInitializer(Router<String> router) {
    handler = new HttpRouterServerHandler(router);
}
 
開發者ID:sinetja,項目名稱:netty-router,代碼行數:4,代碼來源:HttpRouterServerInitializer.java


注:本文中的io.netty.handler.codec.http.router.Router類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。