Сьогодні ми розберемо практичну розробку мікросервісу для управління сутністю Customer. Ми реалізуємо CRUD-операції, підключимо базу даних через Spring Data JPA і протестуємо все це за допомогою інструментів на кшталт Postman.
Крок 1: підготовка оточення та створення нового проєкту
Для початку створимо новий проєкт. Зробити це можна через Spring Initializr:
- Вкажіть такі параметри:
- Project: Maven
- Language: Java
- Spring Boot Version: 3.0.x+
- Dependencies:
- Spring Web
- Spring Data JPA
- H2 Database (вбудована база даних для тестування)
- Spring Boot DevTools (для зручної розробки)
- Завантажте проєкт і відкрийте його у вашій IDE (наприклад, IntelliJ IDEA).
Крок 2: Налаштування бази даних
Створіть файл application.yml в папці src/main/resources (якщо його немає). Налаштуйте підключення до бази даних H2 наступним чином:
spring:
datasource:
url: jdbc:h2:mem:customerdb
driver-class-name: org.h2.Driver
username: sa
password: password
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
format_sql: true
Цей файл конфігурує базу даних в пам'яті (H2) і каже Hibernate автоматично оновлювати схему таблиць. Опція show-sql дозволяє бачити SQL-запити в консолі, що дуже корисно для налагодження.
Крок 3: Створення сутності Customer
Додайте клас Customer в package com.example.customer:
package com.example.customer;
import jakarta.persistence.*;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Автоматична генерація ID
private Long id;
@Column(nullable = false) // Поле не може бути null
private String name;
@Column(nullable = false, unique = true)
private String email;
public Customer() {
// Конструктор за замовчуванням для JPA
}
public Customer(String name, String email) {
this.name = name;
this.email = email;
}
// Геттери і сеттери
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Ми описали сутність Customer з полями id, name і email. Анотація @Entity каже JPA, що цей клас мапиться на таблицю в базі даних.
Крок 4: Створення репозиторію
Створіть інтерфейс CustomerRepository:
package com.example.customer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
// JpaRepository надає базові CRUD-операції
}
Цей інтерфейс дає змогу легко працювати з базою, уникаючи написання SQL-запитів. Наприклад, методи save(), findById(), findAll() і deleteById() доступні одразу після додавання репозиторію.
Крок 5: Створення сервісу
Додайте клас CustomerService:
package com.example.customer;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CustomerService {
private final CustomerRepository customerRepository;
public CustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
public List<Customer> getAllCustomers() {
return customerRepository.findAll();
}
public Customer getCustomerById(Long id) {
return customerRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Customer not found with id: " + id));
}
public Customer createCustomer(Customer customer) {
return customerRepository.save(customer);
}
public Customer updateCustomer(Long id, Customer updatedCustomer) {
Customer existingCustomer = getCustomerById(id);
existingCustomer.setName(updatedCustomer.getName());
existingCustomer.setEmail(updatedCustomer.getEmail());
return customerRepository.save(existingCustomer);
}
public void deleteCustomer(Long id) {
customerRepository.deleteById(id);
}
}
Цей клас інкапсулює бізнес-логіку для роботи з клієнтами.
Крок 6: Створення REST-контролера
Додайте клас CustomerController:
package com.example.customer;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/customers")
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping
public List<Customer> getAllCustomers() {
return customerService.getAllCustomers();
}
@GetMapping("/{id}")
public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) {
try {
Customer customer = customerService.getCustomerById(id);
return ResponseEntity.ok(customer);
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Customer createCustomer(@RequestBody Customer customer) {
return customerService.createCustomer(customer);
}
@PutMapping("/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable Long id, @RequestBody Customer updatedCustomer) {
try {
Customer customer = customerService.updateCustomer(id, updatedCustomer);
return ResponseEntity.ok(customer);
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteCustomer(@PathVariable Long id) {
customerService.deleteCustomer(id);
}
}
Контролер обробляє HTTP-запити і викликає методи сервісу. Тут ми використовуємо анотації @RestController, @GetMapping, @PostMapping, @PutMapping і @DeleteMapping для маршрутизації запитів.
Крок 7: Тестування REST API
Запустіть застосунок: натисніть Run у вашій IDE або запустивши команду mvn spring-boot:run у терміналі.
Тестування ендпоінтів через Postman
- Отримати всіх клієнтів (GET):
- URL:
http://localhost:8080/customers - Response:
[ ](за замовчуванням пустий список).
- URL:
- Створити нового клієнта (POST):
- URL:
http://localhost:8080/customers - Body (JSON):
{ "name": "John Doe", "email": "john.doe@example.com" } - URL:
- Response: Створений об'єкт.
- Отримати клієнта по ID (GET):
- URL:
http://localhost:8080/customers/1.
- URL:
- Оновити клієнта (PUT):
- URL:
http://localhost:8080/customers/1 - Body (JSON):
{ "name": "Jane Doe", "email": "jane.doe@example.com" }
- URL:
- Видалити клієнта (DELETE):
- URL:
http://localhost:8080/customers/1.
- URL:
Крок 8: Обробка помилок і покращення
Якщо ви намагаєтеся видалити або оновити неіснуючого клієнта, застосунок має повертати 404. Це вже реалізовано в нашому контролері через ResponseEntity і кастомні винятки.
Вітаю! Ви щойно створили свій перший мікросервіс з REST API. Тепер ви готові будувати більше і масштабувати!
ПЕРЕЙДІТЬ В ПОВНУ ВЕРСІЮ