03 - สร้างโปรเจกต์แรก
เป้าหมายของบท
Section titled “เป้าหมายของบท”บทนี้จะสร้าง Spring Boot project แรกชื่อ backend-api แล้วรันให้ application เริ่มทำงานสำเร็จ
สร้าง project ด้วย Spring Initializr
Section titled “สร้าง project ด้วย Spring Initializr”เปิด Spring Initializr แล้วเลือกค่าหลัก:
Project: GradleLanguage: JavaSpring Boot: 4.xGroup: com.exampleArtifact: backend-apiName: backend-apiPackage name: com.example.backendapiPackaging: JarJava: 25Dependency ที่เลือก
Section titled “Dependency ที่เลือก”เลือก dependency เหล่านี้:
- Spring Web MVC
- Validation
- Lombok
เหตุผล:
Spring Web MVCใช้สร้าง REST APIValidationใช้ตรวจ requestLombokลดโค้ด getter/setter/constructor
ยังไม่ต้องเลือก Spring Data JPA และ PostgreSQL Driver ในบทนี้ เพราะเรายังไม่ได้ตั้งค่า database ถ้าเพิ่ม dependency สองตัวนี้เร็วเกินไป Spring Boot จะพยายามสร้าง datasource ทันที และ application อาจ start ไม่ขึ้นด้วย error เรื่อง database config
เราจะค่อยเพิ่ม Spring Data JPA และ PostgreSQL Driver ในบทที่ 11 ตอนเริ่มเชื่อม PostgreSQL จริง
เปิด project ใน IntelliJ
Section titled “เปิด project ใน IntelliJ”หลัง download project แล้ว unzip จากนั้นเปิด folder ด้วย IntelliJ IDEA
สิ่งที่ควรเห็น:
backend-api/ build.gradle settings.gradle src/ main/ java/ resources/ test/ไฟล์สำคัญ
Section titled “ไฟล์สำคัญ”build.gradle ใช้จัดการ dependency และ plugin ของ Gradle
settings.gradle เก็บชื่อ root project
DemoApplication.java หรือ BackendApiApplication.java คือ entry point ของ Spring Boot
application.properties ใช้เก็บ configuration เช่น application name, database URL, port
Entry point ของ Spring Boot
Section titled “Entry point ของ Spring Boot”ตัวอย่าง:
package com.example.backendapi;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class BackendApiApplication {
public static void main(String[] args) { SpringApplication.run(BackendApiApplication.class, args); }}@SpringBootApplication ทำให้ Spring Boot เริ่ม scan component และ auto configuration ต่าง ๆ
รัน application
Section titled “รัน application”บน Windows:
.\gradlew.bat bootRunบน macOS/Linux:
./gradlew bootRunถ้าสำเร็จจะเห็น log ประมาณนี้:
Tomcat started on port 8080Started BackendApiApplicationตั้งชื่อ application
Section titled “ตั้งชื่อ application”เปิด src/main/resources/application.properties
spring.application.name=backend-apiปัญหาที่พบบ่อย
Section titled “ปัญหาที่พบบ่อย”Build failed เพราะ Java version ไม่ตรง
Section titled “Build failed เพราะ Java version ไม่ตรง”ตรวจ java -version และค่า Java ใน build.gradle
Lombok error ใน IDE
Section titled “Lombok error ใน IDE”ให้เปิด annotation processing ใน IntelliJ และตรวจว่ามี Lombok plugin
Application รันแล้วปิดทันที
Section titled “Application รันแล้วปิดทันที”ตรวจว่ามี dependency Spring Web MVC หรือไม่ ถ้าไม่มี web server อาจไม่ถูกเปิด
Checkpoint
Section titled “Checkpoint”บทนี้ถือว่าสำเร็จเมื่อ:
- เปิด project ใน IntelliJ ได้
- รัน
bootRunผ่าน - เห็น Spring Boot started ใน console
- มี
application.propertiesพร้อมใช้งาน
แบบฝึกหัดท้ายบท
Section titled “แบบฝึกหัดท้ายบท”เปลี่ยนชื่อ application เป็นชื่อของตัวเอง แล้วรัน project อีกครั้ง