diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 53af0de315..8b967c71f8 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -73,6 +73,7 @@ spring-boot-actuator spring-boot-data-2 spring-boot-react + hex-architecture diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/.gitignore b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/.gitignore new file mode 100644 index 0000000000..549e00a2a9 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### 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/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/pom.xml b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/pom.xml new file mode 100644 index 0000000000..04e0ba1a79 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.0 + + + com.baeldung + hex-architecture + 0.0.1-SNAPSHOT + hex-architecture + Product store for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/HexagonalApplication.java b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/HexagonalApplication.java new file mode 100644 index 0000000000..84a6219302 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/HexagonalApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.hexarchitecture.productstore; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HexagonalApplication { + + public static void main(String[] args) { + SpringApplication.run(HexagonalApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/adapters/persistence/ProductServiceAdapter.java b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/adapters/persistence/ProductServiceAdapter.java new file mode 100644 index 0000000000..4d22a738c3 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/adapters/persistence/ProductServiceAdapter.java @@ -0,0 +1,27 @@ +package com.baeldung.hexarchitecture.productstore.adapters.persistence; + +import com.baeldung.hexarchitecture.productstore.domain.model.Product; +import com.baeldung.hexarchitecture.productstore.ports.ProductDatabasePort; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +@Service +public class ProductServiceAdapter implements ProductDatabasePort { + + private Map productDataBase = new HashMap<>(); + + @Override + public void addProduct(Product product) { + String id = UUID.randomUUID().toString(); + product.setId(id); + productDataBase.put(id, product); + } + + @Override + public Product getProduct(String id) { + return productDataBase.get(id); + } +} diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/adapters/rest/controller/ProductControllerAdapter.java b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/adapters/rest/controller/ProductControllerAdapter.java new file mode 100644 index 0000000000..ee2dda9a18 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/adapters/rest/controller/ProductControllerAdapter.java @@ -0,0 +1,28 @@ +package com.baeldung.hexarchitecture.productstore.adapters.rest.controller; + +import com.baeldung.hexarchitecture.productstore.adapters.persistence.ProductServiceAdapter; +import com.baeldung.hexarchitecture.productstore.domain.model.Product; +import com.baeldung.hexarchitecture.productstore.ports.ProductUserInterfacePort; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/product/") +public class ProductControllerAdapter implements ProductUserInterfacePort { + + @Autowired + private ProductServiceAdapter productServiceAdapter; + + @Override + public void addProduct(@RequestBody Product product) { + productServiceAdapter.addProduct(product); + } + + @Override + public Product getProduct(@PathVariable String id) { + return productServiceAdapter.getProduct(id); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/domain/model/Product.java b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/domain/model/Product.java new file mode 100644 index 0000000000..3876628e8a --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/domain/model/Product.java @@ -0,0 +1,16 @@ +package com.baeldung.hexarchitecture.productstore.domain.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@AllArgsConstructor +@NoArgsConstructor +@Setter +public class Product { + + String id; + String name; + String description; +} diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/ports/ProductDatabasePort.java b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/ports/ProductDatabasePort.java new file mode 100644 index 0000000000..9562408a67 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/ports/ProductDatabasePort.java @@ -0,0 +1,10 @@ +package com.baeldung.hexarchitecture.productstore.ports; + +import com.baeldung.hexarchitecture.productstore.domain.model.Product; + +public interface ProductDatabasePort { + + void addProduct(Product product); + + Product getProduct(String id); +} diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/ports/ProductUserInterfacePort.java b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/ports/ProductUserInterfacePort.java new file mode 100644 index 0000000000..ab5d9e13d5 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/java/com/baeldung/hexarchitecture/productstore/ports/ProductUserInterfacePort.java @@ -0,0 +1,16 @@ +package com.baeldung.hexarchitecture.productstore.ports; + +import com.baeldung.hexarchitecture.productstore.domain.model.Product; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +public interface ProductUserInterfacePort { + + @PostMapping + void addProduct(@RequestBody Product product); + + @GetMapping("{id}") + Product getProduct(@PathVariable String id); +} diff --git a/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/resources/application.properties b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/resources/application.properties new file mode 100644 index 0000000000..6a050feaa3 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexarchitecture/hex-architecture/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration