7Http5Code4Param [Link]
com/7http5code4param
Appearance Add new Edit this post Nitesh Synergy
7Http5Code4Param
Here’s an outline on Nitesh Synergy 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.
@RequestParam vs @QueryParam vs @PathVariable vs
@PathParam
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
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
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.
1 of 7 02/11/25, 10:21 am
7Http5Code4Param [Link]
Appearance Add new Edit this post Use Case: Teaching Platform Nitesh Synergy
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
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
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
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
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
2 of 7 02/11/25, 10:21 am
7Http5Code4Param [Link]
Appearance Add new Edit this post Nitesh Synergy
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
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
[Link] 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
8.H
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
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
3 of 7 02/11/25, 10:21 am
7Http5Code4Param [Link]
Appearance Add new Edit this post Nitesh Synergy
[Link] 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
[Link] 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
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
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
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
4 of 7 02/11/25, 10:21 am
7Http5Code4Param [Link]
permission for, return HTTP 403.
Appearance Add new Edit this post Nitesh Synergy
Sample Code
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
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
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
@RequestParam vs @QueryParam vs @PathVariable vs @PathParam
Here's a comparison of the four annotations commonly used in
Spring and JAX-RS (Java API for RESTful Web Services) for
extracting parameters from a request:
1. @RequestParam (Spring Framework):
• Used to extract query parameters or form
parameters from the request URL in Spring MVC
or Spring Boot.
• Typically used for GET requests with URL parameters
or form submissions.
5 of 7 02/11/25, 10:21 am
7Http5Code4Param [Link]
Appearance Add new Edit this post Nitesh Synergy
2. @QueryParam (JAX-RS):
• Similar to @RequestParam in Spring but used in
JAX-RS (used in frameworks like Jersey or
RESTEasy).
• Extracts query parameters from the URL,
typically used with HTTP GET.
URL: /employees?department=HR
3. @PathVariable (Spring Framework):
Used to extract variables from the URI path, typically
used with RESTful endpoints where the parameter is part
of the URL path.
Commonly used with dynamic URIs like /employees/{id} .
4. @PathParam (JAX-RS):
Similar to @PathVariable in Spring but used in JAX-RS.
Extracts parameters from the URI path.
2. Feature @RequestParam (Spring) @QueryParam (JAX-RS) @PathVariable (Spring)
Framework Spring MVC / Spring Boot JAX-RS (Jersey, RESTEasy, etc.) Spring MVC / Spring Boot
Used to extract query
Extracts values from the path
Usage parameters or form data Extracts query parameters from the URL
of the URL
from the URL or body
Typically used in GET, Typically used in GET, POST,
HTTP Typically used in GET, POST, PUT,
POST, PUT, DELETE (for PUT, DELETE (for dynamic path
Methods DELETE (for query data)
query/form data) segments)
Type of Query parameters or form Path parameters (part of the
Query parameters
Parameter parameters URL)
Example
/employees?department=HR /employees?department=HR /employees/123
URL
public List<Employee> public List<Employee> public Employee
Method
getEmployees(@RequestPara getEmployees(@QueryParam("department" getEmployeeById(@PathVariabl
Example
m String department) ) String department) e Long id)
Can be used with method Can be used with method
Annotation Can be used with method parameters in
parameters in controller parameters in controller
Placement JAX-RS resource classes
classes classes
Default Supports default values
Supports default values using Does not support default
Value using defaultValue
defaultValue attribute values
Support attribute
Default is required, but
Required/ Default is required, but can be made Always required if the path
can be made optional using
Optional optional using defaultValue is dynamic
required = false
For handling form data or
For handling resource
Common query parameters like For handling query parameters in
identifiers in RESTful web
Use Case search filters or RESTful web services
services
pagination
6 of 7 02/11/25, 10:21 am
7Http5Code4Param [Link]
Appearance Add new Edit this post 3. Summary of Use Cases: Nitesh Synergy
@RequestParam / @QueryParam : Extract query parameters,
often for filters, search criteria, or form data (e.g., ?
page=1&size=10 ).
@PathVariable / @PathParam : Extract path parameters,
commonly used for dynamic resource identifiers (e.g., /
employees/{id} ).
7 min read
Nov 17, 2024
By Nitesh Synergy
SHARE
7 of 7 02/11/25, 10:21 am