# REST APIs — Advanced Java

Source: https://www.geekswithgeeks.com/en/advanced-java/adv-rest

> 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.

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