Update to customer order modules - added shared-dto

This commit is contained in:
Sasa M 2020-06-07 01:56:45 +02:00
parent ebfae11149
commit c63c9b889e
21 changed files with 100 additions and 415 deletions

View File

@ -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

View File

@ -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<Order> 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<String, Object> 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;
}
}

View File

@ -19,6 +19,7 @@
<properties>
<java.version>1.8</java.version>
<commons-lang.version>2.6</commons-lang.version>
<structure-maven.version>0.0.2</structure-maven.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -47,6 +48,11 @@
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.qunix</groupId>
<artifactId>structure-maven-plugin</artifactId>
<version>${structure-maven.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
@ -102,6 +108,20 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.qunix</groupId>
<artifactId>structure-maven-plugin</artifactId>
<version>${structure-maven.version}</version>
<inherited>false</inherited>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>modules</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

View File

@ -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

View File

@ -1,17 +0,0 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>payment-service</artifactId>
<groupId>com.baeldung.paymentservice</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.baeldung.paymentservice</groupId>
<artifactId>payment-client</artifactId>
</project>

View File

@ -1,6 +0,0 @@
package com.baeldung.paymentservice;
public interface PaymentClient {
PaymentResponse pay (String orderNumber, PaymentDTO paymentDTO);
}

View File

@ -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<PaymentDTO> request = new HttpEntity<>(paymentDTO, headers);
PaymentResponse paymentResponse = restTemplate.postForObject(serviceUrl + "/pay/" + orderNumber, request, PaymentResponse.class);
return paymentResponse;
}
}

View File

@ -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;
}

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.paymentservice</groupId>
<artifactId>payment-service</artifactId>
<version>1.0-SNAPSHOT</version> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung.paymentservice</groupId>
<artifactId>payment-server</artifactId>
<name>payment-server</name>
<dependencies>
<dependency>
<groupId>com.baeldung.paymentservice</groupId>
<artifactId>payment-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -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 = "^(?:(?<visa>4[0-9]{12}(?:[0-9]{3})?)|" +
"(?<mastercard>5[1-5][0-9]{14})|" +
"(?<discover>6(?:011|5[0-9]{2})[0-9]{12})|" +
"(?<amex>3[47][0-9]{13})|" +
"(?<diners>3(?:0[0-5]|[68][0-9])?[0-9]{11})|" +
"(?<jcb>(?: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;
}
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -1,3 +0,0 @@
#Spring Boot server configuration
server.servlet.context-path=/payment-service
server.port=8003

View File

@ -1,110 +0,0 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>CustomerOrderApp</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.paymentservice</groupId>
<artifactId>payment-service</artifactId>
<packaging>pom</packaging>
<modules>
<module>payment-client</module>
<module>payment-server</module>
</modules>
<properties>
<java.version>1.8</java.version>
<commons-lang.version>2.6</commons-lang.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<paymentservice.mainclass>com.baeldung.paymentservice.PaymentApplication</paymentservice.mainclass>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${paymentservice.mainclass}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exe</classifier>
</configuration>
</execution>
<execution>
<id>start-application</id>
<configuration>
<mainClass>com.baeldung.paymentservice.PaymentApplication</mainClass>
<classesDirectory>../payment-server/target/classes</classesDirectory>
</configuration>
<goals>
<goal>start</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -24,7 +24,7 @@
<modules>
<module>customer-service</module>
<module>order-service</module>
<module>payment-service</module>
<module>shared-dto</module>
</modules>
<dependencies>

View File

@ -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": {}
}

View File

@ -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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>CustomerOrderApp</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>shared-dto</artifactId>
<name>shared-dto</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<structure-maven.version>0.0.2</structure-maven.version>
</properties>
<dependencies>
<dependency>
<groupId>org.qunix</groupId>
<artifactId>structure-maven-plugin</artifactId>
<version>${structure-maven.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.qunix</groupId>
<artifactId>structure-maven-plugin</artifactId>
<version>${structure-maven.version}</version>
<inherited>false</inherited>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>files</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -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;
}

View File

@ -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;
}