Http Methods & Status Code & Spring Bean Scopes
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.
The HTTP 200 status code indicates a successful request. It’s the standard response for successful GET, POST, PUT, and DELETE requests.
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.
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.
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.
HTTP 401 status code means the client is unauthorized to access the requested resource, typically due to missing or invalid authentication.
In a teaching platform API, a teacher or student attempting to access resources without being logged in would get a 401 Unauthorized status.
The GET method retrieves data from the server.
In a food order system, retrieving a list of menu items using a GET request.
The POST method is used to send data to the server, such as creating a new record.
Creating a new employee record.
The PUT method is used to update an existing resource.
Updating a player’s status in a game.
The DELETE method is used to remove a resource from the server.
Deleting an item from a customer’s food order.
The HTTP 201 status code indicates that a request has been successfully processed and resulted in the creation of a new resource.
When an employee is successfully created, return a 201 Created status.
The HTTP 202 status code indicates that the request has been accepted for processing, but the processing has not been completed.
When a game action is initiated, but not completed yet (e.g., a server-side action like saving game progress), return HTTP 202.
The HTTP 203 status code indicates that the response metadata is not from the origin server but from a local or third-party copy.
In an online teaching platform, if course data is fetched from a cached version, this status code may be used.
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.
When a food order is successfully updated (e.g., a customer changes their delivery address), return HTTP 204 No Content.
The HTTP 301 status code indicates that the resource requested has been permanently moved to a new location.
If a food delivery address API has moved to a new endpoint, this status code is returned.
10.HTTP 302 – Found (Temporary Redirect)
The HTTP 302 status code indicates that the resource is temporarily located at a different URI.
If a player's request is temporarily redirected to a new game server for load balancing.
11.HTTP 400 – Bad Request
This indicates that the request could not be understood or was missing required parameters.
If a customer places an incomplete order (missing item details), return HTTP 400.
This status indicates that the request lacks valid authentication credentials.
A student or teacher attempting to access course content without being logged in should receive this status.
This status code is not widely used, but it could indicate that payment is required to proceed with the request.
In a food ordering system, the user is asked to pay before placing an order.
This status indicates that the server understands the request but refuses to authorize it.
If a user tries to access restricted courses they don't have permission for, return HTTP 403.
This status indicates that the requested resource could not be found.
When an employee with a specific ID does not exist in the system.
This indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
When there’s an unexpected error in the game server (e.g., database failure).
This status code indicates that the server received an invalid response from the upstream server.
If a food payment gateway returns an error, a 502 Bad Gateway status could be returned.
Scope | Description | Use Case | Spring MVC/Boot Context |
---|---|---|---|
Singleton | A single instance of the bean is created for the entire Spring context. | Default scope; shared across all requests and users. Example: Shared service like logging, database connection pooling. | The bean is created once and shared throughout the application. It’s ideal for stateless, shared components. |
Prototype | A new bean instance is created each time it is requested. | For beans that should be instantiated each time they are needed. Example: A form object for each HTTP request. | Every time a bean is requested (via @Autowired or @Bean ), a new instance is created. |
Session | A new bean instance is created for each HTTP session. | Used for session-specific data like user preferences, shopping cart, etc. | A new instance of the bean is created for each user session and persists throughout the session. It is cleared when the session expires. |
Application | A single bean is shared across all users and HTTP sessions. | Used for beans that must be shared globally across the entire application. Example: Application-wide settings. | The bean is created once and shared across all sessions, often used for global constants or configurations. |
Request | A new bean is created for each HTTP request. | For request-specific data, such as information specific to one HTTP request. Example: Form processing. | The bean is created once for each request and disposed of after the request is completed. It is typically used for request-scoped operations. |
Global Session | A session scope across multiple applications (in a portlet environment). | Rarely used outside of portlets, typically for sharing data in a global session across multiple applications. | Applicable in portlet-based environments where multiple applications need access to a common session across different portlets. |