我的应用程序设置如下:
我遇到了BasicErrorController的问题。我尝试了多种方法:
@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);
}
}