我当前正在设置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();
}