How would I go about adding "error-page" type configurations to a Spring MVC webapp using Java config? (No web.xml)?
<error-page>
<error-code>404</error-code>
<location>/errors/404</location>
</error-page>
I'd like to use a configuration like this (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'd like Access Denied exceptions to continue to be caught by Spring Security, and just let any other exceptions get handled by my code.
It looks like a similar question was asked here: Spring JavaConfig is not catching PageNotFound?
look at https://java.net/jira/browse/SERVLET_SPEC-50 - it's not possible to configure that without web.xml, but you can create manual filter that will do same thing for you.
You can create SimpleMappingExceptionResolver bean, and set pages for http errors, without using web.xml. Or 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With