在spring引导应用程序中获取联系路径

问题描述:

我发现了几种设置上下文路径的方法,但是在spring引导应用程序中没有找到获取上下文路径的方法。有一种方法可以通过@Value(“server.contextPath”)获得它,但我正在寻找更优雅的方式来获取它。在spring引导应用程序中获取联系路径

如果您愿意,您可以传入请求并使用request.getContextPath

查看Javadoc看看这是否适合你?

您可以在application.properties文件中设置contextPath。

是这样的:

server.contextPath=/yourChosenContextPath 

可以在控制器获取上下文路径为:

@RequestMapping(method = RequestMethod.GET, value = "/getUsers") 
public String getUsers(HttpServletRequest request) { 
    String path = request.getContextPath(); 
    //... 
}