0
在我的spring webservice中,我試圖獲取我的應用程序的URL,如「http://localhost:8080/mycontext」。 我的服務不包含任何HttpServletRequest var,所以我可以得到它?在沒有HttpServletRequest的Spring服務中獲取上下文url
在我的spring webservice中,我試圖獲取我的應用程序的URL,如「http://localhost:8080/mycontext」。 我的服務不包含任何HttpServletRequest var,所以我可以得到它?在沒有HttpServletRequest的Spring服務中獲取上下文url
你可以通過像RequestContextHolder當前請求:
ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();
req.getContextPath();
req.getPathInfo();
或者你也可以通過Spring注入到您的服務:
private @Autowired HttpServletRequest request;
謝謝!我用私有的@Autowired HttpServletRequest請求做了它; – anais1477
您可能需要添加的HttpServletRequest作爲參數。 https://stackoverflow.com/questions/1490821/whats-the-best-way-to-get-the-current-url-in-spring-mvc – chomnoue
'HttpServletRequest'可以在你的控制器的方法中被Spring注入將其添加到參數中。 – sjahan