{"id":32868373,"url":"https://github.com/ercansormaz/hexagonal-architecture","last_synced_at":"2026-05-10T07:33:06.289Z","repository":{"id":320713554,"uuid":"1082809971","full_name":"ercansormaz/hexagonal-architecture","owner":"ercansormaz","description":"Spring Boot project demonstrating the Hexagonal Architecture","archived":false,"fork":false,"pushed_at":"2025-10-25T11:06:40.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-25T13:07:11.167Z","etag":null,"topics":["hexagonal-architecutre","ports-and-adapters","software-architecture","spring","spring-boot"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ercansormaz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-24T20:02:12.000Z","updated_at":"2025-10-25T11:06:43.000Z","dependencies_parsed_at":"2025-10-25T13:07:18.074Z","dependency_job_id":"c6ce740d-b802-4e96-a856-6fa1b3a2eb36","html_url":"https://github.com/ercansormaz/hexagonal-architecture","commit_stats":null,"previous_names":["ercansormaz/hexagonal-architecture"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/ercansormaz/hexagonal-architecture","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ercansormaz%2Fhexagonal-architecture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ercansormaz%2Fhexagonal-architecture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ercansormaz%2Fhexagonal-architecture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ercansormaz%2Fhexagonal-architecture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ercansormaz","download_url":"https://codeload.github.com/ercansormaz/hexagonal-architecture/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ercansormaz%2Fhexagonal-architecture/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":283475139,"owners_count":26841941,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-11-09T02:00:05.828Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hexagonal-architecutre","ports-and-adapters","software-architecture","spring","spring-boot"],"created_at":"2025-11-09T08:01:04.239Z","updated_at":"2025-11-09T08:02:34.716Z","avatar_url":"https://github.com/ercansormaz.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hexagonal Architecture with Spring Boot — Calculator Example\n\n## 📘 Overview\n\nThis project demonstrates the **Hexagonal Architecture (also known as Ports and Adapters\nArchitecture)** using **Spring Boot** through a simple yet educational example — a calculator that\nperforms basic arithmetic operations (**addition**, **subtraction**, **multiplication**, **division**)\nand logs each calculation to a database.\n\nThe main goal of this project is **not** the business logic itself, but rather to show **how to\ndesign clean, maintainable, and testable software** by isolating the core domain logic from\nframeworks, external dependencies, and I/O operations.\n\n---\n\n## 🧱 Project Structure\n\nThe project is organized as a **multi-module Maven project** to clearly separate architectural\nlayers and dependencies:\n\n```\nhexagonal-architecture\n ├── domain\n ├── application\n ├── infrastructure\n ├── rest-api-adapter\n └── soap-service-adapter\n```\n\n---\n\n## ⚙️ Domain Module\n\nThe `domain` module represents the **core business logic** of the application.\nIt contains only **pure Java classes**, with **no framework dependencies**.\n\n- `model`\n    - `Calculation` — represents a mathematical operation.\n    - `CalculationType` — enum for operation types (ADD, SUBTRACT, MULTIPLY, DIVIDE).\n- `port.in`\n    - `CalculateUseCase` — defines the input port for calculation use cases.\n- `port.out`\n    - `CalculationLogger` — defines the output port for persisting calculation logs.\n- `service`\n    - `CalculatorService` — performs arithmetic operations.\n\n**✅ Key Point:**\nThe domain layer knows nothing about frameworks, databases, or APIs — it only defines *what* needs\nto be done, not *how*.\n\n---\n\n## 🧩 Application Module\n\nThe `application` module implements the use cases defined in the domain layer.\n\n- `usecase`\n    - `CalculateUseCaseImpl` — implements the `CalculateUseCase` port and uses `CalculatorService`.\n\nThis module depends **only** on the `domain` module and orchestrates the interaction between domain\nlogic and external adapters.\n\n---\n\n## 🏗️ Infrastructure Module\n\nThe `infrastructure` module provides database access and persistence logic.\n\n- `model`\n    - `CalculationEntity` — database entity for calculations.\n- `repository`\n    - `CalculationRepository` — Spring Data JPA repository.\n- `adapter`\n    - `CalculationLoggerJpaAdapter` — implements `CalculationLogger` port using JPA.\n\nThis module depends only on the **domain** module, adhering to the dependency rule:\n\n\u003e **Outer layers depend on inner layers, but not vice versa.**\n\n---\n\n## 🌐 REST API Adapter\n\nThe `rest-api-adapter` module exposes RESTful endpoints for performing calculations.\n\n- `dto`\n    - `CalculationRequest` and `CalculationResponse` — request/response DTOs.\n- `config`\n    - `BeanConfig` — registers `CalculatorService` and `CalculateUseCaseImpl` as Spring\n      beans.\n- `controller`\n    - `CalculatorRestController` — provides endpoints for the four basic operations.\n\nThis module depends on `application` and `infrastructure` modules.\n\n---\n\n## 🧼 SOAP Service Adapter\n\nThe `soap-service-adapter` module exposes the same functionality as SOAP web services.\n\n- `dto`\n    - `CalculationRequest`, `CalculationResponse`\n- `config`\n    - `BeanConfig` — defines beans for domain/application services.\n- `service`\n    - `CalculatorSoapService` — SOAP endpoint definition.\n\nLike the REST adapter, it depends on `application` and `infrastructure` modules.\n\n--- \n\n## 💡 Design Highlights\n\n- ✅ **Pure Java Domain**: No Spring or persistence dependencies in core logic.\n- ✅ **Multi-module structure**: Clearly separates concerns for better readability and understanding.\n- ✅ **Framework-agnostic domain layer**: Easy to replace REST with SOAP or gRPC without changing\n  core logic.\n- ✅ **Testability**: Domain and application logic can be unit-tested independently from the\n  infrastructure.\n- ✅ **Educational purpose**: The project intentionally uses a simple use case (calculator) to focus\n  on architecture clarity.\n\n---\n\n## 🧠 Key Takeaways\n\nHexagonal Architecture encourages you to:\n\n- Design your system **from the inside out** (domain first).\n- Treat frameworks and tools as **details**, not as the foundation.\n- Achieve **independent**, **pluggable adapters** (REST, SOAP, DB, CLI, etc.).\n- Make your application **resilient to change** — whether it’s a new protocol, a different database,\n  or a new interface.\n\n---\n\n## 🚀 Running the Project\n\nTo run the REST adapter:\n\n```bash\ncd rest-api-adapter\nmvn spring-boot:run\n```\n\nTo run the SOAP adapter:\n\n```bash\ncd soap-service-adapter\nmvn spring-boot:run\n```\n\nBoth adapters use the same domain and application logic, showcasing the **plug-and-play** nature of\nHexagonal Architecture.\n\n---\n\n## 🔹 Usage Examples\n\n### REST API\n\n**Addition**\n```bash\ncurl --location 'http://localhost:8080/rest/calculator/add' \\\n--header 'Content-Type: application/json' \\\n--data '{\n  \"operand1\": 10,\n  \"operand2\": 5\n}'\n```\n**Response**\n```json\n{\n  \"result\": 15.0\n}\n```\n\n### SOAP Service\n\n**Addition**\n```bash\ncurl --location 'http://localhost:8080/SOAP/CalculatorService' \\\n--header 'Content-Type: text/xml' \\\n--data '\u003csoapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cal=\"https://ercan.dev/poc/hexagonal-architecture/calculator\"\u003e\n  \u003csoapenv:Header/\u003e\n  \u003csoapenv:Body\u003e\n    \u003ccal:add\u003e\n      \u003crequest\u003e\n        \u003coperand1\u003e10\u003c/operand1\u003e\n        \u003coperand2\u003e5\u003c/operand2\u003e\n      \u003c/request\u003e\n    \u003c/cal:add\u003e\n  \u003c/soapenv:Body\u003e\n\u003c/soapenv:Envelope\u003e'\n```\n**Response**\n```xml\n\u003cS:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"\u003e\n  \u003cS:Body\u003e\n    \u003cns2:addResponse xmlns:ns2=\"https://ercan.dev/poc/hexagonal-architecture/calculator\"\u003e\n      \u003cresponse\u003e\n        \u003cresult\u003e15.0\u003c/result\u003e\n      \u003c/response\u003e\n    \u003c/ns2:addResponse\u003e\n  \u003c/S:Body\u003e\n\u003c/S:Envelope\u003e\n```\n\n---\n\n## 🎯 Conclusion\n\nThis project serves as a **hands-on example** for understanding Hexagonal Architecture with **Spring\nBoot**.  \nIt demonstrates how clean modularization allows multiple interfaces (REST, SOAP) to coexist while\nkeeping the domain logic independent and reusable.\n\n---\n\n## 📚 Further Reading\nYou can read a detailed explanation of this project in the blog post here:  \n👉 [Read the Blog Post](https://ercan.dev/blog/notes/spring-boot-hexagonal-architecture-example)\n\n\n---\n\n## 🤝 Contributing\n\nContributions are welcome! Feel free to fork the repo, submit pull requests or open issues.\n\n---\n\n## 📜 License\n\nThis project is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fercansormaz%2Fhexagonal-architecture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fercansormaz%2Fhexagonal-architecture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fercansormaz%2Fhexagonal-architecture/lists"}