Example for hexagonal architecture in java
This commit is contained in:
parent
f2d9829b91
commit
21ecb354d9
|
@ -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/
|
|
@ -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
|
|
@ -0,0 +1,53 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.baeldung.hexagonal.arch</groupId>
|
||||
<artifactId>hexagonal-architecture</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>hexagonal-architecture</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Contact> getAllContacts() {
|
||||
return addressBookService.getAllContacts();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, Contact> contactsRepo = new HashMap<String, Contact>();
|
||||
|
||||
@Override
|
||||
public void createContact(Contact contact) {
|
||||
contactsRepo.put(contact.getName(), contact);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Contact> getAllContacts() {
|
||||
return contactsRepo.values()
|
||||
.stream()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Contact> getAllContacts() {
|
||||
return contactRepository.getAllContacts();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Contact> getAllContacts();
|
||||
|
||||
}
|
|
@ -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<Contact> getAllContacts();
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
|
Loading…
Reference in New Issue