我想提取传入请求的 URI。
我的应用程序中有以下代码 - a@RequestMapping
这是@Async
。我想通过提取路径 URIrequest.getRequestURI()
但它返回null
当。。。的时候@Async
注释存在,否则,当注释不存在时,输出将根据需要进行。这是有意的行为吗?如果是的话,为什么?我怎样才能获得相同的输出@Async
?去除@Async
对我来说这不是一个简单的选择,因为我想用它来提高性能。
@Async
@RequestMapping("{name}/**")
@ResponseBody
public void incomingRequest(
@PathVariable("name") String name,
HttpMethod method,
HttpServletRequest request,
HttpServletResponse response)
{
String URI = request.getRequestURI(); // <-- null with @Async
}
看来一般来说,所有与请求相关的参数都会丢失,这不是我想要的;我需要为我的程序提取它们。
最接近空原因的线索HttpServletRequest
我可以找到对这篇文章的答案的评论:HttpServletRequest 对象的生命周期是怎样的?
要解决您的问题,您可以在这里尝试两种方法:
由于你的方法是void
您可以手动启动一个新线程incomingRequest
方法使用,例如,完整的未来.
或者,尝试返回CompletableFuture
从你的方法来看,像这样:
@Async
@RequestMapping("{name}/**")
@ResponseBody
public CompletableFuture<Void> incomingRequest(
@PathVariable("name") String name,
HttpMethod method,
HttpServletRequest request,
HttpServletResponse response)
{
String URI = request.getRequestURI(); // should get the right non null path
return CompletableFuture.completedFuture(null);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)