无法通过Map迭代在Grizzly中创建HttpHandler

时间:2018-08-13 07:03:57

标签: java httphandler httpserver grizzly

我通过将它们放置到Map中来创建许多HttpHandler。

我有这段代码 Handler.java

public class Handler {
    private String path;
    private HttpHandler httpHandler;

    public Handler(String path, HttpHandler httpHandler) {
        this.path = path;
        this.httpHandler = httpHandler;
    }

    public String getPath() {
        return path;
    }

    public HttpHandler getHttpHandler() {
        return httpHandler;
    }   
}

在另一个运行 HttpServer 的类中,我具有以下内容:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;

public class SummerFunApplication {
    private HttpServer server;
    private Map<String, List<Handler>> handlers = new HashMap<>();

    public void get(String path, HttpHandler httpHandler) {
        if (!handlers.containsKey("GET")) {
            handlers.put("GET", new ArrayList<>());
        }

        Handler handler = new Handler(path, httpHandler);       
        handlers.get("GET").add(handler);
    }

    public void run() {
        server = HttpServer.createSimpleServer();

        if (!handlers.isEmpty()) {
            for (Map.Entry<String, List<Handler>> handlerMap : handlers.entrySet()) {
                for (Handler handler : handlerMap.getValue()) {
                    server.getServerConfiguration().addHttpHandler(handler.getHttpHandler(), handler.getPath());
                }
            }
        }

        server.getServerConfiguration().addHttpHandler(new HttpHandler() {

            @Override
            public void service(Request request, Response response) throws Exception {
                String hello = "hello";
                response.setContentType("text/plain");
                response.setContentLength(hello.length());
                response.getWriter().write(hello);
            }
        }, "/hello");

        try {
            server.start();
            System.out.println("Press any key to stop the server...");
            System.in.read();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}

最后,

import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;

public class App {
    public static void main(String[] args) {
        SummerFunApplication app = new SummerFunApplication();

        app.get("/", new HttpHandler() {            
            @Override
            public void service(Request request, Response response) throws Exception {
                String hi = "hi";
                response.setContentType("text/plain");
                response.setContentLength(hi.length());
                response.getWriter().write(hi);
            }
        });

        app.run();
    }
}

在两个处理程序中, / hello 有效,但 / 无效。试图添加另一个类似于 / hello 的处理程序,将其放在 / hello 之后,它也可以工作。我确定 private Map>处理程序=新的HashMap <>(); 包含 / 的处理程序。我确实调试了它,就在那里。我的调试过程是这样的:

if (!handlers.isEmpty()) {
            for (Map.Entry<String, List<Handler>> handlerMap : handlers.entrySet()) {
                for (Handler handler : handlerMap.getValue()) {
                    server.getServerConfiguration().addHttpHandler(handler.getHttpHandler(), handler.getPath());
                }
            }
        }

我想知道通过迭代方式添加时处理程序不会合计。任何帮助,将不胜感激。谢谢。

0 个答案:

没有答案
相关问题