【问题标题】:Is there a static way to get the current HttpServletRequest in Spring有没有一种静态方法可以在 Spring 中获取当前的 HttpServletRequest
【发布时间】:2010-10-10 04:17:15
【问题描述】:

我正在使用 Spring 注释,我可以将 HttpRequestContext 从 Controller 传递给 Service。

我正在寻找一种静态方式或任何比传递RequestContext 更好的解决方案。

【问题讨论】:

    标签: spring servlets request


    【解决方案1】:

    如果您使用的是 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;
    }
    

    【讨论】:

    • 把它当作豆子?在这里使用'autowired'而不是静态方法如何?
    • 每隔几年我就会回到这个答案并重用这个 sn-p。谢谢!!!
    • 记得在你的web.xml 中声明org.springframework.web.context.request.RequestContextListener 作为监听器,这样才能正常工作。
    【解决方案2】:

    或者java8方式

    public static Optional<HttpServletRequest> getCurrentHttpRequest() {
        return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
            .filter(ServletRequestAttributes.class::isInstance)
            .map(ServletRequestAttributes.class::cast)
            .map(ServletRequestAttributes::getRequest);
    }
    

    【讨论】:

    • 您可以将过滤器替换为ServletRequestAttributes::isInstance
    • 然后您可以在过滤器之后添加一个地图到ServletRequestAttributes::cast,以避免在后续地图上投射。
    • Java 中都不存在这些方法。
    【解决方案3】:

    作为参考,这将是 Kotlin 的方式:

        val currentRequest: HttpServletRequest?
            get() = (RequestContextHolder.getRequestAttributes() as? ServletRequestAttributes)?.request
    

    【讨论】:

      【解决方案4】:

      针对上面“或 Java 8 方式”的@andrebrait cmets,方法存在于ServletRequestAttributes.class

          public static Optional<HttpServletRequest> getCurrentHttpRequest() {
              return
                  Optional.ofNullable(
                      RequestContextHolder.getRequestAttributes()
                  )
                  .filter(ServletRequestAttributes.class::isInstance)
                  .map(ServletRequestAttributes.class::cast)
                  .map(ServletRequestAttributes::getRequest);
          }
      

      【讨论】:

      • isInstance 是否仍然适用于子类?
      【解决方案5】:

      这对我有用:

      HttpServletRequest request = 
          ((ServletRequestAttributes) Objects.requireNonNull(
              RequestContextHolder.getRequestAttributes()))
          .getRequest();
      

      【讨论】:

        【解决方案6】:

        我知道,这已经是答案了。我想标记为新更新。我们可以使用@Autowired 注入HttpServletRequestspringboot 工作正常。

        @Autowired
        private HttpServletRequest httpServletRequest;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-02-07
          • 1970-01-01
          • 1970-01-01
          • 2011-03-02
          • 1970-01-01
          • 2014-10-27
          • 2011-08-30
          相关资源
          最近更新 更多