Created
February 18, 2021 14:27
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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