——- 记项目容器更换为undertow的一些坑

一、容器更换后Druid去除广告代码失效

项目是基于若依进行二次开发,所以原来没有对去除广告代码进行更改,使用tomcat也能正常去除广告,但是更换到Druid,代码失效,后端也报错。
报错信息丢失———
网上自己查找无果,求助师傅。最后发现是undertow重新又进入了去除广告的那个拦截器,然后抛出response已经处理处理过的异常(大概),所以解决办法只需要在原来的代码加上一个判断即可!

  1. /**
  2. * 使用新的方法去除阿里的广告,注册一个过滤器,对广告的JS清洗
  3. *
  4. * @return
  5. * @throws IOException
  6. */
  7. @Bean
  8. @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
  9. public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
  10. // 获取web监控页面的参数
  11. DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
  12. // 提取common.js的配置路径
  13. String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
  14. String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
  15. final String filePath = "support/http/resources/js/common.js";
  16. // 创建filter进行过滤
  17. Filter filter = new Filter() {
  18. @Override
  19. public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
  20. }
  21. @Override
  22. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  23. throws IOException, ServletException {
  24. chain.doFilter(request, response);
  25. if (response.isCommitted()) { //这里需要判断请求响应是否已经完成,不知道怎么了会再次进入这里
  26. // 获取common.js
  27. String text = Utils.readFromResource(filePath);
  28. // 正则替换banner, 除去底部的广告信息
  29. text = text.replaceAll("<a.*?banner\"></a><br/>", "");
  30. text = text.replaceAll("powered.*?shrek.wang</a>", "");
  31. text = text.replaceAll("<footer .*?>[\\s\\S]*</footer>",
  32. "");
  33. response.getWriter().write(text);
  34. return;
  35. }
  36. }
  37. @Override
  38. public void destroy() {
  39. }
  40. };
  41. FilterRegistrationBean registrationBean = new FilterRegistrationBean();
  42. registrationBean.setFilter(filter);
  43. registrationBean.addUrlPatterns(commonJsPattern);
  44. return registrationBean;
  45. }

二、使用commons-fileupload文件上传问题

正常小文件能上传,但是当文件超过一定大小就会抛出如下异常,前端因此无法正确接受到返回状态的信息。
image.png
百度查看问题,并按照修改也没有解决该问题,只剩下一个需要自己编写一个工程目录然后云云的未去尝试。

项目有全局异常处理器,理论上来说,异常被全局异常处理器捕抓,然后返回错误信息给前端,但是查看异常发
现,异常是被io.undertow.server…抛出的,并没有被自定义的全局异常处理捕获。

问题来了,换成tomcat,虽然也会抛出异常,但是异常会被自定义的全局异常处理器捕获。最不能理解的就是,当我自己写了一个demo测试文件上传功能,然后编写全局异常 处理器,发现能正常捕获异常,并且成功放回给前端。

个人推测是由于undertow内置的拦截器发现了这个错误,直接返回了,没有继续往下传递,而知直接抛出异常,因此也没有被我们的全局拦截器拦截,因为在服务器内部就自动拦截了。
暂未解决。