DevNotes
Support
Spring Boot · Chapter 08

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping

HTTP methods like a backend architect — safe methods, idempotency, PUT vs PATCH, CRUD mapping, status codes (200, 201, 204, 400, 404, 409) and REST naming.

◷ 7 min read§ 17 sections? 7 interview questions{ } 8 code examples

#Introduction

One of the most misunderstood topics in REST API development. Many developers know @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping. But interviewers ask:

  • Why is GET different from POST?
  • What is idempotency?
  • Why shouldn't GET modify data?
  • What is the difference between PUT and PATCH?
  • When should you return 200 vs 201 vs 204?

After this chapter you'll understand HTTP methods like a backend architect.

#What are HTTP Methods?

Every request carries an HTTP method:

Text
GET /users/10 HTTP/1.1

The first word, GET, tells the server what operation you want it to perform.

REST API Example

HTTP methodURLMeaning
GET/usersFetch users
GET/users/10Fetch one user
POST/usersCreate a user
PUT/users/10Replace a user
PATCH/users/10Partially update a user
DELETE/users/10Delete a user

Spring Mapping Annotations

AnnotationHTTP method
@GetMappingGET
@PostMappingPOST
@PutMappingPUT
@DeleteMappingDELETE
@PatchMappingPATCH

@GetMapping("/users") is equivalent to @RequestMapping(value = "/users", method = RequestMethod.GET).

#1. @GetMapping — Retrieve Data

Purpose: retrieve data. Never modify data.

Java
@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return service.getUser(id);
    }
}

GET /users/10 returns:

JSON
{
  "id": 10,
  "name": "Rahul"
}
GET /users/10
Controller
Service
Repository
Database
JSON response

GET is a Safe Method

Safe means no modification on the server. Allowed: read, search, filter. Not allowed: insert, update, delete.

❌ Wrong:

Java
@GetMapping("/delete/{id}")
public void delete(@PathVariable Long id) {
    repository.deleteById(id);
}

Never delete data using GET — crawlers, link prefetching and caches may call GET URLs freely. Use DELETE.

#2. @PostMapping — Create Resources

Java
@PostMapping
public User save(@RequestBody User user) {
    return service.save(user);
}

POST /users with body:

JSON
{
  "name": "Rahul"
}
JSON
User object
Controller → Service → Repository
Database (ID generated)
JSON response

POST creates data:

BeforeAfter POST /users
1 John1 John, 2 Rahul

#3. @PutMapping — Replace a Resource

Purpose: replace an existing resource completely.

Database: Rahul, 25. PUT request:

JSON
{
  "name": "Rohit",
  "age": 30
}

Database becomes Rohit, 30 — the entire resource is replaced.

Java
@PutMapping("/{id}")
public User update(@PathVariable Long id, @RequestBody User user) {
    return service.update(id, user);
}

#4. @PatchMapping — Partial Update

Purpose: update only the required fields.

Database: Rahul, 25. PATCH request:

JSON
{
  "age": 26
}

Database becomes Rahul, 26 — the name is unchanged.

Java
@PatchMapping("/{id}")
public User updateAge(@PathVariable Long id, @RequestBody User user) {
}

#5. @DeleteMapping — Delete a Resource

Java
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
    service.delete(id);
}

DELETE /users/10 removes row 10.

#CRUD Mapping

CRUDHTTPSpring
CreatePOST@PostMapping
ReadGET@GetMapping
UpdatePUT / PATCH@PutMapping / @PatchMapping
DeleteDELETE@DeleteMapping

Remember this table.

#Safe vs Unsafe Methods

Safe means no modification.

MethodSafe
GET✓
POST✗
PUT✗
DELETE✗
PATCH✗

(HEAD and OPTIONS are also safe.)

#Idempotency

A favourite interview question.

  • GET — GET /users/10 100 times: no change in the database. ✓ Idempotent.
  • PUT — PUT {"name": "John"} once → John; 100 times → still John. ✓ Idempotent.
  • DELETE — delete once → user removed; delete again → still deleted. ✓ Idempotent. (The second call may return 404 — the response can differ, but the state doesn't.)
  • POST — POST /users twice creates two different rows (1 Rahul, 2 Rahul). ✗ Not idempotent.
  • PATCH — usually not idempotent. E.g. {"salary": "+1000"} called twice changes the salary twice. Some PATCH operations (setting a field to a fixed value) are idempotent, but the HTTP specification doesn't guarantee it.
MethodIdempotent
GET✓
PUT✓
DELETE✓
POST✗
PATCHUsually ✗

#HTTP Status Codes

SituationStatus
GET success200 OK
POST success201 Created
DELETE success (no body)204 No Content
Resource not found404 Not Found
Invalid JSON, validation failure, missing fields400 Bad Request
Duplicate email/username, optimistic-locking failure409 Conflict

For POST, prefer:

Java
return ResponseEntity
        .status(HttpStatus.CREATED)
        .body(savedUser);

#Complete REST Controller

Java
@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping
    public List<User> getAll() {
        return service.getAll();
    }

    @GetMapping("/{id}")
    public User get(@PathVariable Long id) {
        return service.get(id);
    }

    @PostMapping
    public User save(@RequestBody User user) {
        return service.save(user);
    }

    @PutMapping("/{id}")
    public User update(@PathVariable Long id, @RequestBody User user) {
        return service.update(id, user);
    }

    @PatchMapping("/{id}")
    public User patch(@PathVariable Long id, @RequestBody UserPatchDto dto) {
        return service.patch(id, dto);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return ResponseEntity.noContent().build();
    }
}

Complete Request Lifecycle (POST /users)

Client — HTTP POST
DispatcherServlet
HandlerMapping
@PostMapping method
Jackson → Java object
Service
Repository
Database
Saved entity
Jackson → JSON
HTTP response

#REST API Naming Best Practices

✅ Good❌ Bad
GET /users/getUsers
GET /users/10
POST /users/createUser
PUT /users/10/updateUser
DELETE /users/10/deleteUser

REST URLs should represent resources (nouns), not actions (verbs). The HTTP method already describes the action.

#Common Mistakes

  • Using GET to delete (@GetMapping("/delete/{id}")) — always use @DeleteMapping.
  • Using POST for updates — prefer PUT or PATCH.
  • Returning 200 after creation — prefer 201 Created.
  • Returning entities directly — in enterprise projects map Entity → DTO → JSON to protect internal models and control the API contract.

#Enterprise Example

Angular / React
GET /employees
EmployeeController
EmployeeService
EmployeeRepository
Hibernate
MySQL
Employee list → JSON
Frontend

#Interview Questions

Q1What is the difference between GET and POST?
  • GET retrieves data and should not modify server state.
  • POST creates a new resource or performs an operation that changes server state.
Q2What is idempotency?

An operation is idempotent if making the same request multiple times results in the same final state on the server.

Q3Which HTTP methods are idempotent?

GET ✓, PUT ✓, DELETE ✓; POST ✗. PATCH depends on the operation but is not guaranteed to be idempotent.

Q4What is the difference between PUT and PATCH?
  • PUT replaces the entire resource representation.
  • PATCH updates only the specified fields.
Q5Why should REST URLs use nouns instead of verbs?

Because the HTTP method already expresses the action; the URL should identify the resource. GET /users is preferable to /getUsers.

Q6Which status code should a successful POST return?

Typically 201 Created, often with the created resource in the body and ideally a Location header pointing to the new resource.

Q7What status code should DELETE return?
  • 204 No Content when no response body is returned.
  • 200 OK when returning additional information in the body.

#Key Takeaways

  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping are specialized forms of @RequestMapping.
  • GET reads, POST creates, PUT replaces, PATCH partially updates, DELETE deletes.
  • Know safe methods, idempotency and status codes — frequent interview topics.
  • Design REST APIs around resources (nouns) and let HTTP methods express the action.
  • Return appropriate status codes: 200, 201, 204, 400, 404, 409.