Reputation: 480
I need to create a Filter and modify header values set in request Object. How we can modify headers in request Object using Filter?, there is no setHeader method available in request Object.
Upvotes: 2
Views: 17218
Reputation: 1
You can import Collectors using
import java.util.stream.Collectors;
Add the method below into your class.
private Map<String, String> convertHeadersToLowerCase(Map<String, String> headers) {
return headers
.entrySet()
.stream()
.collect(Collectors.toMap(entry -> entry.getKey().toLowerCase(), entry -> entry.getValue()));
}
Then before returning headers in the response, you should ensure they are converted before returning by adding the following into the Controller of your Response method:
requestHeaders = convertRequestHeadersToLowerCase(requestHeaders);
Upvotes: -1
Reputation: 17235
You can use javax.servlet.http.HttpServletRequestWrapper
to wrap the HttpServletRequest
object passed by the server.
In the wrapper class you need to override getHeader
method and return modified value of header.
You can refer to similar post over here Modify request parameter with servlet filter
Upvotes: 5