when Dispatcher is ERROR,can't use other thread to write data to client
public class Error404Filter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
AsyncContext asyncContext = request.startAsync(request, response);
Runnable t= new Runnable() {
@Override
public void run() {
try {
ServletResponse servletResponse=asyncContext.getResponse();
servletResponse.setContentType("application/json;charset=UTF-8");
asyncContext.getResponse().getWriter().write("aaaa");
asyncContext.getResponse().flushBuffer();
asyncContext.getResponse().getWriter().close();
asyncContext.complete();
} catch (java.lang.Throwable e) {
e.printStackTrace();
}
}
};
ExecutorService threadPoolExecutor= Executors.newFixedThreadPool(10);
threadPoolExecutor.execute(t);
}
}
@Bean
public FilterRegistrationBean create404Filer(){
Error404Filter filter=new Error404Filter();
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setDispatcherTypes(DispatcherType.ERROR);
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setOrder(100);
// filterRegistrationBean.setAsyncSupported(true);
return filterRegistrationBean;
}
how can i fix the error
Comment From: wilkinsona
It's hard to say how to fix the error as you haven't shown us the error. If you would like us to spend some time investigating, please take the time to provide a minimal sample that reproduces the problem. You can do so by zipping it up and attaching it to this issue or by pushing it to a separate repository on GitHub.
Comment From: allon2
springbooterror.zip if i comment the line filterRegistrationBean.setDispatcherTypes(DispatcherType.ERROR); I will get the response quickly,but when DispatcherType is Error,can't get the response. the file already in the zip
Comment From: wilkinsona
Thanks for the sample.
It's still not clear to me exactly what error you are seeing. I can produce a few different errors using the sample, but never on the first request. Without knowing more about the intended behaviour of your filter, it's hard to know what is going wrong. I can tell, however, the Spring Boot isn't involved. Once the filter has been registered, how it's called and how the servlet API behaves is down to the servlet container that you're using. If I exclude spring-boot-starter-tomcat
and use spring-boot-starter-jetty
instead, I receive what I imagine is the expected response:
$ curl localhost:8080/notfound -i
HTTP/1.1 404 Not Found
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
aaaa
If you wish to pursue this, you may want to open a Tomcat issue. If you do so, please take the time to describe the error that you are seeing and the exact steps that are needed to recreate it.
Comment From: allon2
when the DispatcherType is Error ,i want also can get the "aaaa" string in the response
Comment From: allon2
thanks your response