Get header from request in service layer of Spring Boot application

Uladzislau Kaminski :

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.

wkubasik :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=472624&siteId=1