[SPR-9112] Support @MVC methods that can apply to any @Controller Created: 12/Feb/12  Updated: 14/Aug/12  Resolved: 14/Aug/12

Status: Resolved
Project: Spring Framework
Component/s: Web
Affects Version/s: 3.1 GA
Fix Version/s: 3.2 M2

Type: Improvement Priority: Major
Reporter: Marcel Overdijk Assignee: Rossen Stoyanchev
Resolution: Complete Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relate
is related to SPR-9193 ExceptionHandlerExceptionResolver: pr... Closed
Reference URL: http://forum.springsource.org/showthread.php?120466-Handling-Exceptions-As-JSON-Spring-3-1
Days since last comment: 5 years, 18 weeks, 2 days ago
Last commented by a User: false
Last updater: Rossen Stoyanchev

 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.



 Comments   
Comment by Marcel Overdijk [ 12/Feb/12 ]

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();
	}
}

Comment by Rossen Stoyanchev [ 12/Feb/12 ]

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.

Comment by Rossen Stoyanchev [ 09/Jul/12 ]

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

Comment by Rossen Stoyanchev [ 20/Jul/12 ]

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.

Comment by Rossen Stoyanchev [ 20/Jul/12 ]

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

Generated at Mon Nov 20 00:21:41 UTC 2017 using JIRA 6.4.14#64029-sha1:ae256fe0fbb912241490ff1cecfb323ea0905ca5.