Pure Java minimal http server
When you are working on an API for one reason or another you might need to give it a REST interface, I hade faced this situation a couple of times, there are different ways of approaching this challenge but if you search on the internet most likely you will find options that require adding a couple of extra jars to your project which is not always an option (either because you want to keep it as simple as possible or because of corporative rules)
if what you need is only a simple way of showing data through an http request without adding a ton of jars to your project is creating our own http server with java with what we have available on the JDK
the architecture below
HttpServerDaemon
This class will be in charge of creating the socket that will be waiting for user requests when we are starting the daemon we should bind all the web contexts (all the url which will be dispatched by the server)
HttpDispatcher
As its name states, this class is responsible for replying the requests, the url mapping is done using a hashmap where the key is the url and the value the http handler
Running
and that’s it, just like that we have an http server in java which can server basic content without any dependency, of course, if you need something more sophisticated you can use any of the other alternatives such as JAX-RS, Spark etc
Full source jhttpd