Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6693

@ExceptionHandler methods should support naked beans as return values

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.0 GA
    • Fix Version/s: 3.1 M2
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Spring MVC @ExceptionHandler controller methods support a flexible argument list and return value, similiar to @RequestMapping-annotated methods. One option that is supported by @RequestMapping methods but not @ExceptionHandler methods is returning naked bean objects from the exception handler method.

      For example, this type of exception handler method would be very convenient, especially when building RESTful services using a MarshallingView (the important detail is the "HelloResponse" return value from the handleBindException method):

      @Controller
      @RequestMapping("/Hello")
      public class HelloController {
        @RequestMapping(value = "/greet", method = RequestMethod.GET)
        public HelloResponse greetGet(@Valid HelloRequest request) {
          String message = "Hello, " + request.getTitle() + " " + request.getName();
          return new HelloResponse(message);
        }
      
        @ExceptionHandler(BindException.class)
        public HelloResponse handleBindException(BindException be) {
          List<FieldError> errors = be.getFieldErrors();
          HelloResponse response = new HelloResponse();
          for (FieldError error : errors) {
            response.addError(error.toString());
          }
          return response;
        }
      }
      

      With a response object that looks like this:

      @XmlRootElement(name = "response")
      public class HelloResponse {
        private String message;
        private List<String> errors;
        // getters and setters
      }
      

      Currently this code will throw an exception when the exception handler method returns to the framework:

      java.lang.IllegalArgumentException: Invalid handler method return value: [email protected]
      	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.getModelAndView(AnnotationMethodHandlerExceptionResolver.java:353)
      	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver.doResolveException(AnnotationMethodHandlerExceptionResolver.java:101)
      	at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:109)
      	at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1004)
      	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:792)
      	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
      	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
      	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
      

        Attachments

          Activity

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              scottyfred Scott Frederick
              Last updater:
              Trevor Marshall
            • Votes:
              13 Vote for this issue
              Watchers:
              19 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                6 years, 50 weeks, 5 days ago