2017-06-21 20 views
0

在我的spring webservice中,我試圖獲取我的應用程序的URL,如「http://localhost:8080/mycontext」。 我的服務不包含任何HttpServletRequest var,所以我可以得到它?在沒有HttpServletRequest的Spring服務中獲取上下文url

+1

您可能需要添加的HttpServletRequest作爲參數。 https://stackoverflow.com/questions/1490821/whats-the-best-way-to-get-the-current-url-in-spring-mvc – chomnoue

+1

'HttpServletRequest'可以在你的控制器的方法中被Spring注入將其添加到參數中。 – sjahan

回答

1

你可以通過像RequestContextHolder當前請求:

ServletRequestAttributes sra = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); 
HttpServletRequest req = sra.getRequest();  
req.getContextPath(); 
req.getPathInfo(); 

或者你也可以通過Spring注入到您的服務:

private @Autowired HttpServletRequest request; 
+0

謝謝!我用私有的@Autowired HttpServletRequest請求做了它; – anais1477

相關問題