การสร้างคอนโทรลเลอร์ REST
ออกแบบและสร้างปลายทางแบบ RESTful โดยใช้ `@RestController` และแอนโนเทชันการแมปต่าง ๆ
การสร้างคอนโทรลเลอร์ REST เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Complete Guide และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to REST Controllers!
You're about to dive into building powerful web services with Spring Boot! Today, we'll learn how to create RESTful APIs, which are the backbone of modern web applications.
Think of a REST API as a standard way for different computer systems to talk to each other over the internet.
What are RESTful APIs?
REST stands for REpresentational State Transfer. It's an architectural style for networked applications.
- Resources: Everything is a resource (e.g., a user, a product).
- Unique Identifiers: Each resource has a unique URL (e.g.,
/users/123). - Standard Methods: We use standard HTTP methods like GET, POST, PUT, DELETE to interact with resources.
Spring Boot makes implementing these a breeze!
Introducing `@RestController`
In Spring Boot, the @RestController annotation is key to building RESTful web services. It's a special type of Spring component.
- It tells Spring that this class handles incoming web requests.
- It combines
@Controllerand@ResponseBody.
@ResponseBody means the return value of your methods should be directly bound to the web response body.
Your First GET Endpoint
Let's create a simple endpoint that responds to a GET request. GET requests are used to retrieve data.
We use the @GetMapping annotation to map HTTP GET requests to specific handler methods.
Runnable GET Example
Try running this simple Spring Boot application. It defines a /hello endpoint that returns a greeting.
package com.coddykit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from CoddyKit!";
}
}Testing Your GET Endpoint
After running the previous code, your Spring Boot application starts on port 8080 by default.
You can access your new endpoint by opening a web browser and navigating to:
http://localhost:8080/hello
You should see the message "Hello from CoddyKit!" displayed.
Introducing `@PostMapping`
While GET is for retrieving data, POST requests are typically used to create new resources on the server.
The @PostMapping annotation maps HTTP POST requests to a specific method. When you send data, it often comes in the 'request body'.
We use @RequestBody to tell Spring to convert the incoming body into a Java object (or a simple String).
Runnable POST Example
Let's add a POST endpoint to our existing controller. This endpoint will accept a name in the request body and return a personalized greeting.
package com.coddykit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from CoddyKit!";
}
@PostMapping("/greet")
public String greetUser(@RequestBody String name) {
return "Hello, " + name + "!";
}
}Path Variables with `@PathVariable`
Sometimes, you want to extract a value directly from the URL path itself. This is where path variables come in handy.
The @PathVariable annotation allows you to bind a URL template variable to a method parameter.
- Example URL:
/users/123 - Here,
123is a path variable representing a user ID.
Using Path Variables in Code
This example adds an endpoint that uses a path variable to greet a specific person whose name is part of the URL.
package com.coddykit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from CoddyKit!";
}
@PostMapping("/greet")
public String greetUser(@RequestBody String name) {
return "Hello, " + name + "!";
}
@GetMapping("/greet/{name}")
public String greetPathVar(@PathVariable String name) {
return "Greetings, " + name + " from path!";
}
}Quick Check: Controller Basics
You've learned about `@RestController` and basic mapping annotations. Let's test your understanding.
Recap: Creating REST Controllers
Great job! You've taken your first steps into building RESTful APIs with Spring Boot:
- We understood what RESTful APIs are and their purpose.
- We learned about the fundamental
@RestControllerannotation. - You implemented and tested endpoints for GET requests using
@GetMapping. - You also created a basic POST endpoint with
@PostMappingand used@RequestBody. - Finally, you saw how to extract dynamic data from URLs using
@PathVariable.
Next, we'll dive deeper into handling various HTTP requests and crafting responses!
คำถามที่พบบ่อย
บทเรียน “การสร้างคอนโทรลเลอร์ REST” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างคอนโทรลเลอร์ REST” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างคอนโทรลเลอร์ REST”
ออกแบบและสร้างปลายทางแบบ RESTful โดยใช้ `@RestController` และแอนโนเทชันการแมปต่าง ๆ คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างคอนโทรลเลอร์ REST” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างคอนโทรลเลอร์ REST
- การจัดการคำขอและการตอบกลับ HTTP
- การตรวจสอบอินพุตและการจัดการข้อผิดพลาด
- การจัดทำเอกสารส่วนเชื่อมต่อ REST ด้วย OpenAPI และ Swagger