Spring Framework
  1. Spring Framework
  2. SPR-10090

Exception when using @ResponseEntity on @ExceptionHandler method

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Works as Designed
    • Affects Version/s: 3.2 RC2
    • Fix Version/s: None
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      This is a duplicate of bug SPR-7637 and SPR-6692. SPR-7637 is marked as resolved, claiming the issue has been addressed, but it is not. I am trying to do exactly this, and I get the following exception:

      00:50:42.949 ERROR annotation.AnnotationMethodHandlerExceptionResolver (AnnotationMethodHandlerExceptionResolver.java:147) Invoking request method resulted in exception : public org.springframework.http.ResponseEntity com.latencyzero.gamecenter.web.ServiceController.handleException(java.lang.Exception)
      java.lang.IllegalArgumentException: Invalid handler method return value: <500 Internal Server Error,{"resultMsg":"Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Cannot open connection","result":1},{Content-Type=[application/json;charset=UTF-8]}>
      	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.getModelAndView(AnnotationMethodHandlerExceptionResolver.java:414)
      	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:144)
      	at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136)
      	at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1148)
      	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:985)
      	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
      	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
      	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:917)
      	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:813)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:120)
      	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:798)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:97)
      

      My handler is declared as follows:

      @ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class)
      public
      ResponseEntity<String>
      handleException(org.springframework.web.bind.MissingServletRequestParameterException inEx)
      {
          ...
      }
      

      According to the 3.2.0.RC2 docs, it is now acceptable. But the code seems to handle all cases except ResponseEntity: https://github.com/SpringSource/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java#L376

        Activity

        Hide
        Rick Mann added a comment -

        Sorry, wrong handler declaration. This is the one:

        @ExceptionHandler(java.lang.Exception.class)
        public
        ResponseEntity<String>
        handleException(java.lang.Exception inEx)
        {
            ...
        }
        
        Show
        Rick Mann added a comment - Sorry, wrong handler declaration. This is the one: @ExceptionHandler(java.lang.Exception.class) public ResponseEntity< String > handleException(java.lang.Exception inEx) { ... }
        Show
        Rick Mann added a comment - A little more info. The code for regular @RequestMapping methods is here: https://github.com/SpringSource/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java#L939 . Line 939 shows an explicit check for an HttpEntity return type, which is not present in the analogous code here: https://github.com/SpringSource/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java#L391
        Hide
        Rick Mann added a comment -

        A modified version of AnnotationMethodHandlerExceptionResolver.java, with bits of code pasted in from org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter to handle the HttpEntity return type. In my isolated use case, this works. I have not added test cases for this case. The code may be overly complex, as portions were designed to handle more generic use.

        Show
        Rick Mann added a comment - A modified version of AnnotationMethodHandlerExceptionResolver.java, with bits of code pasted in from org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter to handle the HttpEntity return type. In my isolated use case, this works. I have not added test cases for this case. The code may be overly complex, as portions were designed to handle more generic use.
        Hide
        Rick Mann added a comment -

        I've attached a modified version of AnnotationMethodHandlerExceptionResolver.java, with bits of code pasted in from org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter to handle the HttpEntity return type. In my isolated use case, this works. I have not added test cases for this case. The existing test cases ran 100% successfully.

        The code may be overly complex, as portions were designed to handle more generic use.

        Given the similarity between AnnotationMethodHandlerExceptionResolver and AnnotationMethodHandlerAdapter, they should probably inherit from a common base class. Really, the handling of each should be nearly identical.

        Show
        Rick Mann added a comment - I've attached a modified version of AnnotationMethodHandlerExceptionResolver.java, with bits of code pasted in from org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter to handle the HttpEntity return type. In my isolated use case, this works. I have not added test cases for this case. The existing test cases ran 100% successfully. The code may be overly complex, as portions were designed to handle more generic use. Given the similarity between AnnotationMethodHandlerExceptionResolver and AnnotationMethodHandlerAdapter, they should probably inherit from a common base class. Really, the handling of each should be nearly identical.
        Hide
        Rick Mann added a comment -

        Ugh. Nevermind. I realize now I wasted all that effort. I'm updating my app's config to be more 3.2-friendly (it was a copy of a copy of an app from back in 2.5, probably built from examples older than that).

        I can't figure out how to close this bug.

        Show
        Rick Mann added a comment - Ugh. Nevermind. I realize now I wasted all that effort. I'm updating my app's config to be more 3.2-friendly (it was a copy of a copy of an app from back in 2.5, probably built from examples older than that). I can't figure out how to close this bug.
        Hide
        Rossen Stoyanchev added a comment -

        Ok no worries. I guess you figured out to use ExceptionHandlerExceptionResolver. The AnnotationMethodHandlerExceptionResolver is now officially deprecated as of 3.2.

        Show
        Rossen Stoyanchev added a comment - Ok no worries. I guess you figured out to use ExceptionHandlerExceptionResolver . The AnnotationMethodHandlerExceptionResolver is now officially deprecated as of 3.2.

          People

          • Assignee:
            Rossen Stoyanchev
            Reporter:
            Rick Mann
            Last updater:
            Chris Beams
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              1 year, 19 weeks ago