Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save artemptushkin/42208cc6408933e214db57406c2dfa1f to your computer and use it in GitHub Desktop.
Save artemptushkin/42208cc6408933e214db57406c2dfa1f to your computer and use it in GitHub Desktop.
This gist demonstrates how to get dynamically a value from request header in Spring web stack on any layer different from `@Controller`
@Component
@RequiredArgsConstructor
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class HeaderValueProvider implements Supplier<String> { //supplier interface just for the API segregation
private final HttpServletRequest httpServletRequest;
@Override
public String get() {
return httpServletRequest.getHeader("someHeader");
}
}
/* ... */
/* Usage */
@RestController
@RequestMapping("/demo")
@RequiredArgsConstructor
public class TestController {
private final Supplier<String> headerValueProvider;
@GetMapping("/fromHeader")
public String valueFromYourHeader() {
return headerValueProvider.get();
}
}
/* Usage */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment