From 21ecb354d9a250b899998a4f3e5288d4653e9068 Mon Sep 17 00:00:00 2001 From: Radhe Sravan Date: Wed, 6 Nov 2019 07:16:40 +0530 Subject: [PATCH] Example for hexagonal architecture in java --- patterns/hexagonal-architecture/.gitignore | 33 ++++++++++++ patterns/hexagonal-architecture/README.md | 24 +++++++++ patterns/hexagonal-architecture/pom.xml | 53 +++++++++++++++++++ .../arch/AddressBookApplication.java | 16 ++++++ .../controller/AddressBookController.java | 32 +++++++++++ .../repository/ContactRepositoryImpl.java | 30 +++++++++++ .../hexagonal/arch/core/domain/Contact.java | 33 ++++++++++++ .../core/service/AddressBookServiceImpl.java | 28 ++++++++++ .../arch/port/repo/ContactRepository.java | 13 +++++ .../arch/port/service/AddressBookService.java | 13 +++++ .../src/main/resources/application.properties | 1 + 11 files changed, 276 insertions(+) create mode 100644 patterns/hexagonal-architecture/.gitignore create mode 100644 patterns/hexagonal-architecture/README.md create mode 100644 patterns/hexagonal-architecture/pom.xml create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java create mode 100644 patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java create mode 100644 patterns/hexagonal-architecture/src/main/resources/application.properties diff --git a/patterns/hexagonal-architecture/.gitignore b/patterns/hexagonal-architecture/.gitignore new file mode 100644 index 0000000000..65069b2e61 --- /dev/null +++ b/patterns/hexagonal-architecture/.gitignore @@ -0,0 +1,33 @@ +target/ +.mvn/ +mvnw +mvnw.cmd +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/patterns/hexagonal-architecture/README.md b/patterns/hexagonal-architecture/README.md new file mode 100644 index 0000000000..94224a737d --- /dev/null +++ b/patterns/hexagonal-architecture/README.md @@ -0,0 +1,24 @@ +# HEXAGONAL ARCHITECTURE - EXAMPLE IN JAVA + +A Spring Boot base web application for address book has been built as part of this project based on the hexagonal architecture guidelines, which allows the user to add a contact ad list all the contacts that are available. + +### Running the application + +The application can be run using maven spring-boot run command + +``` +mvn spring-boot:run + +``` + +New contact can be added through a POST request on http://loalhost:8080/contacts with the body as + +``` +{ + "name": "ABC", + "address": "Address", + "contactNumber": 1234567890 +} + +``` +Existing contacts can be listed using a GET request on the same url http://loalhost:8080/contacts \ No newline at end of file diff --git a/patterns/hexagonal-architecture/pom.xml b/patterns/hexagonal-architecture/pom.xml new file mode 100644 index 0000000000..908eb21c01 --- /dev/null +++ b/patterns/hexagonal-architecture/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.0.RELEASE + + + com.baeldung.hexagonal.arch + hexagonal-architecture + 0.0.1-SNAPSHOT + hexagonal-architecture + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java new file mode 100644 index 0000000000..e20b79f8c6 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/AddressBookApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.hexagonal.arch; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +@SpringBootApplication +@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) +public class AddressBookApplication { + + public static void main(String[] args) { + SpringApplication.run(AddressBookApplication.class, args); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java new file mode 100644 index 0000000000..34566c6b61 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/controller/AddressBookController.java @@ -0,0 +1,32 @@ +package com.baeldung.hexagonal.arch.adapter.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.hexagonal.arch.core.domain.Contact; +import com.baeldung.hexagonal.arch.port.service.AddressBookService; + +@RestController +@RequestMapping("/contacts") +public class AddressBookController { + + @Autowired + private AddressBookService addressBookService; + + @PostMapping + public void addContact(@RequestBody Contact contact) { + addressBookService.createContact(contact); + } + + @GetMapping + public List getAllContacts() { + return addressBookService.getAllContacts(); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java new file mode 100644 index 0000000000..235a899d7c --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/adapter/repository/ContactRepositoryImpl.java @@ -0,0 +1,30 @@ +package com.baeldung.hexagonal.arch.adapter.repository; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.springframework.stereotype.Repository; + +import com.baeldung.hexagonal.arch.core.domain.Contact; +import com.baeldung.hexagonal.arch.port.repo.ContactRepository; + +@Repository +public class ContactRepositoryImpl implements ContactRepository { + + private Map contactsRepo = new HashMap(); + + @Override + public void createContact(Contact contact) { + contactsRepo.put(contact.getName(), contact); + } + + @Override + public List getAllContacts() { + return contactsRepo.values() + .stream() + .collect(Collectors.toList()); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java new file mode 100644 index 0000000000..118657a8db --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/domain/Contact.java @@ -0,0 +1,33 @@ +package com.baeldung.hexagonal.arch.core.domain; + +public class Contact { + + private String name; + private String address; + private int contactNumber; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public int getContactNumber() { + return contactNumber; + } + + public void setContactNumber(int contactNumber) { + this.contactNumber = contactNumber; + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java new file mode 100644 index 0000000000..4a95e5cd72 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/core/service/AddressBookServiceImpl.java @@ -0,0 +1,28 @@ +package com.baeldung.hexagonal.arch.core.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonal.arch.core.domain.Contact; +import com.baeldung.hexagonal.arch.port.repo.ContactRepository; +import com.baeldung.hexagonal.arch.port.service.AddressBookService; + +@Service +public class AddressBookServiceImpl implements AddressBookService { + + @Autowired + ContactRepository contactRepository; + + @Override + public void createContact(Contact contact) { + contactRepository.createContact(contact); + } + + @Override + public List getAllContacts() { + return contactRepository.getAllContacts(); + } + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java new file mode 100644 index 0000000000..a8cc4f73ea --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/repo/ContactRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.hexagonal.arch.port.repo; + +import java.util.List; + +import com.baeldung.hexagonal.arch.core.domain.Contact; + +public interface ContactRepository { + + void createContact(Contact contact); + + List getAllContacts(); + +} diff --git a/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java new file mode 100644 index 0000000000..b91683fbc2 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/arch/port/service/AddressBookService.java @@ -0,0 +1,13 @@ +package com.baeldung.hexagonal.arch.port.service; + +import java.util.List; + +import com.baeldung.hexagonal.arch.core.domain.Contact; + +public interface AddressBookService { + + public void createContact(Contact contact); + + public List getAllContacts(); + +} diff --git a/patterns/hexagonal-architecture/src/main/resources/application.properties b/patterns/hexagonal-architecture/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/patterns/hexagonal-architecture/src/main/resources/application.properties @@ -0,0 +1 @@ +