Return ResponseEntity vs returning POJO
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In a Spring MVC or Spring Boot controller, returning a plain object and returning ResponseEntity both produce HTTP responses, but they communicate different levels of intent. A plain object is ideal when the default HTTP behavior is correct, while ResponseEntity is the tool to reach for when status codes, headers, or conditional response logic are part of the contract.
Returning a POJO
If a controller method returns a regular Java object and the method is in a @RestController, Spring serializes that object through an HttpMessageConverter, usually as JSON.
This is the cleanest style when the response is simply "return this representation with a normal success status." It keeps controller code short and lets Spring use the default 200 OK behavior.
That simplicity is a real advantage in CRUD endpoints where there is no need to alter headers or branch on multiple HTTP outcomes. The controller reads like business code instead of transport plumbing.
Returning ResponseEntity
ResponseEntity wraps the body together with HTTP metadata. Use it when the endpoint needs explicit control over status codes, response headers, or whether a body is present at all.
Here ResponseEntity expresses two important HTTP details: the response is 201 Created, and the Location header points to the new resource. Returning only a POJO would lose that semantic information unless another mechanism added it.
How to Decide Between Them
A useful rule is:
- return a POJO when the default status and headers are correct
- return
ResponseEntitywhen the HTTP envelope matters
Examples where ResponseEntity is usually the better fit include:
- '
201 Createdafter resource creation' - '
204 No Contentfor delete operations' - '
404 Not Foundfrom controller logic' - custom headers such as pagination or caching metadata
- conditional responses based on validation or authorization state
If every controller method returns ResponseEntity by habit, the code can become noisy. If no method ever returns it, developers start hiding transport decisions in exceptions or filters that may be harder to reason about.
Error Handling and Consistency
One subtle point is that controller design and global exception handling should work together. Many teams return plain objects for successful responses and let @ControllerAdvice handle error responses centrally. That often produces the cleanest architecture.
ResponseEntity is still valuable in that setup when the success path itself needs explicit HTTP control. The question is not which approach is universally better. The question is where the response metadata belongs for a given endpoint.
Common Pitfalls
- Returning
ResponseEntityeverywhere even when a plain object would be clearer. - Returning only POJOs from endpoints that need custom status codes or headers.
- Mixing ad hoc
ResponseEntityhandling with inconsistent exception handling. - Using controller return types to hide domain problems that should be modeled explicitly.
- Forgetting that transport concerns are part of the public API contract.
Summary
- Returning a POJO is best when default Spring response behavior is correct.
- '
ResponseEntityis best when status codes, headers, or empty bodies matter.' - Use
ResponseEntitydeliberately, not as boilerplate. - Keep success-path design aligned with your exception-handling strategy.
- Choose the return style that makes the HTTP contract obvious to readers and clients.
Related reading
- Returning JSON object as response in Spring Boot
- Ridiculously slow writes to Amazon DynamoDB PHP API
- RMI alternatives for bidirectional asynchronous calls and callbacks through firewalls or NAT
- RouteSpecificPool timeout Occuring processing HTTP request while using NIO
- Return value of JPA query when no matches found
- Returning from a finally block in Java
- Routing messages from Kafka to web socket clients connected to application server cluster
- RPC semantics of gRPC

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.