Created
April 4, 2019 14:35
-
-
Save lsiu/979645f54d4af85b446a4a01cd2c32eb to your computer and use it in GitHub Desktop.
HttpServer from JDK since 1.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.net.InetSocketAddress; | |
import com.sun.net.httpserver.HttpServer; | |
public class WebApp { | |
public static void main(String[] args) throws Exception { | |
HttpServer server = HttpServer.create(new InetSocketAddress(9394), 0); | |
server.createContext("/ping").setHandler(h -> { | |
final String response = "pong!"; | |
h.sendResponseHeaders(200, response.getBytes().length); | |
try (OutputStream os = h.getResponseBody()) { | |
os.write(response.getBytes()); | |
} | |
}); | |
server.createContext("/shutdown").setHandler(h -> server.stop(0)); | |
server.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment