Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get header from request in service layer of Spring Boot application

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.

like image 494
Uladzislau Kaminski Avatar asked Oct 12 '17 15:10

Uladzislau Kaminski


1 Answers

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.

like image 116
wkubasik Avatar answered Oct 14 '22 22:10

wkubasik