Introduction to REST — Spring Technical Interview Questions & Answers
Resource-based, stateless HTTP API design.
Introduction to REST
- REST APIs let clients talk to your server using HTTP and resources.
- Resources are nouns (users, orders), actions use HTTP methods (GET/POST/PUT/DELETE).
- Stateless: each request carries what the server needs; server does not keep client session state for the API call.
- JSON is the common data format in Boot apps.
- Resource + HTTP verb + stateless + JSON.
Technical Interview Tip: Define REST in one breath with an example resource.
Code Examples for Introduction to REST
Introduction to REST — working example
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/users")
class UserController {
@GetMapping("/{id}")
ResponseEntity<String> get(@PathVariable Long id) {
return ResponseEntity.ok("user-" + id);
}
@GetMapping
String byRole(@RequestParam(defaultValue = "user") String role) {
return "role=" + role;
}
@PostMapping
ResponseEntity<String> create(@RequestBody Map<String, String> body) {
return ResponseEntity.status(201).body("created:" + body.get("email"));
}
}Introduction to REST — @RestController = API JSON/text; @PathVariable from URI, @RequestParam from query, @RequestBody from JSON body.
How to Explain Introduction to REST to an Interviewer
What is REST (sb intro)?
Used for a tiny REST app localhost:8080 Not just the heading
Give one small example of REST (sb intro).
@SpringBootApplication + one controller Resource-based, stateless HTTP API design
What mistake do beginners make with REST (sb intro)?
calling Boot ‘just Spring XML’ Show the fix
Where do you use REST (sb intro)?
localhost:8080 a tiny REST app