Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:59:26+00:00 2026-05-23T21:59:26+00:00

I want to create an exception handler which will intercept all controllers in my

  • 0

I want to create an exception handler which will intercept all controllers in my project. Is that possible to do? Looks like I have to put a handler method in each controller. Thanks for your help. I have a spring controller that sends Json response. So if an exception happens I want to send an error response which can be controlled from one place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:59:27+00:00Added an answer on May 23, 2026 at 9:59 pm

    (I found a way to implement it in Spring 3.1, this is described in the second part of this answer)

    See chapter 16.11 Handling exceptions of Spring Reference

    There are some more ways than using @ExceptionHandler (see gouki’s answer)

    • You could implement a HandlerExceptionResolver (use the servlet not the portlet package) – that is some kind of global @ExceptionHandler
    • If you do not have a specific logic for the exception, but only specifc view then you could use the SimpleMappingExceptionResolver, which is at least an implementation of the HandlerExceptionResolver where you can specify an Exception name pattern and the view (jsp) which is shown when the exception is thrown. For example:

      <bean
         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
         p:defaultErrorView="uncaughtException">
         <property name="exceptionMappings">
             <props>
                 <prop key=".DataAccessException">dataAccessFailure</prop>
                 <prop key=".TypeMismatchException">resourceNotFound</prop>
                 <prop key=".AccessDeniedException">accessDenied</prop>
              </props>
          </property>
       </bean>
      

    In Spring 3.2+ one can annotate a class with @ControllerAdvice, all @ExceptionHandler methods in this class work in a global way.


    In Spring 3.1 there is no @ControllerAdvice. But with a little hack one could have a similar feature.

    The key is the understanding of the way @ExceptionHandler works. In Spring 3.1 there is a class ExceptionHandlerExceptionResolver. This class implements (with help of its superclasses) the interface HandlerExceptionResolver and is responsible invoking the @ExceptionHandler methods.

    The HandlerExceptionResolver interface has only one Method:

    ModelAndView resolveException(HttpServletRequest request,
                                  HttpServletResponse response,
                                  Object handler,
                                  Exception ex);`.
    

    When the request was handled by a Spring 3.x Controller Method, then this method (represented by org.springframework.web.method.HandlerMethod) is the handler parameter.

    The ExceptionHandlerExceptionResolver uses the handler (HandlerMethod) to obtain the Controller class and scan it for methods annotated with @ExceptionHandler. If one of this methods matches the exception (ex) then this methods get invoked in order to handle the exception. (else null get returned in order to signal that this exception resolver feels no responsible).

    The first idea would be to implement an own HandlerExceptionResolver that behaves like ExceptionHandlerExceptionResolver, but instead of search for @ExceptionHandler in the controller class, it should search for them in one special bean. The drawback would be, that one has to (copy (or subclass ExceptionHandlerExceptionResolver) and must) configure all nice message converters, argument resolvers and return value handlers by hand (the configuration of the real one and only ExceptionHandlerExceptionResolver is done by spring automatically). So I came up with another idea:

    Implement a simple HandlerExceptionResolver that “forwards” the exception to THE (already configured) ExceptionHandlerExceptionResolver, BUT with an modified handler which points to the bean that contains the global Exception handlers (I call them global, because they do the work for all controllers).

    And this is the implementation: GlobalMethodHandlerExeptionResolver

    import java.util.List;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.Ordered;
    import org.springframework.util.StringUtils;
    import org.springframework.web.method.HandlerMethod;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
    
    
    public class GlobalMethodHandlerExeptionResolver
                 implements HandlerExceptionResolver, Ordered {
    
        @Override
        public int getOrder() {
            return -1; //
        }
    
        private ExceptionHandlerExceptionResolver realExceptionResolver;
    
        private List<GlobalMethodExceptionResolverContainer> containers;
    
        @Autowired
        public GlobalMethodHandlerExeptionResolver(
                ExceptionHandlerExceptionResolver realExceptionResolver,
                List<GlobalMethodExceptionResolverContainer> containers) {
            this.realExceptionResolver = realExceptionResolver;
            this.containers = containers;
        }
    
        @Override
        public ModelAndView resolveException(HttpServletRequest request,
                                             HttpServletResponse response,
                                             Object handler,
                                             Exception ex) {              
            for (GlobalMethodExceptionResolverContainer container : this.containers) {    
                ModelAndView result = this.realExceptionResolver.resolveException(
                        request,
                        response,
                        handlerMethodPointingGlobalExceptionContainerBean(container),
                        ex);
                if (result != null)
                    return result;
            }
            // we feel not responsible
            return null;
        }
    
    
        protected HandlerMethod handlerMethodPointingGlobalExceptionContainerBean(
                                   GlobalMethodExceptionResolverContainer container) {
            try {
                return new HandlerMethod(container,
                                         GlobalMethodExceptionResolverContainer.class.
                                              getMethod("fakeHanderMethod"));            
            } catch (NoSuchMethodException | SecurityException e) {
                throw new RuntimeException(e);
            }            
        }
    }
    

    The global Handler has to implement this interface (in order to get found and to implement the fakeHanderMethod used for the handler

    public interface GlobalMethodExceptionResolverContainer {
        void fakeHanderMethod();
    }
    

    And example for an global Handler:

    @Component
    public class JsonGlobalExceptionResolver
                 implements GlobalMethodExceptionResolverContainer {
    
        @Override
        public void fakeHanderMethod() {
        }
    
    
        @ExceptionHandler(MethodArgumentNotValidException.class)
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        @ResponseBody
        public ValidationErrorDto handleMethodArgumentNotValidException(
                    MethodArgumentNotValidException validationException,
                    Locale locale) {
    
             ...
             /* map validationException.getBindingResult().getFieldErrors()
              * to ValidationErrorDto (custom class) */
             return validationErrorDto;
        }
    }
    

    BTW: You do not need to register the GlobalMethodHandlerExeptionResolver because spring automatically register all beans that implements HandlerExceptionResolver for exception resolvers. So a simple <bean class="GlobalMethodHandlerExeptionResolver"/> is enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use Oracle exception to handle errors that might happen in the
I want to write an IErrorHandler implementation that will handle AuthenticationException instances (a proprietary
I have a requirement to create custom exception for the exceptions generated by my
I have a method in my class called play and I want play which
I have a asp.net 2.0 web app which calls a long running stored proc
I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously
I have a template string and an array of parameters that come from different
I have an application that uses MSMQ for asynchronous processing of certain things. I
I have written a framework that does calculations that take a long time to
In summary : I have an ASP.NET web page that causes an AJAX postback

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.