вы можете использовать внутренний сервер Sun http, включенный в Java, для анализа http-запроса и построения ответа
import com.sun.net.httpserver.*
import java.util.concurrent.Executors
import groovy.json.*
public class Web2{
private static int HTTP_SERVER_PORT=8000;
public static void log(String s){
println "${new Date().format('yyyy-MM-dd HH:mm:ss.SSS')} $s"
}
public static void main(String[]arg){
try {
InetSocketAddress addr = new InetSocketAddress(HTTP_SERVER_PORT);
def httpServer = com.sun.net.httpserver.HttpServer.create(addr, 0)
httpServer.with {
createContext('/', new MyHttpHandler())
setExecutor(Executors.newCachedThreadPool())
start()
log "server started.";
log "endpoint: http://${java.net.InetAddress.getLocalHost().getHostName()}:$HTTP_SERVER_PORT/"
}
}catch(e){
log "Error starting server:\n\t$e\n"
log "press any key to continue..."
System.in.read()
}
}
static class MyHttpHandler implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException{
def id = Long.toString(httpExchange.hashCode(),36).padLeft(8,'0')
try {
log "[$id] handle - start";
String requestMethod = httpExchange.getRequestMethod();
log "[$id] method : $requestMethod"
log "[$id] headers: ${httpExchange.getRequestHeaders().toString()}"
if (requestMethod.equalsIgnoreCase("GET")) {
Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.set("Content-Type", "application/json");
httpExchange.sendResponseHeaders(200, 0);
httpExchange.getResponseBody().withWriter("UTF-8"){w->
def respData = httpExchange.getRequestHeaders() //just example as json data to send back
new JsonBuilder(respData).writeTo(w)
}
} else {
throw new Exception("not supported")
}
httpExchange.close();
log "[$id] response sent";
}catch(Throwable e){
log "[$id] $e"
try{
httpExchange.sendResponseHeaders(500, 0);
responseHeaders.set("Content-Type", "text/plain");
}catch(ei){}
httpExchange.getResponseBody().write(e.toString().getBytes());
}
}
}
}