diff --git a/spring-boot-modules/spring-boot-microservices/README.md b/spring-boot-modules/spring-boot-microservices/README.md index d2008c51fc..a85d52b2bc 100644 --- a/spring-boot-modules/spring-boot-microservices/README.md +++ b/spring-boot-modules/spring-boot-microservices/README.md @@ -1,13 +1,11 @@ --------------------------------- CustomerOrderApp installation steps---------------------------------- -1. Clone or download sample project from GitHub repo: https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/customer-order-app -2. Unzip project folder to local disk for example to: C:/baeldung-tutorials/customer-order-app +1. Clone or download sample project from GitHub repo: https://github.com/eugenp/tutorials/tree/master/spring-boot-modules/spring-boot-microservices +2. Unzip project folder to local disk for example to: C:/baeldung-tutorials/spring-boot-microservices/customer-order-app 3. Run `mvn clean install -DskipTests=true` -4. Navigate to payment-service/payment-server module folder and type `mvn spring-boot:run` -5. Open another CMD PROMPT window. - Navigate to order-service/order-server module folder and type `mvn spring-boot:run` -6. Open another CMD PROMPT window. +4. Navigate to order-service/order-server module folder and type `mvn spring-boot:run` +5. Open another CMD PROMPT window. Navigate to customer-service module folder and type `mvn spring-boot:run` -7. Launch the Postman application from your machine and import the collection of POST requests located in the _postman_ folder - +6. Launch the Postman application from your machine and import the collection located in the _postman_ folder in project root +7. Verify successful request from the POSTMAN to http://localhost:8001/customer-service/order diff --git a/spring-boot-modules/spring-boot-microservices/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java b/spring-boot-modules/spring-boot-microservices/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java index 5ad66e1052..09d72e63f4 100644 --- a/spring-boot-modules/spring-boot-microservices/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java +++ b/spring-boot-modules/spring-boot-microservices/order-service/order-server/src/main/java/com/baeldung/orderservice/OrderService.java @@ -17,14 +17,11 @@ import org.springframework.web.bind.annotation.RestController; import java.util.Arrays; import java.util.Date; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; @RestController public class OrderService { - @Autowired - private PaymentClient paymentClient; private List orders = Arrays.asList( @@ -57,19 +54,4 @@ public class OrderService { return new OrderResponse(order.getId(), order.getItemId(), "CREATED"); } - @PostMapping("/pay/{orderNumber}") - public PaymentResponse sendPayment(@PathVariable String orderNumber, @RequestBody Map body) { - - PaymentDTO dto = new PaymentDTO(); - dto.setFirstName((String) body.get("firstName")); - dto.setLastName((String) body.get("lastName")); - dto.setCardNumber((String) body.get("cardNumber")); - dto.setAmount((Double) body.get("amount")); - dto.setCurrency((String) body.get("currency")); - - PaymentResponse paymentResponse = paymentClient.pay(orderNumber, dto); - - return paymentResponse; - - } } diff --git a/spring-boot-modules/spring-boot-microservices/order-service/pom.xml b/spring-boot-modules/spring-boot-microservices/order-service/pom.xml index 603b845cef..fcdfdac595 100644 --- a/spring-boot-modules/spring-boot-microservices/order-service/pom.xml +++ b/spring-boot-modules/spring-boot-microservices/order-service/pom.xml @@ -19,6 +19,7 @@ 1.8 2.6 + 0.0.2 1.8 1.8 UTF-8 @@ -47,6 +48,11 @@ commons-lang ${commons-lang.version} + + org.qunix + structure-maven-plugin + ${structure-maven.version} + org.springframework.boot spring-boot-starter-json @@ -102,6 +108,20 @@ + + org.qunix + structure-maven-plugin + ${structure-maven.version} + false + + + compile + + modules + + + + diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/.gitignore b/spring-boot-modules/spring-boot-microservices/payment-service/.gitignore deleted file mode 100644 index 0e9415ada6..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/.gitignore +++ /dev/null @@ -1,41 +0,0 @@ -#https://github.com/spring-projects/spring-boot/blob/master/.gitignore - -*# -*.iml -*.ipr -*.iws -*.jar -*.sw? -*~ -.#* -.*.md.html -.DS_Store -.classpath -.factorypath -.gradle -.idea -.metadata -.project -.recommenders -.settings -.springBeans -/build -/code -MANIFEST.MF -_site/ -activemq-data -bin -build -build.log -dependency-reduced-pom.xml -dump.rdb -interpolated*.xml -lib/ -manifest.yml -overridedb.* -target -transaction-logs -.flattened-pom.xml -secrets.yml -.gradletasknamecache -.sts4-cache diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/pom.xml b/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/pom.xml deleted file mode 100644 index 19c1bb7e39..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - 4.0.0 - - payment-service - com.baeldung.paymentservice - 1.0-SNAPSHOT - - - - com.baeldung.paymentservice - payment-client - - - \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentClient.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentClient.java deleted file mode 100644 index f9e24725b8..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentClient.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.paymentservice; - -public interface PaymentClient { - - PaymentResponse pay (String orderNumber, PaymentDTO paymentDTO); -} diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentClientImpl.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentClientImpl.java deleted file mode 100644 index 87609b098e..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentClientImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.baeldung.paymentservice; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class PaymentClientImpl implements PaymentClient { - - private RestTemplate restTemplate; - - public PaymentClientImpl(RestTemplateBuilder builder) { - - this.restTemplate = builder.build(); - } - - @Override - public PaymentResponse pay(String orderNumber, PaymentDTO paymentDTO) { - - String serviceUrl = "http://localhost:8003/payment-service"; - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - - HttpEntity request = new HttpEntity<>(paymentDTO, headers); - - PaymentResponse paymentResponse = restTemplate.postForObject(serviceUrl + "/pay/" + orderNumber, request, PaymentResponse.class); - - return paymentResponse; - - } -} diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentResponse.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentResponse.java deleted file mode 100644 index 4a155d4571..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentResponse.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.paymentservice; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@NoArgsConstructor -@AllArgsConstructor -public class PaymentResponse{ - - private String paymentId; - private String paymentMethod; - private String customerFullName; - private Double amount; - private String currency; - -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/resources/application.properties b/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/resources/application.properties deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/pom.xml b/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/pom.xml deleted file mode 100644 index eb429553dd..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/pom.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - 4.0.0 - - - com.baeldung.paymentservice - payment-service - 1.0-SNAPSHOT - - com.baeldung.paymentservice - payment-server - payment-server - - - com.baeldung.paymentservice - payment-client - 1.0-SNAPSHOT - compile - - - diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/CardValidator.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/CardValidator.java deleted file mode 100644 index e4f0a3a68a..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/CardValidator.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.paymentservice; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class CardValidator { - - public static boolean validate(String cardNumber){ - - boolean isValid = false; - - String regex = "^(?:(?4[0-9]{12}(?:[0-9]{3})?)|" + - "(?5[1-5][0-9]{14})|" + - "(?6(?:011|5[0-9]{2})[0-9]{12})|" + - "(?3[47][0-9]{13})|" + - "(?3(?:0[0-5]|[68][0-9])?[0-9]{11})|" + - "(?(?:2131|1800|35[0-9]{3})[0-9]{11}))$"; - - - Pattern pattern = Pattern.compile(regex); - cardNumber = cardNumber.replaceAll("-", ""); - Matcher matcher = pattern.matcher(cardNumber); - - if(matcher.matches()){ - - isValid = true; - } - - return isValid; - } -} diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/Payment.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/Payment.java deleted file mode 100644 index 9789195c98..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/Payment.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.paymentservice; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Payment { - - private String paymentId; - private String paymentMethod; - private String customerFullName; - private double amount; - private String currency; - -} - diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/PaymentApplication.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/PaymentApplication.java deleted file mode 100644 index 0e9336dcfe..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/PaymentApplication.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.paymentservice; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * - * Spring Boot application starter class - */ -@SpringBootApplication -public class PaymentApplication { - public static void main(String[] args) { - SpringApplication.run(PaymentApplication.class, args); - } -} diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/PaymentService.java b/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/PaymentService.java deleted file mode 100644 index f44e342cd7..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/java/com/baeldung/paymentservice/PaymentService.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.paymentservice; - -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -import javax.smartcardio.CardException; -import java.util.UUID; - - -@RestController -public class PaymentService { - - - @PostMapping("/pay/{orderNum}") - public PaymentResponse createPayment(@PathVariable String orderNum, @RequestBody PaymentDTO paymentDTO) { - - Payment payment = new Payment(); - payment.setPaymentId(UUID.randomUUID().toString().replace("-", "")); - String firstName = paymentDTO.getFirstName(); - String lastName = paymentDTO.getLastName(); - payment.setCustomerFullName(firstName + " " + lastName); - String cardNumber = paymentDTO.getCardNumber(); - - if(CardValidator.validate(cardNumber)){ - payment.setPaymentMethod("CREDITCARD"); - } else try { - throw new CardException("Card with number:"+ cardNumber + " is invalid"); - } catch (CardException e) { - e.printStackTrace(); - } - payment.setAmount(paymentDTO.getAmount()); - payment.setCurrency(paymentDTO.getCurrency()); - - return new PaymentResponse(payment.getPaymentId(), payment.getPaymentMethod(), payment.getCustomerFullName(), payment.getAmount(), payment.getCurrency()); - - } -} diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/resources/application.properties b/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/resources/application.properties deleted file mode 100644 index 8f3550946e..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-server/src/main/resources/application.properties +++ /dev/null @@ -1,3 +0,0 @@ -#Spring Boot server configuration -server.servlet.context-path=/payment-service -server.port=8003 diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/pom.xml b/spring-boot-modules/spring-boot-microservices/payment-service/pom.xml deleted file mode 100644 index 474ab6fe93..0000000000 --- a/spring-boot-modules/spring-boot-microservices/payment-service/pom.xml +++ /dev/null @@ -1,110 +0,0 @@ - - - - CustomerOrderApp - com.baeldung - 1.0-SNAPSHOT - - 4.0.0 - - com.baeldung.paymentservice - payment-service - pom - - payment-client - payment-server - - - - 1.8 - 2.6 - 1.8 - 1.8 - UTF-8 - com.baeldung.paymentservice.PaymentApplication - - - - - - org.springframework.boot - spring-boot-dependencies - 2.3.0.RELEASE - pom - import - - - - - - - - org.springframework.boot - spring-boot-starter-web - - - - commons-lang - commons-lang - ${commons-lang.version} - - - - org.springframework.boot - spring-boot-starter-json - - - - org.springframework.boot - spring-boot-test - test - - - - org.springframework.boot - spring-boot-test-autoconfigure - test - - - junit - junit - test - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - ${paymentservice.mainclass} - - - - - repackage - - - exe - - - - start-application - - com.baeldung.paymentservice.PaymentApplication - ../payment-server/target/classes - - - start - - - - - - - - diff --git a/spring-boot-modules/spring-boot-microservices/pom.xml b/spring-boot-modules/spring-boot-microservices/pom.xml index 1cdd7595d4..774a1080ec 100644 --- a/spring-boot-modules/spring-boot-microservices/pom.xml +++ b/spring-boot-modules/spring-boot-microservices/pom.xml @@ -24,7 +24,7 @@ customer-service order-service - payment-service + shared-dto diff --git a/spring-boot-modules/spring-boot-microservices/postman/customer-order.postman_collection b/spring-boot-modules/spring-boot-microservices/postman/customer-order.postman_collection index aa9960aead..6f7fd9d35a 100644 --- a/spring-boot-modules/spring-boot-microservices/postman/customer-order.postman_collection +++ b/spring-boot-modules/spring-boot-microservices/postman/customer-order.postman_collection @@ -52,36 +52,8 @@ } }, "response": [] - }, - { - "name": "Send Order for payment", - "request": { - "method": "POST", - "header": [], - "body": { - "mode": "raw", - "raw": "{\n\t\n\"firstName\":\"John\",\n\"lastName\":\"Smith\",\n\"cardNumber\":\"4260-6720-3283-7081\",\n\"amount\":150.0,\n\"currency\":\"USD\"\n\t\n\t\n}", - "options": { - "raw": { - "language": "json" - } - } - }, - "url": { - "raw": "localhost:8002/order-service/pay/A152", - "host": [ - "localhost" - ], - "port": "8002", - "path": [ - "order-service", - "pay", - "A152" - ] - } - }, - "response": [] } + ], "protocolProfileBehavior": {} } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-microservices/shared-dto/pom.xml b/spring-boot-modules/spring-boot-microservices/shared-dto/pom.xml new file mode 100644 index 0000000000..d61a5bb34b --- /dev/null +++ b/spring-boot-modules/spring-boot-microservices/shared-dto/pom.xml @@ -0,0 +1,53 @@ + + + + + CustomerOrderApp + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + com.baeldung + shared-dto + + shared-dto + + + + UTF-8 + 1.8 + 1.8 + 0.0.2 + + + + + org.qunix + structure-maven-plugin + ${structure-maven.version} + + + + + + + + org.qunix + structure-maven-plugin + ${structure-maven.version} + false + + + compile + + files + + + + + + + + diff --git a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentDTO.java b/spring-boot-modules/spring-boot-microservices/shared-dto/src/main/java/com/baeldung/CustomerDTO.java similarity index 76% rename from spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentDTO.java rename to spring-boot-modules/spring-boot-microservices/shared-dto/src/main/java/com/baeldung/CustomerDTO.java index cd81b78366..549a0dbaf0 100644 --- a/spring-boot-modules/spring-boot-microservices/payment-service/payment-client/src/main/java/com.baeldung.paymentservice/PaymentDTO.java +++ b/spring-boot-modules/spring-boot-microservices/shared-dto/src/main/java/com/baeldung/CustomerDTO.java @@ -7,11 +7,11 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor -public class PaymentDTO { +public class CustomerDTO { private String firstName; private String lastName; + private String homeAddress; private String cardNumber; - private double amount; - private String currency; + } diff --git a/spring-boot-modules/spring-boot-microservices/shared-dto/src/main/java/com/baeldung/OrderDTO.java b/spring-boot-modules/spring-boot-microservices/shared-dto/src/main/java/com/baeldung/OrderDTO.java new file mode 100644 index 0000000000..c31c9f6bec --- /dev/null +++ b/spring-boot-modules/spring-boot-microservices/shared-dto/src/main/java/com/baeldung/OrderDTO.java @@ -0,0 +1,16 @@ +package com.baeldung.orderservice.client; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class OrderDTO { + + private int customerId; + private String itemId; + +}