Stop 14 · Spring Boot & Backend DevelopmentTopics · 4/13

Building REST APIs with Spring Boot

Part of the Java Roadmap

In short

Mobile apps, React frontends and other services all talk to your backend through REST APIs. With a few annotations, Spring Boot turns plain methods into clean JSON endpoints.

With @RestController and mappings like @GetMapping and @PostMapping you build JSON APIs. Good APIs use correct HTTP methods and status codes, validation and consistent error responses.

The jaadu as a waiter balances a tray with a covered plate between a kitchen counter and a diner's table, a small order slip tucked behind one ear.

Think of it like this

The restaurant waiter

The waiter takes your order slip (the request), carries it to the right station in the kitchen (the controller method), and brings back your plate (the JSON response) with a quick note like "Here you go" or "Sorry, that's finished" (the status code). You never walk into the kitchen yourself.

Watch it happen

POST /api/products

Client
Controller
Service
Repository

POST JSON

1/5The client sends {"name": "Kurta", "price": 899}. Jackson turns it into a Java record.

The big ideas

What you need to know

@RestController

Every method's return value is written straight to the response body as JSON.

Mappings

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping. @PathVariable reads /products/{id}, @RequestParam reads ?page=2.

@RequestBody

Turns incoming JSON into a Java object. Use small DTO records instead of exposing entities.

Status codes

200 OK, 201 Created, 204 No Content, 400 Bad Request, 404 Not Found. ResponseEntity lets you choose.

See it in code

Walk through the code

java
1import org.springframework.http.ResponseEntity;2import org.springframework.web.bind.annotation.*;3import java.net.URI;4import java.util.List;5 6record CreateProductRequest(String name, int price) {}7record ProductResponse(Long id, String name, int price) {}8 9@RestController10@RequestMapping("/api/products")11public class ProductController {12    private final ProductService service;13 14    public ProductController(ProductService service) { this.service = service; }15 16    @GetMapping17    public List<ProductResponse> list(@RequestParam(defaultValue = "0") int page) {18        return service.list(page);19    }20 21    @GetMapping("/{id}")22    public ProductResponse get(@PathVariable Long id) {23        return service.get(id);24    }25 26    @PostMapping27    public ResponseEntity<ProductResponse> create(@RequestBody CreateProductRequest req) {28        ProductResponse saved = service.create(req);29        return ResponseEntity.created(URI.create("/api/products/" + saved.id())).body(saved);30    }31 32    @DeleteMapping("/{id}")33    public ResponseEntity<Void> delete(@PathVariable Long id) {34        service.delete(id);35        return ResponseEntity.noContent().build();36    }37}

1/5Separate DTOs for input and output, so the entity can change without breaking clients.

A thin controller over a ProductService. Records make tidy request and response DTOs.

Save yourself the pain

Common mistakes

Oops

Returning JPA entities directly from controllers.

Do this instead

Map to DTOs. Entities can leak fields like password hashes and trigger lazy-loading errors during JSON conversion.

Oops

Using verbs in URLs like /getAllProducts or /deleteProduct?id=5.

Do this instead

Use nouns and HTTP methods: GET /products, DELETE /products/5.

Oops

Returning 200 OK with {"error": "not found"}.

Do this instead

Return the real status code, like 404, so clients and tools can react correctly.

Your mission

A products API

~45 min

Quick check

Which status code best fits a successful POST that created a new resource?

Go deeper

  1. 1Build CRUD endpoints for one resource and test them with Postman or curl.
  2. 2Validate request bodies with @Valid and Jakarta Bean Validation annotations.
  3. 3Handle errors centrally with @RestControllerAdvice.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge