Initial upload opcua service
This commit is contained in:
66
main/java/de/opcua/app/test/MinimalRestTest.java
Normal file
66
main/java/de/opcua/app/test/MinimalRestTest.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package de.opcua.app.test;
|
||||
|
||||
import com.sun.net.httpserver.*;
|
||||
import java.io.*;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
* Minimal test to verify HttpServer works
|
||||
* Run with: mvn exec:java -Dexec.mainClass="de.opcua.app.test.MinimalRestTest"
|
||||
*/
|
||||
public class MinimalRestTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
System.out.println("Creating HTTP Server on port 8081...");
|
||||
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(8081), 0);
|
||||
|
||||
server.createContext("/", exchange -> {
|
||||
String html = "<html><body><h1>It Works!</h1><p>REST API is running on port 8081</p></body></html>";
|
||||
byte[] bytes = html.getBytes("UTF-8");
|
||||
|
||||
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8");
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
exchange.getResponseBody().write(bytes);
|
||||
exchange.getResponseBody().close();
|
||||
});
|
||||
|
||||
server.createContext("/test", exchange -> {
|
||||
String json = "{\"status\":\"ok\",\"message\":\"Test endpoint works!\"}";
|
||||
byte[] bytes = json.getBytes("UTF-8");
|
||||
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
exchange.getResponseBody().write(bytes);
|
||||
exchange.getResponseBody().close();
|
||||
});
|
||||
|
||||
server.start();
|
||||
|
||||
System.out.println("✅ HTTP Server started successfully!");
|
||||
System.out.println("✅ Open in browser: http://localhost:8081/");
|
||||
System.out.println("✅ Test endpoint: http://localhost:8081/test");
|
||||
System.out.println("");
|
||||
System.out.println("Test with curl:");
|
||||
System.out.println(" curl http://localhost:8081/");
|
||||
System.out.println(" curl http://localhost:8081/test");
|
||||
System.out.println("");
|
||||
System.out.println("Press Ctrl+C to stop...");
|
||||
|
||||
Thread.sleep(Long.MAX_VALUE);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("❌ Failed to start server!");
|
||||
System.err.println("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
|
||||
System.err.println("");
|
||||
System.err.println("Possible causes:");
|
||||
System.err.println("1. Port 8081 is already in use");
|
||||
System.err.println(" Check with: lsof -i :8081");
|
||||
System.err.println("2. Firewall is blocking the port");
|
||||
System.err.println("3. Insufficient permissions");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user