DEV Community

Cover image for Mastering HTTP Methods in Spring Boot: Quick Reference Guide
Pramod Boda
Pramod Boda

Posted on

Mastering HTTP Methods in Spring Boot: Quick Reference Guide

HTTP Methods: Definitions & Descriptions

HTTP Method Definition / Description
GET Retrieve data from a server at the specified resource. It is read-only and should have no side effects (i.e., not modify data). Commonly used for fetching lists, details, or static content.
POST Submit data to the server to create a new resource. The server processes the request and returns the new resource or a success status. Used in forms, sign-ups, data submissions.
PUT Replace an existing resource entirely with the provided data. If the resource does not exist, some APIs may create it (though this is debated). Used for complete updates.
PATCH Partially update an existing resource. Only the fields sent in the request are modified; others remain unchanged. More efficient than PUT for small changes.
DELETE Remove a resource from the server. It targets a specific resource or dataset. Often used for deleting users, posts, files, etc.
HEAD Same as GET, but only returns headers, not the response body. Useful for checking if a resource exists or retrieving metadata.
OPTIONS Returns allowed HTTP methods and CORS policies for a resource. Often used in preflight requests in cross-origin setups.
TRACE Echoes back the received request. Mostly used for debugging and testing, but generally disabled for security reasons.
CONNECT Establishes a tunnel, typically for HTTPS connections through proxies. Rarely used in app-level programming.

Mastering HTTP Methods in Spring Boot

HTTP Method Spring Boot Annotation Usage Description Typical Responses
GET @GetMapping Retrieve resource(s) without modifying anything. 200 OK, 204 No Content, 404 Not Found
POST @PostMapping Create a new resource. 201 Created, 400 Bad Request, 409 Conflict, 422 Unprocessable Entity, 500 Internal Server Error
PUT @PutMapping Replace an existing resource entirely. 200 OK, 204 No Content, 400 Bad Request, 404 Not Found
PATCH @PatchMapping Update a part of the existing resource. 200 OK, 204 No Content, 400 Bad Request, 404 Not Found
DELETE @DeleteMapping Delete a resource. 200 OK, 204 No Content, 404 Not Found, 400 Bad Request

Typical HTTP Response Codes

Code Meaning When to Use
200 OK Request succeeded (GET, PUT, PATCH, DELETE).
201 Created Resource created successfully (POST).
204 No Content Success with no body returned.
400 Bad Request Invalid input or request syntax.
401 Unauthorized Authentication is required or failed.
403 Forbidden Authenticated but not authorized.
404 Not Found Resource doesn’t exist.
409 Conflict Resource already exists or version conflict.
422 Unprocessable Entity Validation passed structurally but failed semantically.
500 Internal Server Error Unexpected error on server side.

Top comments (0)