Take external requests (HTTP REST controllers, CLI inputs, Kafka consumers) and translate them into domain commands.
Because the domain layer has zero infrastructure dependencies, you can write blistering-fast unit tests using plain JUnit 5 or Mockito. You do not need to boot up a slow Spring Application Context ( @SpringBootTest ).
: Define the operations the application offers (e.g., a PlaceOrder service interface).
Note: This article is for educational purposes. Always respect copyright laws and intellectual property when downloading digital assets. Take external requests (HTTP REST controllers, CLI inputs,
// Domain Entity (Pure Java) public class User private UUID id; private String username; private String email; // Business logic methods, not just getters/setters public void changeEmail(String newEmail) // Validate domain rules this.email = newEmail; Use code with caution.
├── model/ # (Pure Java) Contains domain entities and value objects ├── application/ # (Pure Java) Contains the domain services and ports ├── adapters/ # (Framework-aware) Contains REST, JPA, and in-memory adapters └── bootstrap/ # (Framework-aware) Contains configuration and startup logic
Hexagonal Architecture is an architectural pattern that was first introduced by Alistair Cockburn in 2005. The pattern is called "hexagonal" because it visualizes the architecture as a hexagon with the core business logic at its center. The hexagon is surrounded by ports and adapters that interact with the outside world. : Define the operations the application offers (e
If you legally obtained a clean copy (e.g., from a publisher’s promo or an author’s GitHub release), this book is a very practical guide for intermediate Java developers who want to move beyond layered architectures. Just don’t risk malware or legal issues from shady “free download” sites – check your local library’s digital lending or a subscription like O’Reilly Safari instead.
package com.example.order.adapters.inbound; import com.example.order.domain.Order; import com.example.order.ports.inbound.CreateOrderUseCase; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; @RestController @RequestMapping("/orders") public class OrderRestController private final CreateOrderUseCase createOrderUseCase; public OrderRestController(CreateOrderUseCase createOrderUseCase) this.createOrderUseCase = createOrderUseCase; @PostMapping public ResponseEntity create(@RequestParam BigDecimal amount) return ResponseEntity.ok(createOrderUseCase.createOrder(amount)); Use code with caution.
: A research paper by Chavez, M., & Park, Y. that explores the implementation of these principles in serverless environments. // Domain Entity (Pure Java) public class User
Interfaces exposed by the domain to allow external actors to trigger business logic. Use cases and service interfaces are driving ports.
Define how the domain interacts with the outside world (e.g., saving data, sending an email). Adapters (The Implementation)
: Designing Hexagonal Architecture with Java (Packt) .
[ HTTP / REST ] ---> ( Port ) | [ Core Logic ] ---> ( Port ) ---> [ Database ] | [ CLI / Test ] ---> ( Port ) The Three Core Components
Invented by Alistair Cockburn and published in 2005, Hexagonal Architecture was created to avoid structural pitfalls in object-oriented design, such as unwanted dependencies between layers and the contamination of user interface code with business logic. Its core philosophy is to isolate an application's "core" from the outside world.