Here's a breakdown of the different project components:
src/main/java
: The source code for the project, organized by package:eu.ammbra.bday.Organizer.java
: The main entry point of the application, possibly responsible for coordinating the overall flow of the program.eu.ammbra.bday.details
: A package containing domain model classes. These classes represent entities involved in birthday celebrations, such as cakes, parties, and people attending them.eu.ammbra.bday.handlers
: A package containing classes responsible for handling specific tasks.eu.ammbra.bday.operations
: A package containing classes responsible for handling the operational side of parties.eu.ammbra.bday.store
: A package containing classes responsible for interacting with data from files.
src/main/resources/store/events.json
: A resource file containing sample event data in JSON format.src/main/resources/static
: A folder holding client-side related static files.src/test/java
: The source code for unit tests and integration tests.src/snippets
: The JSON snippets folder.
JDK 18 introduced the SimpleFileServer
API and added it to the jdk.httpserver
module. This simple web server is a minimal HTTP static file server, designed to be used for prototyping, testing, and debugging.
You can run the simple web server in the command line with the jwebserver
JDK tool.
Jwebserver serves static files in a single directory hierarchy over HTTP/1.1; dynamic content and other HTTP versions are not supported.
Often times, front-end development occurs in parallel with back-end efforts. Imagine the following scenario:
- you work on a Java API but you need to know how your colleagues working on front-end expect data from your API
- in the same time, your colleagues develop HTML, CSS, javascript files and need to be aware on how your API endpoints and data served by them would look like.
To improve productivity of teams that work with different programming languages (Java, javascript),
would be great if both teams can access each others work without repetitive deployment. jwebserver
can help you achieve this simple setup.
Your task relies on you to do the following steps:
- Launch the
Organizer.java
application in a terminal window. - Open another terminal window and place yourself in
C_bday_jwebserver/src/main/resources/static
folder. - Launch
jwebserver
by runningjwebserver
command. - Visit the link
jwebserver
output suggests and check if it connects to your backend API. - While
jwebserver
still runs, modify theC_bday_jwebserver/src/main/resources/static/index.html
file with:
<p>Explore details of party number 1 <a href=":8081/api/organize/1">/api/organize/1</a></p>
- Refresh the browser window to see if the change was taken into account.
⚠️ If your terminal is unaware ofjwebserver
, you may invoke it through$JAVA_HOME/bin/jwebserver
. All JDK tools are executable from JDK'sbin
folder.
In addition to a command-line tool, the Simple Web Server provides an API for programmatic creation and customization of the server and its components.
This API extends the com.sun.net.httpserver
package. Moreover, the HttpServer
API is capable of working with multiple handlers and sometimes
is possible to combine a file handler with a canned response handler
Let's evolve the previous scenario by integrating the client-side code. Your task relies on you to do the following steps:
- Modify the code of
C_bday_jwebserver/src/main/java/eu/ammbra/bday/Organizer.java
to handle the content insidestatic
folder.
Path staticDir = Paths.get(Organizer.class.getResource("/static").toURI());
var fileHandler = SimpleFileServer.createFileHandler(staticDir);
//serve the static files
server.createContext("/", fileHandler);
System.out.printf("Birthday Party Server is running at http://127.0.0.1:%d%n", port);
- Launch the previously modified
Organizer.java
application in a terminal window. Now access the server running on http://127.0.0.1:8081.
The HttpClient
API was added in Java 11 and you can use it to request HTTP resources over the network.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://openjdk.org/"))
.build();
client.send(request, HttpResponse.BodyHandlers.ofString());
In this activity you will leverage the HttpClient
API to create integration tests for our setup:
- Go to the
src/test/java/eu/ammbra/bday/OrganizerIntegrationTest.java
file. - The
setup()
andtearDown()
methods are already prepared for you and those should not be changed. - Fix all the tests so they can assess correctly the application.