Skip to content

This repository contains a sample Java REST application implemented according to hexagonal architecture.

Notifications You must be signed in to change notification settings

SvenWoltmann/hexagonal-architecture-java

Repository files navigation

Hexagonal Architecture in Java Tutorial

Build Coverage Maintainability Rating Reliability Rating Security Rating

This repository contains a sample Java REST application implemented according to hexagonal architecture.

It is part of the HappyCoders tutorial series on Hexagonal Architecture:

Branches

main

In the main branch, you'll find the application implemented without an application framework. It's only using:

without-jpa-adapters

In the without-jpa-adapters branch, you'll find the application implemented without an application framework and without JPA adapters. It's only using RESTEasy and Undertow.

with-quarkus

In the with-quarkus branch, you'll find an implementation using Quarkus as application framework.

with-spring

In the with-quarkus branch, you'll find an implementation using Spring as application framework.

Architecture Overview

The source code is separated into four modules:

  • model - contains the domain model
  • application - contains the domain services and the ports of the hexagon
  • adapters - contains the REST, in-memory and JPA adapters
  • boostrap - contains the configuration and bootstrapping logic

The following diagram shows the hexagonal architecture of the application along with the source code modules:

Hexagonal Architecture Modules

The model module is not represented as a hexagon because it is not defined by the Hexagonal Architecture. Hexagonal Architecture leaves open what happens inside the application hexagon.

How to Run the Application

The easiest way to run the application is to start the main method of the Launcher class (you'll find it in the boostrap module) from your IDE.

You can use one of the following VM options to select a persistence mechanism:

  • -Dpersistence=inmemory to select the in-memory persistence option (default)
  • -Dpersistence=mysql to select the MySQL option

If you selected the MySQL option, you will need a running MySQL database. The easiest way to start one is to use the following Docker command:

docker run --name hexagon-mysql -d -p3306:3306 \
    -e MYSQL_DATABASE=shop -e MYSQL_ROOT_PASSWORD=test mysql:8.1

The connection parameters for the database are hardcoded in RestEasyUndertowShopApplication.initMySqlAdapter(). If you are using the Docker container as described above, you can leave the connection parameters as they are. Otherwise, you may need to adjust them.

Example Curl Commands

The following curl commands assume that you have installed jq, a tool for pretty-printing JSON strings.

Find Products

The following queries return one and two results, respectively:

curl localhost:8080/products/?query=plastic | jq
curl localhost:8080/products/?query=monitor | jq

The response of the second query looks like this:

[
  {
    "id": "K3SR7PBX",
    "name": "27-Inch Curved Computer Monitor",
    "price": {
      "currency": "EUR",
      "amount": 159.99
    },
    "itemsInStock": 24081
  },
  {
    "id": "Q3W43CNC",
    "name": "Dual Monitor Desk Mount",
    "price": {
      "currency": "EUR",
      "amount": 119.9
    },
    "itemsInStock": 1079
  }
]

Get a Cart

To show the cart of user 61157 (this cart is empty when you begin):

curl localhost:8080/carts/61157 | jq

The response should look like this:

{
  "lineItems": [],
  "numberOfItems": 0,
  "subTotal": null
}

Adding Products to a Cart

Each of the following commands adds a product to the cart and returns the contents of the cart after the product is added (note that on Windows, you have to replace the single quotes with double quotes):

curl -X POST 'localhost:8080/carts/61157/line-items?productId=TTKQ8NJZ&quantity=20' | jq
curl -X POST 'localhost:8080/carts/61157/line-items?productId=K3SR7PBX&quantity=2' | jq
curl -X POST 'localhost:8080/carts/61157/line-items?productId=Q3W43CNC&quantity=1' | jq
curl -X POST 'localhost:8080/carts/61157/line-items?productId=WM3BPG3E&quantity=3' | jq

After executing two of the four commands, you can see that the cart contains the two products. You also see the total number of items and the sub-total:

{
  "lineItems": [
    {
      "productId": "TTKQ8NJZ",
      "productName": "Plastic Sheeting",
      "price": {
        "currency": "EUR",
        "amount": 42.99
      },
      "quantity": 20
    },
    {
      "productId": "K3SR7PBX",
      "productName": "27-Inch Curved Computer Monitor",
      "price": {
        "currency": "EUR",
        "amount": 159.99
      },
      "quantity": 2
    }
  ],
  "numberOfItems": 22,
  "subTotal": {
    "currency": "EUR",
    "amount": 1179.78
  }
}

This will increase the number of plastic sheetings to 40:

curl -X POST 'localhost:8080/carts/61157/line-items?productId=TTKQ8NJZ&quantity=20' | jq

Producing an Error Message

Trying to add another 20 plastic sheetings will result in error message saying that there are only 55 items in stock:

curl -X POST 'localhost:8080/carts/61157/line-items?productId=TTKQ8NJZ&quantity=20' | jq

This is how the error response looks like:

{
  "httpStatus": 400,
  "errorMessage": "Only 55 items in stock"
}

Emptying the Cart

To empty the cart, send a DELETE command to its URL:

curl -X DELETE localhost:8080/carts/61157

To verify it's empty:

curl localhost:8080/carts/61157 | jq

You'll see an empty cart again.


Additional Resources


Java Versions PDF Cheat Sheet

Stay up-to-date with the latest Java features with this PDF Cheat Sheet!

Java Versions PDF Cheat Sheet Mockup

  • Avoid lengthy research with this concise overview of all Java versions up to Java 23.
  • Discover the innovative features of each new Java version, summarized on a single page.
  • Impress your team with your up-to-date knowledge of the latest Java version.

👉 Download the Java Versions PDF

(Hier geht's zur deutschen Version → Java-Versionen PDF)


The Big O Cheat Sheet

With this 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.

Big O PDF Cheat Sheet Mockup

  • Always choose the most efficient data structures and thus increase the performance of your applications.
  • Be prepared for technical interviews and confidently present your algorithm knowledge.
  • Become a sought-after problem solver and be known for systematically tackling complex problems.

👉 Download the Big O Cheat Sheet

(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)


HappyCoders Newsletter

👉 Want to level up your Java skills? Sign up for the HappyCoders newsletter and get regular tips on programming, algorithms, and data structures!

(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)


🇩🇪 An alle Java-Programmierer, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen

Trage dich jetzt auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!

Mastering Data Structures Mockup

👉 Zur Warteliste

About

This repository contains a sample Java REST application implemented according to hexagonal architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages