Java HttpServer无法处理上下文“ /”的请求

时间:2019-11-22 19:34:32

标签: java httpserver

我当前正在设置Java HttpServer。除了一件事,它工作正常: 每当我将HttpContext设置为“ /”时,它就无法处理该请求并说:“ 500 Internal Server Error 没有上下文处理程序。”这是唯一无法正常工作的东西,其他所有东西都工作正常。

private static WOP instance;
private WebServer webServer;

private WOP() {
    webServer = new WebServer(8080);
    HttpContext httpContext = webServer.getHttpServer().createContext("/");
    httpContext.setHandler(this::handleRequest);
    webServer.getHttpServer().start();
}

private void handleRequest(HttpExchange httpExchange) throws IOException {
    String response = "Hi there!";
    httpExchange.sendResponseHeaders(200, response.getBytes().length);//response code and length
    OutputStream os = httpExchange.getResponseBody();
    os.write(response.getBytes());
    os.close();
}

0 个答案:

没有答案