DAY 1 observation 16/7/25
tools description:
language => java version 21 (LTS)
ide => eclipse
Spring Boot =>
✅ What is @RestController?
@RestController
It is a specialized version of @Controller that combines:
-
@Controller→ Marks the class as a Spring MVC controller -
@ResponseBody→ Tells Spring to return data (like JSON/XML) instead of rendering a view
🔹 Purpose:
-
Used for REST APIs that return data, not HTML
-
Automatically serializes Java objects (like
List,Map, or custom classes) to JSON (via Jackson)
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
When you access http://localhost:8080/hello, it returns:
✅ What is @GetMapping?
@GetMapping("/endpoint")
@RequestMapping(method = RequestMethod.GET, value = "/endpoint")
@GetMapping("/students")
public List<String> getStudents() {
return List.of("Alice", "Bob", "Charlie");
}
🔹 Purpose:
-
Maps HTTP GET requests to a specific method
-
Annotation Purpose Used On Returns @RestControllerMarks a class as a REST controller On class Data (JSON/XML) @GetMappingMaps HTTP GET request to a method On method Response data (JSON) Commonly used to read/fetch data
Annotation Description @PostMappingMaps HTTP POST requests @PutMappingMaps HTTP PUT requests @DeleteMappingMaps HTTP DELETE requests @RequestBodyMaps the request JSON body to a POJO @PathVariableGets a value from URL path @RequestParamGets query parameters from the URL
Comments
Post a Comment