What is the difference between returning a ResponseEntity vs directly returning an object in a REST API?

Returning ResponseEntity from a REST controller gives explicit control over the HTTP status code, headers, and body, while returning a plain object lets Spring Boot handle those details automatically with a default 200 OK response.

Key Points

Example

A POST endpoint that creates a resource typically returns ResponseEntity.status(HttpStatus.CREATED).body(resource) so clients get a 201 with a Location header, whereas a simple GET endpoint can just return the resource object directly.

@PostMapping("/orders")
public ResponseEntity<Order> createOrder(@RequestBody OrderDto dto) {
    Order created = orderService.create(dto);
    return ResponseEntity.status(HttpStatus.CREATED).body(created);
}

@GetMapping("/orders/{id}")
public Order getOrder(@PathVariable Long id) {
    return orderService.findById(id);
}

Interview Tip

A concise interview answer is

I return ResponseEntity when I need to control the status code or headers explicitly, like a 201 with a Location header on creation. For straightforward 200 OK responses where no customization is needed, returning the object directly is simpler and Spring wraps it automatically.

Last updated: 16 July 2026

Previous question: How do you configure and connect multiple databases in a Spring Boot application?

Next question: You are tasked with designing a series of new RESTful endpoints for a complex product inventory system. What best practices would you follow to ensure scalability, maintainability, and performance?

More Spring Boot - Level IV (Advanced) interview questions

All Spring Boot - Level IV (Advanced) questions