In my spring webservice I'm trying to get the url of my application like "http://localhost:8080/mycontext". My service does not contain any HttpServletRequest var, so I can I get it ?
1 Answer
You can get the current request via the RequestContextHolder like:
ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();
req.getContextPath();
req.getPathInfo();
Or you can just inject it via Spring into your service:
private @Autowired HttpServletRequest request;
1 Comment
anais1477
Thank you ! I did it with private @Autowired HttpServletRequest request;
HttpServletRequestcan be injected by Spring in your controller's methods, just add it in the parameters.