![]() |
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:
@RequestHeader annotation@RequestHeader annotation is used to bind a specific HTTP request header to a method parameter. The header value will be injected. Syntax:@RequestHeader( Attributes
How to Retrieve Header Values From the HTTP Request
Use Cases For @RequestHeader in Controller Methods:
Example of @RequestHeader AnnotationJava
The headers will be passed to the authToken and studentId parameters respectively. The method then validates the auth token and returns the student based on the studentId.
@ResponseHeader annotation
Syntax:@ResponseHeader( Attributes:
How to add/modify headers in the HTTP response:
Use cases for @ResponseHeader in controller methods:
Example of @ResponseHeader AnnotationJava
@ResponseHeader annotation indicates that the “Cache-Control” header should be added to the response generated by the “getStudent” method. The response can be cached for up to one hour before needing to be refreshed, according to the “max-age=3600” directive.
ConclusionIn summary, @RequestHeader and @ResponseHeader provide an elegant way to work with HTTP headers in Spring MVC. They simplify access to request headers and enable convenient manipulation of response headers, contributing to well-structured and informative responses. |
Reffered: https://www.geeksforgeeks.org
Advance Java |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |