# The JDK built-in web server with Apache Groovy

> Master using the JDK's built-in HTTP server with Apache Groovy to quickly create convenient mock services and test APIs.

Author: [Guillaume Laforge](https://glaforge.dev/) | Published: 2017-11-20 | Topics: groovy, java | Canonical: https://glaforge.dev/posts/2017/11/20/the-jdk-builtin-web-server-with-apache-groovy/


In my timeline, I saw a tweet from Joe Walnes about the built-in HTTP server available in the JDK since Java 6. It's super convenient, starts super fast, easy to use, but I often forget about it. I'd probably not use it for serving planet-wide load, but it's very useful when you need to create a quick service, a little mock for testing some web or micro-service.

Here's a little hello world for the fun.

I'm taking advantage of Apache Groovy's closure-to-functional-interface coercion support, as well as the `with{}` method to reuse the `HttpServer` instance for two method calls on the same instance (I could've used it for the `http` variable as well, actually).

```groovy
import com.sun.net.httpserver.HttpServer

HttpServer.create(new InetSocketAddress(8080), 0).with {
    createContext("/hello") { http ->
        http.responseHeaders.add("Content-type", "text/plain")
        http.sendResponseHeaders(200, 0)
        http.responseBody.withWriter { out ->
            out << "Hello ${http.remoteAddress.hostName}!"
        }
    }
    start()
}
```
