使用Tomcat8和ErrorPageFilter进行Spring Boot war部署

时间:2018-04-24 16:55:02

标签: java spring spring-mvc spring-boot tomcat8

我的应用程序设置如下:

  • Spring Boot 2.0.1.RELEASE
  • Tomcat 8.5
  • 部署为WAR
  • Spring MVC

我遇到了BasicErrorController的问题。我尝试了多种方法:

  1. 不会更改错误处理方法,这会导致默认的白标错误页面。这对404错误有效,但不是500错误。收到的页面是默认的Tomcat错误页面。
  2. 在下面配置一个类似于代码段的ErrorViewResolver。这也可以按预期解决404错误。对于500个状态代码,它的解析类似于Tomcat显示默认错误页面的上述内容。
  3. @Component

    public class CustomErrorViewResolver implements ErrorViewResolver {
    
        private Log logger = LogFactory.getLog(getClass());
    
        @Override
        public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
            if (HttpStatus.NOT_FOUND == status) {
                    logger.error("Error 404 for url " + model.get("path"));
                return new ModelAndView("errors/page-not-found", model);
            }
            else if (HttpStatus.INTERNAL_SERVER_ERROR == status) {
                return new ModelAndView("errors/server-error", model);
            }
    
            return new ModelAndView("errors/server-error", model);
        }
    
    }
    

    在查看ErrorPageFilter时,是否可能是因为response.sendError(..)强制Tomcat处理错误?在404调用期间不会触发此代码段,因为它特定于异常处理。我也没有在Spring Boot for ErrorPageFilter.

    的主分支中看到这一点
    private void forwardToErrorPage(String path, HttpServletRequest request,
                HttpServletResponse response, Throwable ex)
                throws ServletException, IOException {
            if (logger.isErrorEnabled()) {
                String message = "Forwarding to error page from request "
                        + getDescription(request) + " due to exception [" + ex.getMessage()
                        + "]";
                logger.error(message, ex);
            }
            setErrorAttributes(request, 500, ex.getMessage());
            request.setAttribute(ERROR_EXCEPTION, ex);
            request.setAttribute(ERROR_EXCEPTION_TYPE, ex.getClass());
            response.reset();
            response.sendError(500, ex.getMessage());
            request.getRequestDispatcher(path).forward(request, response);
            request.removeAttribute(ERROR_EXCEPTION);
            request.removeAttribute(ERROR_EXCEPTION_TYPE);
        }
    

    Spring boot 2.0.1是否存在潜在问题?或者我应该升级到不同的版本?我错过了单独的配置吗?任何想法都会很棒,谢谢。

    **编辑,添加应用程序和配置类**

    @Configuration
    @ImportResource({"classpath:spring/ibatisContext.xml", "classpath:spring/daoContext-latam.xml"})
    @SpringBootApplication
    @EnableScheduling
    @ComponentScan(basePackages = { ...},
            excludeFilters = { ... }
            )
    public class LatApplication extends SpringBootServletInitializer{
    
        public static void main(String[] args) {
            SpringApplication.run(LatApplication.class, args);
        }
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(LatApplication.class);
        }
    }
    

    MVC配置类:

    @Configuration
    public class MvcConfiguration implements WebMvcConfigurer {
    
        @Autowired
        CustomStringToArrayConverter stringToArrayConverter;
    
        @Autowired
        InterceptorController businessTypeInterceptor;
    
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            registry.viewResolver(resolver);
        }
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
        }
    
        @Override
        public void addFormatters(FormatterRegistry registry) {
            registry.addConverter(new CustomStringToArrayConverter());
        }
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    
        public void configurePathMatch(PathMatchConfigurer configurer) {
            configurer.setUseSuffixPatternMatch(true);
            configurer.setUseTrailingSlashMatch(false);
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(businessTypeInterceptor);
        }
    
    }
    

0 个答案:

没有答案