Chuẩn bị IntelliJ IDEA

Chuẩn bị IntelliJ IDEA

  1. Dùng IntelliJ IDEA mở project spring boot vừa tạo

Architect

  1. Cài đặt project
  • lick phải vào project chọn Open Module Settings Architect
  1. Chọn Project Setting, hãy đảm bảo rằng project của bạn có SDK21language level21 Architect

  2. Chọn Platform setting, hãy đảm bảo rằng platform SDK của bạn là 21 Architect

Chạy spring boot project

  1. Mở file application.properties thêm vào server.port=8081 để cấu hình project chạy trên port 8081 Architect

  2. Nhấn vào biểu tượng Run để chạy project, bạn có thể thấy project đang chạy trên port 8081 Architect

Kiểm thử project trên postman

  1. Tạo request HTTP GET với đường dẫn http://localhost:8080/actuator/health để kiểm tra
  • Reponse trả về “status”: “UP” chứng tỏ project hoạt đông bình thường

Architect

Thêm thư viên Log4j2

  1. Mở file build.grade
    • Trong phần dependencies thêm thư viện Log4j2 bằng cách thêm đoạn mã sau implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    • Thêm configurations
configurations {
  configureEach {
  	exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
  	exclude group: 'commons-logging', module: 'commons-logging'
  }
}

Architect

Tạo Controller

  1. Trong thư mục com.firstcloudjourney.productsservice tạo folder products Architect

  2. Trong thư mục products tạo thư mục controllers và tạo file ProductsController.java từ thư mục controllers

Architect

  1. Copy đoạn code sau vào file ProductsController.java
@RestController
@RequestMapping("/api/products")
public class ProductsController {
    private static final Logger LOG = LogManager.getLogger(ProductsController.class);

    @GetMapping
    public String getAllProducts() {
        LOG.info("Get all products");
        return "All products";
    }
}

Architect

  1. Chạy project và test trên postman bằng cách tạo request HTTP GET với đường dẫn http://localhost:8081/api/products

Architect

  1. Kiểm tra log trả về trên termnial khi gọi HTTP GET Architect