Request vs Response - Complete Java & Spring Boot Interview Guide
Learn the differences between HTTP requests and responses, request lifecycle, response lifecycle, headers, body, metadata, Spring Boot implementation, production scenarios, common mistakes, best practices, and 15 interview questions with answers.
Introduction
Every HTTP communication consists of two parts:
- HTTP Request
- HTTP Response
A client sends a request to ask for an operation.
The server processes the request and returns a response.
Understanding requests and responses is essential because every REST API, GraphQL API, gRPC gateway, API Gateway, Load Balancer, Reverse Proxy, Browser, Mobile App, and Microservice relies on this communication model.
What is an HTTP Request?
An HTTP Request is a message sent from a client to a server requesting an operation.
Example:
GET /api/v1/customers/101 HTTP/1.1
Host: api.codewithvenu.com
Accept: application/json
Authorization: Bearer eyJhb...
A request contains:
- Request Line
- Request Headers
- Optional Request Body
What is an HTTP Response?
An HTTP Response is a message returned by the server after processing a request.
Example:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id":101,
"name":"Venu"
}
A response contains:
- Status Line
- Response Headers
- Optional Response Body
Complete Communication Flow
Browser
↓
HTTP Request
↓
Load Balancer
↓
API Gateway
↓
Spring Boot Controller
↓
Service
↓
Repository
↓
Database
↓
Repository
↓
Service
↓
Controller
↓
HTTP Response
↓
Browser
Request Structure
Explain in detail:
- Request Line
- HTTP Method
- URI
- Query Parameters
- Path Variables
- Headers
- Cookies
- Body
Include examples for:
- GET
- POST
- PUT
- PATCH
- DELETE
Response Structure
Explain:
- Status Line
- Status Code
- Headers
- Body
Examples:
- 200
- 201
- 204
- 400
- 401
- 403
- 404
- 409
- 500
Request Headers
Explain each header with examples:
- Authorization
- Accept
- Content-Type
- User-Agent
- Host
- Cache-Control
- If-None-Match
- If-Match
- Accept-Encoding
- Accept-Language
- Cookie
- X-Correlation-ID
Response Headers
Explain:
- Content-Type
- Content-Length
- Cache-Control
- ETag
- Set-Cookie
- Retry-After
- Location
- Server
- WWW-Authenticate
- Content-Encoding
Spring Boot Example
Create a complete CRUD example including:
- DTO
- Controller
- Request DTO
- Response DTO
- Validation
- ResponseEntity
- Global Exception Handler
Show:
@GetMapping
@PostMapping
@PutMapping
@PatchMapping
@DeleteMapping
Include:
- Request JSON
- Response JSON
- HTTP headers
- Status codes
Request Processing Flow
Explain step-by-step:
Client
↓
DispatcherServlet
↓
Handler Mapping
↓
Controller
↓
Validation
↓
Service
↓
Repository
↓
Database
↓
Entity
↓
DTO
↓
Jackson
↓
JSON
↓
Client
Explain every step.
Serialization vs Deserialization
Explain:
Incoming JSON
↓
Java Object
↓
Business Logic
↓
Java Object
↓
JSON Response
Include Jackson examples.
Request Validation
Explain:
- @Valid
- @NotNull
- @NotBlank
- @Pattern
- BindingResult
Include validation error responses.
ResponseEntity
Explain:
- OK
- Created
- NoContent
- BadRequest
- Status()
Examples:
ResponseEntity.ok()
ResponseEntity.created()
ResponseEntity.noContent()
ResponseEntity.status()
Common Response Formats
Success
Validation Error
Business Error
Authentication Error
Authorization Error
Server Error
Use a standardized error response model.
Large Payload Handling
Explain:
- Pagination
- Streaming
- Compression
- Chunked responses
File Upload Request
Multipart request example.
File Download Response
Explain:
- application/pdf
- application/octet-stream
- Content-Disposition
Production Scenarios
Include scenarios such as:
- Missing Authorization header
- Invalid Content-Type
- Invalid Accept header
- Large request body
- Timeout
- Slow client
- Reverse proxy modifications
- API Gateway header injection
- Correlation IDs
- Trace IDs
- Request replay
- Duplicate requests
Common Mistakes
Explain at least:
- Returning 200 for errors
- Ignoring validation
- Logging sensitive request bodies
- Returning internal exceptions
- Missing correlation IDs
- Returning inconsistent JSON
- Using entities as API responses
- Exposing database IDs unnecessarily
- Accepting huge payloads
- Ignoring request size limits
Best Practices
Include 20+ best practices for production APIs.
Production Architecture
Browser
↓
CDN
↓
Load Balancer
↓
WAF
↓
API Gateway
↓
Spring Boot
↓
Kafka
↓
Database
↓
Redis
↓
Response
Explain each component.
15 Interview Questions
- What is an HTTP Request?
- What is an HTTP Response?
- Difference between Request Headers and Response Headers?
- What is Request Body?
- What is Response Body?
- Difference between Path Variable and Query Parameter?
- What is ResponseEntity?
- Why should DTOs be used instead of Entities?
- What is Serialization?
- What is Deserialization?
- How does Spring Boot process an HTTP Request?
- What happens after Controller returns an object?
- How does Jackson work?
- How are validation errors returned?
- How would you design request and response models for enterprise APIs?
Quick Revision
Include a one-page comparison table:
| Request | Response |
|---|---|
| Sent by Client | Sent by Server |
| Contains Method | Contains Status Code |
| Contains Request Headers | Contains Response Headers |
| Optional Body | Optional Body |
| Starts Communication | Ends Communication |
Key Takeaways
- Every API call consists of a request and a response.
- Requests describe what the client wants.
- Responses describe the outcome.
- DTOs should isolate API contracts from persistence models.
- Validation, serialization, and standardized error handling are essential for enterprise APIs.
- Understanding request and response flow is foundational for designing secure, scalable, and maintainable APIs.