mic4ael
mic4ael

Reputation: 8300

Handling "error 500" in Spring MVC using @ExceptionHandler

(Firstly, I must say that I have been looking for answers on stackoverflow and the internet but haven't found sufficient answers)**I have just started my adventure with Spring MVC and my first task is to handle Internal Server Error using @ExceptionHandler annotation (first of all, i got to point out that I don't want to use error-page in web.xml). Briefly, whenever "error 500" occurs, there ought to be displayed a proper site with a link to the home site. So, my problem is that I don't know how to make method which follows @ExceptionHandler(Exception.class) invoked each time error 500 takes place.

Upvotes: 1

Views: 13698

Answers (2)

Md. Kamruzzaman
Md. Kamruzzaman

Reputation: 1905

If you use java configuration you may try like follows:

@Configuration
public class ExcpConfig {

    @Bean(name = "simpleMappingExceptionResolver")
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver resolver= new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
        resolver.setExceptionMappings(mappings); // None by default
        resolver.setExceptionAttribute("ErrorOccurred"); // Default is "exception"
        resolver.setDefaultErrorView("500"); // 500.jsp
        return resolver;
    }

}

Upvotes: 2

Jimadilo
Jimadilo

Reputation: 507

You can't actually do what you want with the @ExceptionHandler annotation. This is only for exceptions in a specific controller, not for more general things like error 500's.

You probably want to look at implementing the HandlerExceptionResolver interface, and wiring that in for more general exceptions.

Here's a link to it in the spring docs.

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-exceptionhandlers

Let me know if you need any other help.

Upvotes: 0

Related Questions