Lesson 18 / 38

REST APIs

Design resource-oriented HTTP APIs — URIs, verbs, status codes and statelessness — and implement them with JAX-RS or Spring Web returning JSON via Jackson.

REST principles

Model resources with URIs (/orders/42), use HTTP verbs for actions (GET read, POST create, PUT/PATCH update, DELETE remove), return proper status codes (200, 201, 400, 404, 409), and keep the server stateless — each request carries its own auth.

A controller

JAX-RS (@Path, @GET) and Spring Web (@RestController, @GetMapping) both map methods to routes and (de)serialise JSON automatically via Jackson.

@RestController
@RequestMapping("/orders")
class OrderController {
    @GetMapping("/{id}")
    Order get(@PathVariable long id) {
        return service.find(id)
            .orElseThrow(() -> new ResponseStatusException(NOT_FOUND));
    }
}