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

Support @MVC methods that can apply to any @Controller

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.1 GA
    • Fix Version/s: 3.2 M2
    • Component/s: Web
    • Labels:
      None

      Description

      This Spring 3.1 example (https://github.com/rstoyanchev/spring-mvc-31-demo/tree/master/src/main/java/org/springframework/samples/mvc31/exceptionhandler) gives an example how to declare "global" exception handlers. I think the ExtendedExceptionHandlerExceptionResolver example class should in some way brought into core mvc, so it would be easier to this.

        Issue Links

          Activity

          marceloverdijk Marcel Overdijk created issue -
          Hide
          marceloverdijk Marcel Overdijk added a comment -

          Thinking about it..., adding a @ExceptionHandler to a @EnableWebMvc configuration class might be a good idea to define global exception handlers?

          @Configuration
          @EnableWebMvc
          public class WebConfig extends WebMvcConfigurerAdapter {
           
          	@ExceptionHandler(MethodArgumentNotValidException.class)
          	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
          	@ResponseBody
          	public String handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
          		return "My error message: " + e.getMessage();
          	}
          }

          Show
          marceloverdijk Marcel Overdijk added a comment - Thinking about it..., adding a @ExceptionHandler to a @EnableWebMvc configuration class might be a good idea to define global exception handlers? @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter {   @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public String handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { return "My error message: " + e.getMessage(); } }
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          We can probably add this in 3.2 maybe even as early as M1. We'll need way to configure global @ExceptionHandler methods so suggestions like yours above are welcome. One other option would be to discover a bean by name (e.g. "globalExceptionHandler") where the bean would contain one or more @ExceptionHandler methods. A dedicated @Component stereotype annotation might be another option but I think a name is simpler and should work.

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - We can probably add this in 3.2 maybe even as early as M1. We'll need way to configure global @ExceptionHandler methods so suggestions like yours above are welcome. One other option would be to discover a bean by name (e.g. "globalExceptionHandler") where the bean would contain one or more @ExceptionHandler methods. A dedicated @Component stereotype annotation might be another option but I think a name is simpler and should work.
          rstoya05-aop Rossen Stoyanchev made changes -
          Field Original Value New Value
          Affects Version/s 3.1 GA [ 12697 ]
          Fix Version/s 3.2 Backlog [ 11128 ]
          rstoya05-aop Rossen Stoyanchev made changes -
          Link This issue is related to SPR-9193 [ SPR-9193 ]
          rstoya05-aop Rossen Stoyanchev made changes -
          Assignee Rossen Stoyanchev [ rstoya05-aop ]
          rstoya05-aop Rossen Stoyanchev made changes -
          Status Open [ 1 ] In Progress [ 3 ]
          rstoya05-aop Rossen Stoyanchev made changes -
          Fix Version/s 3.2 Backlog [ 11128 ]
          Fix Version/s 3.2 M2 [ 12702 ]
          tmarshall Trevor Marshall made changes -
          Workflow SPR Workflow [ 52129 ] New SPR Workflow [ 56866 ]
          tmarshall Trevor Marshall made changes -
          Workflow New SPR Workflow [ 56866 ] SPR Workflow [ 66019 ]
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          This now supported through a new @ExceptionResolver annotation. See the commit comment for details.

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - This now supported through a new @ExceptionResolver annotation. See the commit comment for details.
          rstoya05-aop Rossen Stoyanchev made changes -
          Status In Progress [ 3 ] Resolved [ 5 ]
          Resolution Complete [ 8 ]
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          Re-opening with the intent of broadening the scope of this feature.

          The creation of an @ExceptionResolver component annotation has led to the idea of a more general component annotation for a class with globally applicable @MVC methods such as @InitBinder, @ModelAttribute, and @ExceptionHandler, which normally only apply to the controller in which they're defined. The going name for this annotation is @ControllerAdvice.

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - Re-opening with the intent of broadening the scope of this feature. The creation of an @ExceptionResolver component annotation has led to the idea of a more general component annotation for a class with globally applicable @MVC methods such as @InitBinder , @ModelAttribute , and @ExceptionHandler , which normally only apply to the controller in which they're defined. The going name for this annotation is @ControllerAdvice .
          rstoya05-aop Rossen Stoyanchev made changes -
          Resolution Complete [ 8 ]
          Status Resolved [ 5 ] Reopened [ 4 ]
          Hide
          rstoya05-aop Rossen Stoyanchev added a comment -

          Modified title (was: "Out-of-the-box support for "global" exception resolvers in combination with @ResponseBody")

          Show
          rstoya05-aop Rossen Stoyanchev added a comment - Modified title (was: "Out-of-the-box support for "global" exception resolvers in combination with @ResponseBody")
          rstoya05-aop Rossen Stoyanchev made changes -
          Summary Out-of-the-box support for "global" exception resolvers in combination with @ResponseBody Support @MVC methods that can apply to any @Controller
          rstoya05-aop Rossen Stoyanchev made changes -
          Resolution Complete [ 8 ]
          Status Reopened [ 4 ] Resolved [ 5 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open In Progress In Progress
          120d 4h 14m 1 Rossen Stoyanchev 11/Jun/12 10:38 AM
          In Progress In Progress Resolved Resolved
          28d 3h 52m 1 Rossen Stoyanchev 09/Jul/12 2:31 PM
          Resolved Resolved Reopened Reopened
          10d 22h 27m 1 Rossen Stoyanchev 20/Jul/12 12:58 PM
          Reopened Reopened Resolved Resolved
          25d 1h 44m 1 Rossen Stoyanchev 14/Aug/12 2:43 PM

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              marceloverdijk Marcel Overdijk
              Last updater:
              Rossen Stoyanchev
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 10 weeks, 2 days ago