I have a Rest API. Inside one of the method in Rest API, I am trying to get the URL from each request sent to the API and later performing some operation based on the URL.
Found below possibilities in stack overflow to get the URL from the incoming request.
Example 1:
@Autowired
private HttpServletRequest request;
"Inside one method"
String url = request.getRequestURL().toString();
Example 2:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
String url = request.getRequestURL().toString();
Both will give the same result? If yes, then which one to use to get the URL from the incoming request?