I'm always excited to take on new projects and collaborate with innovative minds.

Email

contact@niteshsynergy.com

Website

https://www.niteshsynergy.com/

Http Methods & Status Code

Here’s an outline for your blog on HTTP status codes (400, 401, 200) and HTTP methods in REST API using Spring Boot & Spring MVC. It includes use cases with real-time examples, such as employee management, gaming, teaching, and food order payments.

HTTP Status Codes & Methods in REST API with Spring Boot & Spring MVC

1. Introduction

In RESTful services, HTTP status codes and methods play a crucial role in determining the success or failure of requests. Understanding how to use these correctly helps in building robust APIs. In this post, we’ll look at common HTTP status codes (200, 400, 401) and HTTP methods (GET, POST, PUT, DELETE) in the context of Spring Boot and Spring MVC, with real-time use cases for employee management, gaming, teaching, and food order payments.
 

2. HTTP Status Codes

2.1. HTTP 200 – OK

The HTTP 200 status code indicates a successful request. It’s the standard response for successful GET, POST, PUT, and DELETE requests.

Use Case: Employee Management

In an employee management system, after adding or updating an employee, a response with a 200 OK status is sent to confirm that the request was successfully processed.

Sample Code
image-104.png

2.2. HTTP 400 – Bad Request

The HTTP 400 status code indicates that the server could not process the request due to client-side errors, such as invalid data or missing parameters.

Use Case: Gaming

In a gaming API, if a player sends an invalid move (e.g., a non-existent character ID), the server responds with a 400 Bad Request status.

Sample Code
image-105.png

2.3. HTTP 401 – Unauthorized

HTTP 401 status code means the client is unauthorized to access the requested resource, typically due to missing or invalid authentication.

Use Case: Teaching Platform

In a teaching platform API, a teacher or student attempting to access resources without being logged in would get a 401 Unauthorized status.

Sample Code
image-106.png

 

3. HTTP Methods

3.1. GET

The GET method retrieves data from the server.

Use Case: Food Order System

In a food order system, retrieving a list of menu items using a GET request.

Sample Code
image-107.png

3.2. POST

The POST method is used to send data to the server, such as creating a new record.

Use Case: Employee Management

Creating a new employee record.

Sample Code
image-108.png

3.3. PUT

The PUT method is used to update an existing resource.

Use Case: Gaming

Updating a player’s status in a game.

Sample Code
image-109.png

3.4. DELETE

The DELETE method is used to remove a resource from the server.

Use Case: Food Order System

Deleting an item from a customer’s food order.

Sample Code
image-110.png
image-111.png

5. HTTP 201 – Created

The HTTP 201 status code indicates that a request has been successfully processed and resulted in the creation of a new resource.

Use Case: Employee Management

When an employee is successfully created, return a 201 Created status.

Sample Code
image-112.png

6. HTTP 202 – Accepted

The HTTP 202 status code indicates that the request has been accepted for processing, but the processing has not been completed.

Use Case: Gaming

When a game action is initiated, but not completed yet (e.g., a server-side action like saving game progress), return HTTP 202.

Sample Code
image-113.png

 

7.HTTP 203 – Non-Authoritative Information

The HTTP 203 status code indicates that the response metadata is not from the origin server but from a local or third-party copy.

Use Case: Teaching Platform

In an online teaching platform, if course data is fetched from a cached version, this status code may be used.

Sample Code
image-114.png

8.HTTP 204 – No Content

The HTTP 204 status code indicates that the request has been successfully processed, but there is no content to return.

Use Case: Food Order System

When a food order is successfully updated (e.g., a customer changes their delivery address), return HTTP 204 No Content.

Sample Code
image-115.png

 

9. HTTP 301 – Moved Permanently

The HTTP 301 status code indicates that the resource requested has been permanently moved to a new location.

Use Case: Food Order System

If a food delivery address API has moved to a new endpoint, this status code is returned.

Sample Code
image-116.png

10.HTTP 302 – Found (Temporary Redirect)

The HTTP 302 status code indicates that the resource is temporarily located at a different URI.

Use Case: Gaming

If a player's request is temporarily redirected to a new game server for load balancing.

Sample Code
image-117.png

11.HTTP 400 – Bad Request

This indicates that the request could not be understood or was missing required parameters.

Use Case: Food Order System

If a customer places an incomplete order (missing item details), return HTTP 400.

Sample Code
image-118.png

12. HTTP 401 – Unauthorized

This status indicates that the request lacks valid authentication credentials.

Use Case: Teaching Platform

A student or teacher attempting to access course content without being logged in should receive this status.

Sample Code
image-119.png

13. HTTP 402 – Payment Required

This status code is not widely used, but it could indicate that payment is required to proceed with the request.

Use Case: Food Order Payment

In a food ordering system, the user is asked to pay before placing an order.

Sample Code
image-120.png

14. HTTP 403 – Forbidden

This status indicates that the server understands the request but refuses to authorize it.

Use Case: Teaching Platform

If a user tries to access restricted courses they don't have permission for, return HTTP 403.

Sample Code

 

image-121.png

15. HTTP 404 – Not Found

This status indicates that the requested resource could not be found.

Use Case: Employee Management

When an employee with a specific ID does not exist in the system.

Sample Code
image-122.png

16. HTTP 500 – Internal Server Error

This indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

Use Case: Gaming

When there’s an unexpected error in the game server (e.g., database failure).

Sample Code
image-123.png

17. HTTP 502 – Bad Gateway

This status code indicates that the server received an invalid response from the upstream server.

Use Case: Food Order Payment

If a food payment gateway returns an error, a 502 Bad Gateway status could be returned.

Sample Code
image-124.png
5 min read
Nov 17, 2024
By Nitesh Synergy
Share