Creating a CRUD
Hello, thanks for read until here!
NOW, we gonna make a CRUD with the services below:
- Spring Boot
- Spring JPA
- MySQL inside AWS RDS Instance
- EC2
- ECS
- Application LoadBalance
- Logs in AWS CloudWatch
- Target Group
- Health Check
First , we gonna configure our application to access our database in RDS.
Add the dependencies on gradle.build
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.7.2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Then , create a new package with name: model and create a class called Product
package com.spiet.aws_spring01.model;
import lombok.Data;
import lombok.Getter;
import javax.persistence.*;
@Entity
@Data
@Table(name = "Product", uniqueConstraints = {
@UniqueConstraint(columnNames = {"code"}) //make code unique
})
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 24, nullable = false)
private String name;
@Column(length = 8, nullable = false)
private String code;
private float price;
}
Next Create a repository package.
Create a interface called ProductRepository inside it.
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
Optional<Product> findByCode(String code);
}
Next add a new class on controller called ProductController
package com.spiet.aws_spring01.controller;
import com.spiet.aws_spring01.model.Product;
import com.spiet.aws_spring01.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
@RequestMapping(value = "/v1/products")
public class ProductController {
@Autowired
private ProductRepository productRepository;
@PostMapping
public ResponseEntity<Product> saveProduct(
@RequestBody Product product) {
product.setId(null);
Product productCreated = productRepository.save(product);
return new ResponseEntity<Product>(productCreated,
HttpStatus.CREATED);
}
@DeleteMapping(path = "/{id}")
public ResponseEntity<Product> deleteProduct(@PathVariable("id") long id) {
Optional<Product> optProduct = productRepository.findById(id);
if (optProduct.isPresent()) {
Product product = optProduct.get();
productRepository.delete(product);
return new ResponseEntity<Product>(product, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping
public Iterable<Product> findAll() {
return productRepository.findAll();
}
@PutMapping(path = "/{id}")
public ResponseEntity<Product> updateProduct(
@RequestBody Product product, @PathVariable("id") long id) {
if (productRepository.existsById(id)) {
product.setId(id);
Product productUpdated = productRepository.save(product);
return new ResponseEntity<Product>(productUpdated,
HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping(path = "/bycode")
public ResponseEntity<Product> findByCode(@RequestParam String code) {
Optional<Product> optProduct = productRepository.findByCode(code);
if (optProduct.isPresent()) {
return new ResponseEntity<Product>(optProduct.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/{id}")
public ResponseEntity<Product> findById(@PathVariable long id) {
Optional<Product> optProduct = productRepository.findById(id);
if (optProduct.isPresent()) {
return new ResponseEntity<Product>(optProduct.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
Now, we gonna put the spring datasource configs in application.yml
spring:
jpa:
database: MYSQL
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://localhost:3306/aws_spring01?useTimezone=true&serverTimezone=UTC&createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: ****
password: ****
now test the CRUD endpoints and look the data in your database.
Next, change version project on the build.gradle
and change the image on Service01Stack
Now, make deploy again and test the endpoints.
Create a product:
and others endpoints that you can test.
See you in the part 8.
Top comments (0)