Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Which HTTP methods are safe and which are idempotent? How does Spring MVC handle HEAD and OPTIONS requests for a controller that only declares @GetMapping and @PostMapping?

Safe methods do not change state on the server: GET, HEAD and OPTIONS. Idempotent methods have the same effect whether we send them once or many times: the safe ones plus PUT and DELETE, while POST and PATCH are not idempotent by default. For a controller with only @GetMapping and @PostMapping, Spring MVC answers HEAD automatically by running the GET handler without writing the body, answers OPTIONS automatically with an Allow header, and answers any other method with 405 Method Not Allowed and an Allow header.

Why this question is asked

These definitions sound academic, but they decide real behaviour: which requests a proxy may retry, which ones a browser prefetches, which ones a cache may store, and what happens when a client repeats a request after a timeout. Interviewers also like to check whether we know what Spring does with methods we never mapped. In this answer, we will define safe and idempotent with examples, look at PUT versus PATCH, and then see exactly how Spring MVC answers HEAD, OPTIONS and unsupported methods for a controller that declares only GET and POST.

1What Safe and Idempotent Mean

A method is safe when the client does not ask for any change on the server by sending it. GET, HEAD and OPTIONS are safe. The server may still log the request or count it, but the client did not request a change, so crawlers, prefetchers and caches may send these freely.

A method is idempotent when sending the same request several times has the same effect on the server as sending it once. Every safe method is idempotent, because doing nothing twice is still nothing. PUT is idempotent because it says set the resource to exactly this, and setting it twice leaves the same state. DELETE is idempotent because after the first call the resource is gone, and later calls leave it gone, even if they answer 404 instead of 204. Notice that idempotent is about the effect on the server, not about the response being identical.

POST is not idempotent: POST /orders twice usually creates two orders. PATCH is not idempotent by default, because a patch such as add 1 to quantity gives a different result each time, although a particular patch can be.

HTTP methods by safety and idempotency
MethodSafeIdempotentTypical use
GETyesyesread a resource
HEADyesyesheaders of a GET, no body
OPTIONSyesyeswhich methods are allowed
PUTnoyesreplace the whole resource
DELETEnoyesremove the resource
POSTnonocreate, or run a command
PATCHnonot by defaultpartial change

2PUT Replaces, PATCH Changes Part

PUT sends the complete new state of the resource, and fields that are left out are reset or removed. PATCH sends only a change. The format of that change must be defined, and two standard ones exist. JSON Merge Patch, media type application/merge-patch+json, sends a partial document where present fields are replaced and null removes a field. JSON Patch, media type application/json-patch+json, sends a list of operations such as replace, add and remove at given paths.

Java
PUT /orders/1 {"status": "PAID", "note": null, "lines": [...]} -- the full order
PATCH /orders/1 Content-Type: application/merge-patch+json
{"status": "PAID"} -- only status changes
PATCH /orders/1 Content-Type: application/json-patch+json
[{"op": "replace", "path": "/status", "value": "PAID"}]

So a retry of the same PUT is harmless, while a retry of a PATCH is only harmless if that particular patch sets values rather than incrementing them.

3How Spring MVC Answers HEAD

Now take a controller that only maps GET and POST.

Java
@RestController
@RequestMapping("/orders")
class OrderController {
@GetMapping("/{id}") OrderDto get(@PathVariable long id) { ... }
@PostMapping ResponseEntity<OrderDto> create(@RequestBody OrderDto body) { ... }
}
// HEAD /orders/1 -> 200, same headers as GET (Content-Type, Content-Length), no body

Spring MVC lets a GET mapping match HEAD requests too, so the get method runs as usual. The servlet layer then discards the body and keeps the headers, including the Content-Length the body would have had. The reason HEAD exists is to let clients check a resource cheaply, for example whether it exists or its ETag, so it is useful to know that the handler still does its full work. If a HEAD needs to be cheaper, we can map it explicitly with @RequestMapping(method = RequestMethod.HEAD) and Spring will prefer that mapping.

4How Spring MVC Answers OPTIONS and Unsupported Methods

An OPTIONS request asks which methods the path supports. Spring MVC answers it automatically when no handler maps OPTIONS explicitly: it collects the methods of all mappings for that path and returns them in the Allow header.

Java
OPTIONS /orders/1 -> 200, Allow: GET,HEAD,OPTIONS
OPTIONS /orders -> 200, Allow: POST,OPTIONS
DELETE /orders/1 -> 405 Method Not Allowed, Allow: GET
PUT /orders -> 405 Method Not Allowed, Allow: POST
Step throughHow Spring MVC resolves the method
  1. Match the path/orders/1
  2. Is it a CORS preflight?
    YesAnswer from CORS configNoCheck the method
  3. Does a mapping support the method?
    YesRun the handler (HEAD uses GET)NoIs it OPTIONS?
  4. Is it OPTIONS?
    Yes200 with Allow headerNo405 with Allow header

4 steps in order. Play them, or step through them one at a time.

Notice that HEAD appears next to GET, because Spring supports it implicitly. When a request uses a method that no mapping supports for a path that exists, Spring throws HttpRequestMethodNotSupportedException, and the default handling answers 405 with the Allow header so the client knows what it can do instead. A path that matches no mapping at all gives 404, not 405.

One special case is worth knowing: a CORS preflight is also an OPTIONS request, but it carries Origin and Access-Control-Request-Method headers. Spring recognises it and answers it from the CORS configuration, not with the plain Allow response.

5Follow-up questions

Is a GET that increments a view counter still safe?

From the protocol's point of view, yes, because the client did not ask for a change, and side effects like logging and counters are allowed. But a GET that changes business state, such as /orders/1/cancel, breaks the contract: a crawler or prefetch could cancel orders. Such actions belong in POST.

Should DELETE of a missing resource return 404 or 204?

Both keep DELETE idempotent, because the server state is the same. 204 makes retries look identical to the first call, which simplifies clients; 404 tells the client the resource was not there. A company should choose one rule and use it everywhere.

How do we make a POST safe to retry?

With an idempotency key: the client sends a unique Idempotency-Key header per operation and the server stores the result for that key, replaying it on a retry instead of creating a second order. That makes the operation idempotent even though POST itself is not.

6Conclusion

In this answer, we saw that GET, HEAD and OPTIONS are safe, that idempotent methods are the safe ones plus PUT and DELETE, and that POST and PATCH are not idempotent by default. PUT replaces a resource while PATCH applies a partial change in a defined format such as JSON Merge Patch or JSON Patch. For a controller with only GET and POST mappings, Spring MVC answers HEAD with the GET handler minus the body, answers OPTIONS with an Allow header, and answers other methods with 405 and an Allow header.

What to say in the interviewAbout 80 seconds aloud

Open with

Safe methods are the ones where the client asks for no change on the server: GET, HEAD and OPTIONS. Idempotent means repeating the request has the same effect as sending it once, so it includes the safe methods plus PUT and DELETE.

Then, if they want more

PUT is idempotent because it replaces the resource with the same full state each time, and DELETE because the resource stays deleted, even if the second call returns 404. POST is not idempotent, since two POSTs usually create two resources, and PATCH is not by default, because a patch can be relative like incrementing a value.

PATCH should use a defined format such as JSON Merge Patch or JSON Patch. For a controller that only has @GetMapping and @PostMapping, Spring MVC handles the rest itself. A HEAD request matches the GET mapping, the handler runs normally and the body is dropped, so the headers including Content-Length are the same as for GET.

An OPTIONS request is answered automatically with 200 and an Allow header listing the supported methods for that path, for example GET, HEAD, OPTIONS. Any other method, like DELETE on that path, gets 405 Method Not Allowed, also with an Allow header, from HttpRequestMethodNotSupportedException.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which HTTP methods are safe and which are idempotent? How does Spring MVC handle HEAD...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/17e715a837/