How to set respond header values in Spring Boot rest service method?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Spring Boot, response headers are usually set for caching, content disposition, custom metadata, or location information after resource creation. The cleanest implementation depends on where the header value comes from: controller logic, framework infrastructure, or low-level servlet code.
Prefer ResponseEntity in Controller Methods
For most REST endpoints, ResponseEntity is the best choice because it keeps the status code, headers, and body in one return value. That makes the method easy to test and avoids mixing transport details with servlet APIs.
This pattern scales well when you need to add conditional headers such as ETag, Location, or a download filename. The method remains declarative instead of mutating shared response state.
Use HttpServletResponse for Direct Streaming
If you are writing directly to the output stream, the servlet response is still valid. It is especially common for file downloads where the body is produced incrementally.
This is more imperative, so use it when you really need stream control. For ordinary JSON responses, ResponseEntity is usually easier to maintain.
Returning Headers for Resource Creation
REST APIs often need to return a Location header after creating a resource. Spring makes this straightforward with ResponseEntity.created.
That produces a conventional API response without forcing you to manipulate headers separately from the status code.
Keep Cross-Cutting Headers Out of Individual Methods
Some headers do not belong inside every controller. Security headers, CORS policy, and many cache rules are better handled with filters, interceptors, or Spring Security configuration. The controller method should usually set only headers that are specific to that endpoint's business behavior.
That separation matters because duplicated header logic becomes inconsistent quickly. If five endpoints all set Cache-Control manually, one of them will eventually drift. Infrastructure-level headers should live in infrastructure code.
Testing also gets easier when the header logic stays explicit. A MockMvc test can assert Location, Cache-Control, or custom metadata directly from the controller response without having to inspect servlet internals.
Common Pitfalls
- Using
HttpServletResponseeverywhere even whenResponseEntitywould be simpler and easier to test. - Setting the same cross-cutting headers in many controller methods instead of a filter or centralized configuration.
- Forgetting that some headers, such as
Content-Disposition, require careful quoting and formatting. - Writing to the response output stream and also trying to return a normal body from the same method.
- Adding custom headers but not checking whether downstream proxies or browsers actually expose or preserve them.
Summary
- '
ResponseEntityis the usual Spring Boot answer for setting headers in a REST method.' - '
HttpServletResponseis appropriate when you need low-level streaming control.' - Resource-creation endpoints commonly use
LocationthroughResponseEntity.created. - Endpoint-specific headers belong in the controller; cross-cutting headers belong in shared infrastructure.
- Choose the highest-level API that still matches the response you need to build.
Related reading
- How to set the Content-Type header for an HttpClient request?
- How to set timeout in Retrofit library?
- How to set timeout in Retrofit library?
- How to set up an OAuth2 Authentication Provider with AWS API Gateway?
- How to set selected item of Spinner by value, not by position?
- How to set specific Java version to Maven?
- How to specify a prefix to a service exposed with an ingress
- How to specify all ports in Security group - CloudFormation

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.