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

发布于 2024-07-14 02:15:30 字数 122 浏览 5 评论 0原文

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

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

I am using Spring annotations, I can pass the HttpRequestContext from the Controller to the Service.

I am looking for a static way or any better solution than passing RequestContext around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

假装爱人 2024-07-21 02:15:31

您可以将 HttpServletRequest 添加为控制器处理程序方法中的参数。 像这样:

@GetMapping("/hello")
public String hello(HttpServletRequest request) {
    //...
    return "Hello world!";
}

可以在此处找到支持的控制器方法参数和返回类型的列表:
https://docs。 spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-methods

You can add HttpServletRequest as a parameter in controller handler method. Like this:

@GetMapping("/hello")
public String hello(HttpServletRequest request) {
    //...
    return "Hello world!";
}

List of supported controller method arguments and return types can be found here:
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-methods

水中月 2024-07-21 02:15:31

这对我有用:

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

This is what works for me:

HttpServletRequest request = 
    ((ServletRequestAttributes) Objects.requireNonNull(
        RequestContextHolder.getRequestAttributes()))
    .getRequest();
吾家有女初长成 2024-07-21 02:15:30

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

If you are using spring you can do the following:

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;
}
扛起拖把扫天下 2024-07-21 02:15:30

或者java8方式

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

Or the java8 way

public static Optional<HttpServletRequest> getCurrentHttpRequest() {
    return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
        .filter(ServletRequestAttributes.class::isInstance)
        .map(ServletRequestAttributes.class::cast)
        .map(ServletRequestAttributes::getRequest);
}
不美如何 2024-07-21 02:15:30

我知道,这已经是答案了。 我想标记为新更新。 我们可以使用@Autowired注入HttpServletRequest。 它在springboot上运行良好。

@Autowired
private HttpServletRequest httpServletRequest;

I know, it is already answer. I would like to mark as new update. we can inject HttpServletRequest by using @Autowired. It is working fine at springboot.

@Autowired
private HttpServletRequest httpServletRequest;
蝶舞 2024-07-21 02:15:30

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

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

For reference, this would be the Kotlin way:

    val currentRequest: HttpServletRequest?
        get() = (RequestContextHolder.getRequestAttributes() as? ServletRequestAttributes)?.request
×纯※雪 2024-07-21 02:15:30

为了响应上面的 @andrebrait 评论“或者 Java 8 方式”,这些方法存在于 ServletRequestAttributes.class

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

In response to @andrebrait comments above "Or the Java 8 way", the methods are present on the ServletRequestAttributes.class

    public static Optional<HttpServletRequest> getCurrentHttpRequest() {
        return
            Optional.ofNullable(
                RequestContextHolder.getRequestAttributes()
            )
            .filter(ServletRequestAttributes.class::isInstance)
            .map(ServletRequestAttributes.class::cast)
            .map(ServletRequestAttributes::getRequest);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文