@ResponseHeader annotation
- @ResponseHeader allows easily adding and modifying HTTP response headers from controller methods in a clean way.
Syntax:
@ResponseHeader(
name = "headerName",
description = "",
response = String.class,
responseContainer = ""
)
Attributes:
- name: The name of the HTTP response header to add.
- description: An optional description of the header’s purpose or usage.
- response: The type of the header value. This is typically a String, but it can be any Java type.
- responseContainer: An optional indication of whether the header value is wrapped in a container, such as a list or set.
How to add/modify headers in the HTTP response:
- Using the HttpServletResponse object
- Using the HttpHeaders class
- Using the ResponseEntity class
Use cases for @ResponseHeader in controller methods:
- Security Enhancements
- Customizing Response Metadata
- Custom Response Handling
- Link Management
Working With HTTP Headers in Spring MVC: A Guide to @RequestHeader and @ResponseHeader
HTTP headers are key-value pairs that are sent along with HTTP requests from clients and responses from servers. Spring MVC provides various ways to access and manipulate HTTP headers in controllers.
Below are some ways to access and manipulate HTTP headers:
- HttpServletRequest and HttpServletResponse object
- Filter interfaces
- WebMvcConfigurer
- @ModelAttribute