gpt4 book ai didi

java - 使用 Spring RequestContextListener

转载 作者:行者123 更新时间:2023-12-01 06:03:27 31 4
gpt4 key购买 nike

如果我使用

@Autowired HttpServletRequest httpServletRequest;

我收到错误:

No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

然后在谷歌搜索时要求我使用:

<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

我可以从请求中获取 IP 地址。我的疑问是,如果在异步方法中使用此方法,当有多个请求并行传入时,IP 地址是否正确。

这是一个 Spring MVC 应用程序。有没有更好的方法来获取IP地址。我想在我的自定义提供商中记录 IP 地址。

最佳答案

我认为你使用它的方式是错误的。您可以在 Controller 方法调用中获取HttpServletRequest

@RestController
public class SomeController {

@RequestMapping(path = "/dosmtng")
public void doSomething(HttpServletRequest request, <other params>){
....
}

}

例如这样。因此,您可以从此处的请求中获取必要的属性并将它们放入其他方法中。

关于java - 使用 Spring RequestContextListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51535476/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com