Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HttpServletRequest详细信息在@Async Spring中返回null_Java_Spring_Spring Boot_Tomcat_Servlets - Fatal编程技术网

Java HttpServletRequest详细信息在@Async Spring中返回null

Java HttpServletRequest详细信息在@Async Spring中返回null,java,spring,spring-boot,tomcat,servlets,Java,Spring,Spring Boot,Tomcat,Servlets,我想提取传入请求的URI 我的应用程序中有以下代码-a@RequestMapping,它是@Async。我想通过request.getRequestURI()提取路径URI,但当@Async注释存在时,它返回null,否则,当它不存在时,输出符合要求。如果是,原因是什么?如何使用@Async获得相同的输出?删除@Async对我来说不是一个容易的选择,因为我想用它来提高性能 @Async @RequestMapping("{name}/**") @ResponseBody public void

我想提取传入请求的URI

我的应用程序中有以下代码-a
@RequestMapping
,它是
@Async
。我想通过
request.getRequestURI()
提取路径URI,但当
@Async
注释存在时,它返回
null
,否则,当它不存在时,输出符合要求。如果是,原因是什么?如何使用
@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
}
@Async
@请求映射(“{name}/**”)
@应答器
公开无效收入申请(
@路径变量(“名称”)字符串名称,
HttpMethod方法,
HttpServletRequest请求,
HttpServletResponse(响应)
{

String URI=request.getRequestURI();//我能找到的关于空
HttpServletRequest
原因的最接近的线索是对这篇文章答案的评论:

要解决您的问题,您可以尝试以下两种方法:

  • 由于您的方法是
    void
    ,因此您可以从
    incomingRequest
    方法手动启动一个新线程,例如使用

  • 或者,尝试从您的方法返回
    CompletableFuture
    ,如下所示:

  • @Async
    @请求映射(“{name}/**”)
    @应答器
    公共可补全未来收入申请(
    @路径变量(“名称”)字符串名称,
    HttpMethod方法,
    HttpServletRequest请求,
    HttpServletResponse(响应)
    {
    String URI=request.getRequestURI();//应获取正确的非空路径
    返回CompletableFuture.completedFuture(空);
    }
    
    我在本地尝试了这个方法,它对我有效,因此我不确定还有什么其他原因会导致空值。但是,我建议您创建一个服务类,该类接受您的请求数据并对其进行处理。您可以使用@Async标记服务方法,而不是使用控制器,并从控制器调用服务。W如果他返回了一个可调用的,他还需要
    @Async
    吗?我想这样做,Spring就会知道异步执行。我不这么认为。如果没有注释,它仍然会在http请求线程上运行。你可以自己尝试并检查当前线程。实际上,刚刚尝试过。它确实有效,但你必须返回一个可调用的,或者lambda wil我做了
    return()->{log.debug(“callable:+request.getRequestURI());return null;};
    。这里有更多信息:。但无论哪种方式,OP都没有理由得到null,这是个谜。为了补充,我尝试了所有4种方式(@Asyc、普通方式、callable和@Async+CompletableFuture)。在所有情况下,getRequestURI都不是null,并且是正确的值,因此我怀疑这里还有其他原因。发生这种情况时,您对路径使用了什么?
    @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);
    }