34 lines
1.1 KiB
Java
34 lines
1.1 KiB
Java
import java.io.OutputStream;
|
|
import java.net.InetSocketAddress;
|
|
import com.sun.net.httpserver.HttpServer;
|
|
|
|
public class Application {
|
|
public static void main(String[] args) throws Exception {
|
|
int serverPort = 8084;
|
|
|
|
// Create an HTTP server and bind it to the specified port
|
|
HttpServer server = HttpServer.create(new InetSocketAddress(serverPort), 0);
|
|
|
|
// Create a context for the "/api/hello" endpoint
|
|
server.createContext("/api/hello", (exchange -> {
|
|
String respText = "Hello world"; // The response text
|
|
|
|
// Send the response headers (HTTP 200 OK, content length of the response)
|
|
exchange.sendResponseHeaders(200, respText.getBytes().length);
|
|
|
|
// Get the response body output stream and write the response to it
|
|
OutputStream outputStream = exchange.getResponseBody();
|
|
outputStream.write(respText.getBytes());
|
|
outputStream.flush();
|
|
|
|
// Close the exchange to finish the request
|
|
exchange.close();
|
|
}));
|
|
|
|
server.setExecutor(null); // Use default executor
|
|
server.start(); // Start the server
|
|
System.out.println("Server started on port " + serverPort);
|
|
}
|
|
}
|
|
|