0% found this document useful (0 votes)
24 views5 pages

Spring Boot E-Commerce Microservices

The document outlines the implementation of a microservices-based e-commerce application, detailing the Product and Order services. It includes Java code for data access objects, repositories, services, and controllers for managing product information and orders. Additionally, it specifies the necessary dependencies and configurations for the Spring Boot application.

Uploaded by

bhagath sunkara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Spring Boot E-Commerce Microservices

The document outlines the implementation of a microservices-based e-commerce application, detailing the Product and Order services. It includes Java code for data access objects, repositories, services, and controllers for managing product information and orders. Additionally, it specifies the necessary dependencies and configurations for the Spring Boot application.

Uploaded by

bhagath sunkara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

1.

Scenario:
You're building a microservices-based application for an e-commerce platform. One
microservice is responsible for managing product information (Product Service),
another manages orders (Order Service).

package [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "product")
@FieldDefaults(level = [Link])
public class Product {
@Id
@GeneratedValue(strategy = [Link])
Long id;
String name;
String description;
}

----------

package [Link];
import [Link];
import [Link];
import [Link];
@Repository
public interface ProductRepository extends JpaRepository <Product, Long> {
}

------------

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<Product> listProducts() {
try {
return [Link]();
} catch (Exception e) {
throw new RuntimeException("Getting DB error while fetching records");
}
}
public Product getProductById(Long id) {
Optional<Product> productInfo = [Link](id);
if(![Link]()) {
throw new RuntimeException("Product Id info not found" + id);
}
return [Link]();
}
}

----------

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@RequestMapping("/api/")
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/products")
public List<Product> getList() {
return [Link]();
}
@GetMapping("/products/{id}")
public Product getProductId(@PathVariable Long id) {
return [Link](id);
}
}

---------------

Order

____________

package [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "order")
@FieldDefaults(level = [Link])
public class Order {
@Id
@GeneratedValue(strategy = [Link])
Long id;
String customerName;
LocalDate orderDate;
List<Long> productIds;
}

package [Link];
import [Link];
import [Link];
import [Link];
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
}

------------

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private RestTemplate restTemplate;
private static final Logger log = [Link]([Link]);
public Order createOrder(Order order) {
for (Long productId : [Link]()) {
try {
ResponseEntity<Product> productResponse =
[Link]("[Link] productId,
[Link], null, [Link]);
if(![Link]().is2xxSuccessful()) {
[Link]("Product id not found "+ productId);
}
} catch (RestClientException e) {
[Link]("Error:internal server error "+ [Link]());
throw new
RuntimeException([Link](HttpStatus.INTERNAL_SERVER_ERROR));
}
}
return [Link](order);
}
}
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@RequestMapping("/api/")
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping("/order")
public Order upsert(@Valid @RequestBody Order order) {
return [Link](order);
}
}

----------

plugins {
id 'java'
id '[Link]' version '3.3.0'
id '[Link]-management' version '1.1.5'
id '[Link]' version '6.2.1'
}
group = '[Link]'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = [Link](17)
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation '[Link]:spring-boot-starter-web'
implementation '[Link]:spring-boot-starter-data-jpa'
compileOnly '[Link]:lombok'
runtimeOnly '[Link]:mysql-connector-j'
annotationProcessor '[Link]:lombok'
testImplementation '[Link]:spring-boot-starter-test'
testRuntimeOnly '[Link]:junit-platform-launcher'
implementation '[Link]:kafka-clients:3.4.0'
implementation '[Link]:[Link].6.2'
implementation 'org.slf4j:slf4j-api:2.0.9'
}
generateJava {
schemaPaths = ["${projectDir}/src/main/resources/graphql-client"]
packageName = '[Link]'
generateClient = true
}
[Link]('test') {
useJUnitPlatform()
}

You might also like