Is there any way in spring boot to grab header from request in any point of application? Some static stuff will be great.
Please, be aware that @RequestHeader
does not work for me since I need this value on service layer.
You can inject HttpServletRequest
object in your service layer like this :
@Autowired
HttpServletRequest request;
private void method() {
request.getHeader("headerName");
}
but remember, that bean HttpServletRequest
has HTTP request scope. So, you can't inject that into asynchronous methods etc, because it will throw Runtime Exception
.
hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With