Announcement Announcement Module
Collapse
No announcement yet.
ExceptionHandler Annotation Spring 3.0RC1 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ExceptionHandler Annotation Spring 3.0RC1

    Hi,

    i'm trying to use the @ExceptionHandler but the thrown exception is never reaching the annotaded method, simple it doesn't work. All other RESTfull controller, methods annotaded with @RequestMapping are working. I've also tried another exceptions. Are I mission something?

    Code:
    @Controller
    public class ExceptionHandlerController
    {
        private final Logger log = LoggerFactory.getLogger(getClass());
        
        @ExceptionHandler(ManagerSecurityException.class)
        public void handleManagerSecurityException(ManagerSecurityException e,
                HttpServletResponse response)
        throws IOException
        {
            log.info("Manager exception handler " + e.getMessage());
            
            response.sendError(HttpServletResponse.SC_FORBIDDEN,
                e.getMessage());
        }
    }

  • #2
    Hi,

    nobody an idea? Maybe my web context can help, my exception handler controller is in com.bb.cadn.manager.web package
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
           					   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       					  
    						   http://www.springframework.org/schema/context
           					   http://www.springframework.org/schema/context/spring-context-3.0.xsd
           					   http://www.springframework.org/schema/util
           					   http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    	<context:property-placeholder location="file:/etc/archive/discoverer.conf"/>
    	
    	<context:annotation-config/>
    	
    	<context:component-scan base-package="com.bb.cadn.discoverer.web,
    				com.bb.cadn.manager.web"/>
    
    	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
    		  p:defaultViews-ref="viewResolverList"
    		  p:defaultContentType="application/json"
    		  p:favorPathExtension="false"/>
    	
    	<util:list id="viewResolverList">
    		<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
    	</util:list>
    	
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
        	  p:useDefaultSuffixPattern="false"/>
       
    </beans>

    Comment


    • #3
      i also have this kind of problem:
      Controller Method
      Code:
      @RequestMapping(value = "/test")
      public String get(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws NoConfigFoundException {
      Config config = configService.getConfigByUserName(request.getRemoteUser());//might throw a NoConfigFoundException
      modelMap.addAttribute("config",config);//adding the found config to the ModelMap
      return "test"; //for using the test.jsp
      And the annotated ExceptionHandler Method:
      Code:
      @ExceptionHandler(NoConfigFoundException.class)
      public ModelAndView handleNoConfigException(ModelAndView mav, NoConfigFoundException ex, HttpServletRequest request){
              mav.addObject("ex",ex);
              mav.setViewName("noconfig");
              return mav;
      }
      The get-Method is called correctly when browsing to /nameofthewebapp/test

      But in case when the call to the configService throws an Exception i get the Stacktrace presented instead of the ExcptionHandler Method gets called.

      Code:
      com.foo.service.NoConfigFoundException: No config for UserName [user2]
      	at com.foo.service.ConfigServiceImpl.getConfigByUserName(ConfigServiceImpl.java:24)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              ....
      Anybody knows a solution for us both

      Comment


      • #4
        ok, I came a little bit further - after debugging deeply.
        For the short: It's not allowed to have ModelAndView as Parameter for the @ExceptionHandler Method
        Theirfor it raises an IllegalStateException in the AnnotationMethodHandlerExceptionResolver.resolveHa ndlerArguments Method


        So I have to create a ModelAndView Object, set viewName and add my exception - so I can output the cause of the exception
        :-) At least - after this monolog ;-) - I'm happy now :-)

        Comment


        • #5
          To Boban,

          I've just tried the same thing and had a similar outcome.

          From reading the spring API it says:
          Annotation for handling exceptions in specific handler* classes and/or handler* methods.

          * By handler I think Spring is referring to the Controllers not the ExceptionHandler.

          If you try putting that @ExceptionHandler block in the Controller that is actually throwing the exception it will be handled. As far as I understand, your @ExceptionHandler(ManagerSecurityException.class) will only be reached if the ManagerSecurityException is thrown from within the ExceptionHandlerController.

          Also try looking at the HandlerExceptionResolver interface, that seems to be a better solution if your trying to do a global exception handler.

          Hope that helps.
          Last edited by lazy_p; May 19th, 2010, 08:56 AM.

          Comment


          • #6
            Originally posted by lazy_p View Post
            To Boban,

            I've just tried the same thing and had a similar outcome.

            From reading the spring API it says:
            Annotation for handling exceptions in specific handler* classes and/or handler* methods.

            * By handler I think Spring is referring to the Controllers not the ExceptionHandler.

            If you try putting that @ExceptionHandler block in the Controller that is actually throwing the exception it will be handled. As far as I understand, your @ExceptionHandler(ManagerSecurityException.class) will only be reached if the ManagerSecurityException is thrown from within the ExceptionHandlerController.

            Also try looking at the HandlerExceptionResolver interface, that seems to be a better solution if your trying to do a global exception handler.

            Hope that helps.
            Thank you! I also had this problem. The documentation should be amended to let people know that the annotation is only usable in the controller that throws the exception.

            Comment


            • #7
              I was also stuck on this. Thanks, lazy_p!

              Originally posted by lazy_p View Post
              To Boban,

              I've just tried the same thing and had a similar outcome.

              From reading the spring API it says:
              Annotation for handling exceptions in specific handler* classes and/or handler* methods.

              * By handler I think Spring is referring to the Controllers not the ExceptionHandler.

              If you try putting that @ExceptionHandler block in the Controller that is actually throwing the exception it will be handled. As far as I understand, your @ExceptionHandler(ManagerSecurityException.class) will only be reached if the ManagerSecurityException is thrown from within the ExceptionHandlerController.

              Also try looking at the HandlerExceptionResolver interface, that seems to be a better solution if your trying to do a global exception handler.

              Hope that helps.

              Comment

              Working...
              X