Java中对Spring控制器的异步支持

Java中对Spring控制器的异步支持,java,spring,spring-mvc,asynchronous,Java,Spring,Spring Mvc,Asynchronous,当我尝试调用我的spring web客户端时,我得到错误: HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java

当我尝试调用我的spring web客户端时,我得到错误:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding "true" to servlet and filter declarations in web.xml.
我没有访问权/我不想弄乱web.xml文件。我的spring控制器如下所示:

@Controller
@RequestMapping("/test/test")
public class MyController
{
     //Using DeferedResult and Http get / post
}

我尝试过的事情:将@Async放在类之上,放在get/post方法之上。还尝试将@EnableAsync置于类之上。如何在java代码中启用异步支持而不在web.xml中实现?无法在线找到太多帮助

如果您使用java风格的应用程序上下文而不是xml,下面的配置应该可以工作

@Configuration
@EnableAutoConfiguration
@EnableAsync
@ComponentScan("your.package")
public class AppConfig extends SpringBootServletInitializer implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(200);
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new MyAsyncExceptionHandler();
    }
}
这里还有asyncExceptionHandler的示例

public class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    static Logger log = Logger.getLogger(MyAsyncExceptionHandler.class.getName());

    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
        log.error("Exception message - " + throwable.getMessage());
        log.error("Method name - " + method.getName());
        for (Object param : obj) {
            log.error("Parameter value - " + param);
        }

    }

}

显示你的
web.xml
文件正如我所说,我没有访问web.xml文件的权限。我需要用java方式来做,是的,但是您的web.xml可能配置不正确。这不仅仅是“java方式”的问题。您是否在使用基于java的servlet配置?