码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc 配置异步请求

时间:2018-09-09 00:37:24      阅读:857      评论:0      收藏:0      [点我收藏+]

标签:Stub   page   can   ram   pat   cannot   starting   总结   version   

最开始按照网上配置了一个servlet class 没有继承Filter .结果报错.网上有文章说是tomcat 启动加载的servlet-3.0- api 加载了 tomcat 安装目录下lib里边的自带的 servlet api 与 环境中api冲突 改成

<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>  

问题是配置文件里配置就是这样的

严重: Exception starting filter AsyncFilter
java.lang.ClassCastException: com.AsynDemo.AsynServ cannot be cast to javax.servlet.Filter
这个错误其实是没有继承Filter,重写一个过滤导致的

环境:

技术分享图片

技术分享图片

pom.xml

<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>

 

web.xml 配置

这个是把你重写的过滤类写入
<filter> <filter-name>pageResponse</filter-name> <filter-class>com.AsynDemo.AsynServlet</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>pageResponse</filter-name> <!-- 需要注意的是url-parttern 下面的这个路径项目名称是不需要写的直接以 "/" 代替 --> <url-pattern>/*</url-pattern> </filter-mapping>

<servlet>
<servlet-name>applicationContext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-common.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>  //
</servlet>


@Controller
@RequestMapping("/asyncServlet")
public class AsynServlet implements Filter
{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}


@RequestMapping(value="/testAsyn")
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
System.out.println("start");
PrintWriter out = response.getWriter();
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(5000);
asyncContext.addListener(new MyAsyncListener());
new Thread(new Work(asyncContext,request)).start();
out.print("异步执行中");

}
@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

 

 

就这么样了 还会报错

A filter or servlet of the current chain does not support asynchronous opera

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);

查了查资料,

所有的解决办法都在说可能是参数配置未配置正确,或者是需要在server.xml上配置。最后,在StackOverflow上找到了如下答案

request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);

尝试这个改了改,问题解决

总结一下,这种配置文件的方式 .坑不少,但最终解决了. 

还有一种写法有空再测试

https://blog.csdn.net/zxaini2010/article/details/50358763

springmvc 配置异步请求

标签:Stub   page   can   ram   pat   cannot   starting   总结   version   

原文地址:https://www.cnblogs.com/zuochanzi/p/9611088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!