There was an unexpected error (type=Internal Server Error, status=500).
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 "<async-supported>true</async-supported>" to servlet and filter declarations in web.xml.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
最近刚刚接触WebFlux“响应式”接口,但是一直报这个错误,提示我在web.xml开启异步支持,但是我是SpringBoot项目= =,于是开始网上查找。
网上普遍给出的答案是 :
@Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new DispatcherServlet(), "/");
registration.setAsyncSupported(true);
return registration;
}
但是我配置了,却有了新错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method dispatcherServletRegistration in org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration required a bean of type 'org.springframework.web.servlet.DispatcherServlet' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.web.servlet.DispatcherServlet' in your configuration.
这里不是很清楚为什么没有注入,但是解决方法就是把DispatcherServlet注入进去即可解决。
以下是我整个class
@Configuration
public class OnlyConfiguration{
// @Autowired
// private DispatcherServlet dispatcherServlet;
/**
* 开启异步支持
* @return
*/
@Bean
public ServletRegistrationBean dispatcherServletBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(
this.dispatcherServlet(), "/");
registration.setAsyncSupported(true);
return registration;
}
@Bean
DispatcherServlet dispatcherServlet(){
return new DispatcherServlet();
}
}
问题还是没有解决
后来发现:
在Apllication.class 启动文件中,存在
@ServletComponentScan(xxx.xxx)
注解
每一个filter相当于是一个Servlet,所以在xxx.xxx这个扫描路径下所有的Filter也需要加
asyncSupported = true
(如果访问的接口也需要异步支持)