JDK HTTP Server codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.
This codebase was created to demonstrate a fully fledged fullstack application built with the JDK HTTP Server including CRUD operations, authentication, routing, pagination, and more.
For more information on how to this works with other frontends/backends, head over to the RealWorld repo.
This is deployed here
This is built up of a few components. Primarily
- The
jdk.httpserver
module which provides the API that is programmed against - Jetty which provides the actual backing implementation for
jdk.httpserver
- Postgresql for the database
- RainbowGum for logging
Then, serving specific tasks:
- dev.mccue.jdk.httpserver for providing a
Body
abstraction - dev.mccue.jdk.httpserver.regexrouter for basic request routing
- dev.mccue.json for reading and writing JSON
- dev.mccue.jdk.httpserver.json for using JSON as a
Body
and reading it fromHttpExchange
s - dev.mccue.urlparameters for parsing query params
- dev.mccue.jdbc for
UncheckedSQLException
andSQLFragment
- io.github.cdimascio.dotenv.java for local development
.env
files - slugify for turning text into a url sage slug
- com.zaxxer.hikari for connection pooling
- bcrypt for password salt and hashing
- org.slf4j as a logging facade
Almost all the code is contained in the RealWorldAPI
class. If any of the choices made here offend your sensibilities
I encourage forking and showing the way you would prefer it be done. If you think something is done in a subpar way or
is otherwise objectively broken please open an issue.
Specifically, I would encourage folks to try and
- Split up the
RealWorldAPI
class. Where are the natural boundaries? - Try using their database abstraction of choice. What would this look like with
Hibernate
,JOOQ
, orJDBI
? Would there be fewer or more round trips to the database? - Try using their JSON library of choice.
- Try to do the whole persistence/service/etc. split. Does that make things better?
- Add unit tests. For this exact thing there are already API tests I was able to just use, but how would testing look with JUnit?
- etc.
I personally see a lot of areas for improvement once string templates are real. Counting ?
s in big queries is maybe the biggest
remaining "raw" JDBC shortcoming.
- Java 22 or above
- SDKMan
- Docker
First, start up postgres
$ docker compose up -d
Then install MyBatis Migrations. This is currently easiest to do with SDKMan.
$ sdk install mybatis
Apply the migrations to the database
$ cd migrations
$ migrate up
$ cd ..
Then to run the server either
- open the project in your editor
- run it through maven (
./mvnw exec:java -Dexec.mainClass="dev.mccue.jdk.httpserver.realworld.Main"
) - run it through docker
$ docker build -t realworld .
$ docker run realworld
The .env
file for this project is committed to the repo. Note that in general this is a bad idea/practice, but the
only secrets here are for the local database connection so it's fine.