Spring Framework
  1. Spring Framework
  2. SPR-9112

Support @MVC methods that can apply to any @Controller

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major 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

          Hide
          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
          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
          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
          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.
          Hide
          Rossen Stoyanchev added a comment -

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

          Show
          Rossen Stoyanchev added a comment - This now supported through a new @ExceptionResolver annotation. See the commit comment for details.
          Hide
          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
          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 .
          Hide
          Rossen Stoyanchev added a comment -

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

          Show
          Rossen Stoyanchev added a comment - Modified title (was: "Out-of-the-box support for "global" exception resolvers in combination with @ResponseBody")

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              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:
                1 year, 39 weeks, 4 days ago