HTTP İsteklerini ve Yanıtlarını Yönetme
İstek parametrelerini, yol değişkenlerini ve istek gövdelerini işlemeyi, uygun HTTP yanıtları oluşturmayı öğrenin.
HTTP İsteklerini ve Yanıtlarını Yönetme, CoddyKit'te ücretsiz bir Spring Boot 4 Complete Guide dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Boot 4 Complete Guide öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Complete Guide kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Requests & Responses
When you interact with a web application, your browser sends an HTTP Request to a server. The server then processes it and sends back an HTTP Response.
In Spring Boot, we write code to listen for these requests, extract information, perform actions, and then craft a suitable response.
Getting Query Params: @RequestParam
Sometimes, extra data is sent in the URL after a ?, like /search?keyword=java. These are query parameters.
Spring Boot uses the @RequestParam annotation to easily extract these values into your method parameters.
Live Demo: @RequestParam
Try running this example. After the app starts, open your browser or a tool like Postman and visit http://localhost:8080/hello?name=Coddy. See what happens!
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@GetMapping("/hello")
public String sayHello(
@RequestParam(defaultValue = "Guest") String name) {
return "Hello, " + name + "!";
}
}Extracting from Path: @PathVariable
Other times, important data is part of the URL path itself, like /users/123. Here, 123 is an ID.
The @PathVariable annotation lets you capture these dynamic segments from the URL directly into your method parameters.
Live Demo: @PathVariable
Run this app, then visit http://localhost:8080/items/apple or http://localhost:8080/items/banana in your browser.
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.RestController;
@SpringBootApplication
@RestController
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@GetMapping("/items/{itemName}")
public String getItemDetails(@PathVariable String itemName) {
return "You requested item: " + itemName;
}
}Query vs. Path: When to Use?
- @RequestParam: Use for optional filtering, sorting, or pagination (e.g.,
/products?category=books&page=1). - @PathVariable: Use for identifying a specific resource (e.g.,
/users/{id},/products/{sku}). It's essential for the resource's identity.
Handling Data Payloads: @RequestBody
For operations like creating or updating resources (POST, PUT requests), clients often send complex data (like JSON or XML) in the request body.
The @RequestBody annotation automatically converts this body content into a Java object for you, thanks to Spring's built-in message converters.
Live Demo: @RequestBody
Run this. Use a tool like Postman to send a POST request to http://localhost:8080/users with a JSON body:
{"id":1, "name":"Coddy"}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
// Define a simple User class
public static class User {
private Long id;
private String name;
// Getters and setters for JSON mapping
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; }
}
@PostMapping("/users")
public String createUser(@RequestBody User user) {
return "User created: ID=" + user.getId() +
", Name=" + user.getName();
}
}Custom Responses: ResponseEntity
By default, Spring often returns a String or a Java object, which Spring converts to JSON/XML with a 200 OK status.
For more control, especially over HTTP status codes (like 201 Created, 404 Not Found), use ResponseEntity. It lets you specify the body, status, and headers explicitly.
Quick Check: Request Handling
Consider a Spring Boot REST endpoint designed to fetch a user by their unique ID, like /api/users/5. Which annotation is best suited to extract the 5 from the URL?
Lesson Summary
We've covered how Spring Boot makes handling HTTP requests easy:
@RequestParamfor query parameters (optional data).@PathVariablefor path variables (resource identifiers).@RequestBodyfor deserializing request body content into Java objects.ResponseEntityfor fine-grained control over HTTP responses, including status codes.
Mastering these annotations is key to building robust RESTful APIs!
Sıkça Sorulan Sorular
“HTTP İsteklerini ve Yanıtlarını Yönetme” dersi ücretsiz mi?
Evet — “HTTP İsteklerini ve Yanıtlarını Yönetme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Boot 4 Complete Guide kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Complete Guide kursu toplamda 4 dersten oluşur.
“HTTP İsteklerini ve Yanıtlarını Yönetme” dersinde ne öğreneceğim?
İstek parametrelerini, yol değişkenlerini ve istek gövdelerini işlemeyi, uygun HTTP yanıtları oluşturmayı öğrenin. Spring Boot 4 Complete Guide ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Spring Boot 4 Complete Guide öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Complete Guide, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“HTTP İsteklerini ve Yanıtlarını Yönetme” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Spring Boot 4 Complete Guide dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Spring Boot 4 Complete Guide dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- REST Denetleyicileri Oluşturma
- HTTP İsteklerini ve Yanıtlarını Yönetme
- Girdi Doğrulama ve Hata Yönetimi
- REST API'lerini OpenAPI ve Swagger ile Belgeleme