Spring Boot · Lesson 3 of 7

REST controllers

@RestController, mapping GET/POST/PUT/DELETE, path variables, query parameters and ResponseEntity.

A @RestController handles HTTP requests and returns data, which Spring converts to JSON automatically (using Jackson).

  • @RequestMapping("/api/jobs") on the class sets the base path.
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping map methods.
  • @PathVariable reads /jobs/{id}; @RequestParam reads ?city=Kochi; @RequestBody reads JSON.
  • ResponseEntity lets you set the status code — such as 201 Created or 404 Not Found.

Use Java records for simple request and response objects (DTOs).

Example code

Spring Boot 3 · Java 17+
public record JobDto(Long id, String title, String city) {}

@RestController
@RequestMapping("/api/jobs")
public class JobController {
    private final Map<Long, JobDto> jobs = new ConcurrentHashMap<>();
    private final AtomicLong ids = new AtomicLong();

    @GetMapping
    public List<JobDto> list(@RequestParam(required = false) String city) {
        return jobs.values().stream()
            .filter(j -> city == null || j.city().equalsIgnoreCase(city))
            .toList();
    }

    @GetMapping("/{id}")
    public ResponseEntity<JobDto> get(@PathVariable Long id) {
        JobDto job = jobs.get(id);
        return job == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(job);
    }

    @PostMapping
    public ResponseEntity<JobDto> create(@RequestBody JobDto body) {
        JobDto saved = new JobDto(ids.incrementAndGet(), body.title(), body.city());
        jobs.put(saved.id(), saved);
        return ResponseEntity.status(HttpStatus.CREATED).body(saved);
    }
}

Output / API response

POST /api/jobs  {"title":"Java Developer","city":"Kochi"}
→ 201 Created  {"id":1,"title":"Java Developer","city":"Kochi"}

GET /api/jobs?city=kochi
→ 200 OK  [{"id":1,"title":"Java Developer","city":"Kochi"}]

GET /api/jobs/7
→ 404 Not Found

Generate a project at start.spring.io (Maven, Java 17+, dependencies: Spring Web, Spring Data JPA, H2, Validation), add the code, and run ./mvnw spring-boot:run. Learn the Java tutorial first if you're new to Java. Data is fictional.

Key points

  • @RestController returns JSON automatically.
  • @PathVariable for /{id}, @RequestParam for ?query, @RequestBody for JSON.
  • Use ResponseEntity to control status codes.
  • Records make concise DTOs.

Quick check

Which annotation reads the id from /api/jobs/5?

Practice task

Add PUT /api/jobs/{id} that replaces a job (404 if missing) and DELETE /api/jobs/{id} that returns 204 No Content.

Interview corner

What is the difference between @Controller and @RestController?

@Controller is for MVC apps that return view names (HTML templates). @RestController = @Controller + @ResponseBody, so every method's return value is written straight to the response, usually as JSON.

@PathVariable vs @RequestParam?

@PathVariable reads part of the path (/jobs/5) and usually identifies a resource. @RequestParam reads query parameters (/jobs?city=Kochi) and is typically used for filtering, sorting and paging.

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.