Lesson 12 · Spring MVC · completes Section 3

REST

HTTP verbs and status codes, building REST controllers, and consuming services with RestTemplate.

What you'll be tested on Which HTTP verb does what (and which are safe / idempotent); common status codes; building endpoints with @RestController and ResponseEntity; and using RestTemplate to call another service.

What REST is

REST (Representational State Transfer) is an architectural style that uses HTTP as its protocol. Its hallmarks: stateless requests, resources identified by URIs, a uniform interface built on the standard HTTP verbs, and representations (usually JSON) exchanged in message bodies.

The HTTP verbs

VerbPurposeSafe?Idempotent?
GETRead a resource✅ yes✅ yes
POSTCreate a resource❌ no❌ no
PUTCreate/replace (full update)❌ no✅ yes
PATCHPartial update❌ no❌ no
DELETERemove a resource❌ no✅ yes
Safe vs idempotent Safe = no state change (only GET, HEAD). Idempotent = repeating it has the same effect as doing it once (GET, PUT, DELETE). POST is neither — two POSTs create two resources.

Status codes worth memorising

CodeMeaningClass
200OK2xx success
201Created (after a successful POST)
204No Content
400Bad Request4xx client error
401Unauthorized (not authenticated)
403Forbidden (authenticated, not allowed)
404Not Found
500Internal Server Error5xx server error

401 vs 403: 401 = "I don't know who you are" (not authenticated); 403 = "I know who you are, but you can't" (not authorised).

Building REST endpoints

Use @RestController (Lesson 11) with the verb-mapping shortcuts. For full control of the status, headers and body, return a ResponseEntity; for a fixed status, use @ResponseStatus:

@PostMapping
public ResponseEntity<Account> create(@RequestBody Account a) {
  Account saved = service.save(a);
  return ResponseEntity.status(HttpStatus.CREATED).body(saved);  // 201 + body
}

The @RestController, @RequestBody and @ResponseBody annotations live in the spring-web module. A returned object is serialised to JSON by an HttpMessageConverter — Jackson by default.

Consuming REST with RestTemplate

RestTemplate is Spring's synchronous client for calling REST services (it uses the same HttpMessageConverters to (de)serialise). Common methods:

MethodDoes
getForObject(url, Type.class)GET → response body mapped to an object
getForEntity(url, Type.class)GET → a ResponseEntity (status + headers + body)
postForObject(url, body, Type.class)POST → response body as object
exchange(url, method, entity, Type.class)any verb, full request/response control

(In newer Spring, WebClient is the recommended reactive alternative — but the exam tests RestTemplate.)

Primary source — read this
Spring Framework 5.3 — Web MVC: Annotated Controllers & Accessing REST Endpoints with RestTemplate

Read the "Handler Methods" return-value table (ResponseEntity) and the RestTemplate method summary.

Check yourself

The safe/idempotent verbs, the status codes, and the RestTemplate methods are the money questions. Covers your book's whole REST section. Options shuffle on every load.

🎉 Section 3 (Spring MVC) complete You've covered the request lifecycle, controllers, and REST. Second pass: your book's REST section (12 questions). Ask me any time for an MVC + REST mixed review.
I'm your teacher — ask me anything. Want a full CRUD controller with the right verbs and status codes, or a RestTemplate call example? Ask. Say "continue" to start Section 4 — Testing with Lesson 13.
← Lesson 11 · Spring MVC Lesson 13 · Testing →