Spring中有没有静态的方法来获取当前的HttpServletRequest

2024-04-08

我正在使用Spring注释,我可以通过HttpRequestContext从控制器到服务。

我正在寻找一种静态方式或任何比通过更好的解决方案RequestContext around.


如果您使用 spring,您可以执行以下操作:

public static HttpServletRequest getCurrentHttpRequest(){
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        return request;
    }
    logger.debug("Not called in the context of an HTTP request");
    return null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring中有没有静态的方法来获取当前的HttpServletRequest 的相关文章

随机推荐