Error-page configuration in Spring MVC JavaConfig webapp? (no web.xml)

How can I add adding configurations of type "error-page" to Spring MVC webapp using Java config? (No web.xml)?

<error-page> <error-code>404</error-code> <location>/errors/404</location> </error-page> 

I would like to use such a configuration (in Java Config) to forward all uncaught exceptions to a specific controller method.

I was hoping to avoid the @ ControllerAdvice / @ ExceptionHandler configuration (which allows me to create a controller method that would handle ALL errors) because I would like Access Denied exceptions to continue to fall under Spring Security and just let the other exceptions be handled by my code.

It looks like a similar question was asked here: Spring JavaConfig not catching PageNotFound?

+7
java spring-mvc
source share
2 answers

look at https://java.net/jira/browse/SERVLET_SPEC-50 - it is impossible to configure this without web.xml, but you can create a manual filter that will do the same for you.

+8
source share

You can create a SimpleMappingExceptionResolver bean and set the pages for HTTP errors without using web.xml. Or the default views for any exceptions. Here:

  @Bean public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){ SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver(); //page and statusCode pare //you also can use method setStatusCodes(properties) resolver.addStatusCode(viewName, statusCode); //set views for exception Properties mapping = new Properties(); mapping.put("ua.package.CustomException" , "page") resolver.setExceptionsMapping(mapping); return resolver; } 
+2
source share

All Articles