@ExceptionHandler & @ResponseStatus annotation

In this section, you will learn about @ExceptionHandler & @ResponseStatus annotation.

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

@ExceptionHandler & @ResponseStatus annotation

In this section, you will learn about @ExceptionHandler & @ResponseStatus annotation.

In Spring, during controller execution, HandlerExceptionResolver implementation manages unexpected exceptions. HandlerExceptionResolver looks very similar to the exception mappings of web.xml. Even so they are more flexible when dealing so. For example, when an exception was thrown, which handler was executing ?-kind of information is provided by HandlerExceptionResolver .

Implementation of both interfaces HandlerExceptionResolver and SimpleMappingExceptionResolver will allow you , declaratively, to map Exceptions to specific views with some nonmandatory Java logic.

The @ExceptionHandler annotation should be used on methods which are called to handle exception. It can be defined locally inside an @Controller or within an @ControllerAdvice class to apply it globally to all @RequestMapping methods.

Given below a sample @ExceptionHandler method:

@Controller
public class SimpleController {
	....................
	....................
	....................
	
	@ExceptionHandler(IOException.class)
	public ResponseEntity<String> handleIOException(IOException ex) {
	
		// prepare responseEntity
		
		return responseEntity;
	}

}

An array of exception types can be supply to @ExceptionHandler. If the value is not set to @RequestMapping annotation then the method arguments type , which is an exception type, is used.

Exceptions With @ResponseStatus

The @ResponseStatus annotation is annotated on business exceptions. As the exception occurs the ResponseStatusExceptionResolver handles it by setting the status of the response according to the exception. The ResponseStatusExceptionResolver is by default registered by the DispatcherServlet.

 

Advertisements

Ads
Share on Google+Share on Google+

@ExceptionHandler & @ResponseStatus annotation

Posted on: January 12, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: @ExceptionHandler & @ResponseStatus annotation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads