@RestControllerAdvice vs @ControllerAdvice

Spring

Spring Problem Overview


  • What are the major difference between @RestControllerAdvice and @ControllerAdvice ??
  • Is it we should always use @RestControllerAdvice for rest services and @ControllerAdvice MVC ?

Spring Solutions


Solution 1 - Spring

@RestControllerAdvice is just a syntactic sugar for @ControllerAdvice + @ResponseBody, you can look here.

> Is it we should always use @RestControllerAdvice for rest services and > @ControllerAdvice MVC?

Again, as mentioned above, @ControllerAdvice can be used even for REST web services as well, but you need to additionally use @ResponseBody.

Solution 2 - Spring

In addition, we can just understand it as:

@RestControler = @Controller + @ResponseBody

@RestControllerAdvice = @ControllerAdvice + @ResponseBody.

Keeping in mind that @RestControllerAdvice is more convenient annotation for handling Exception with RestfulApi.

Example os usage:

@RestControllerAdvice
public class WebRestControllerAdvice {
  
  @ExceptionHandler(CustomNotFoundException.class)
  public ResponseMsg handleNotFoundException(CustomNotFoundException ex) {
    ResponseMsg responseMsg = new ResponseMsg(ex.getMessage());
    return responseMsg;
  }
}

In that case any exception instanceOf CustomNotFoundException will be thrown in body of response.

Example extracted here: https://grokonez.com/spring-framework/spring-mvc/use-restcontrolleradvice-new-features-spring-framework-4-3

Solution 3 - Spring

Exception: A good REST API should handle the exception properly and send the proper response to the user. The user should not be rendered with any unhandled exception. A REST API developer will have two requirements related to error handling.

  1. Common place for Error handling
  2. Similar Error Response body with a proper HTTP status code across APIs

@RestControllerAdvice is the combination of both @ControllerAdvice and @ResponseBody

The @ControllerAdvice annotation was first introduced in Spring 3.2.

We can use the @ControllerAdvice annotation for handling exceptions in the RESTful Services but we need to add @ResponseBody separately.

Note:
GlobalExceptionHandler was annotated with @ControllerAdvice, thus it is going to intercept exceptions from controllers accross the application.

Solution 4 - Spring

The differences between @RestControllerAdvice and @ControllerAdvice is :

> @RestControllerAdvice = @ControllerAdvice + @ResponseBody. - we can > use in REST web services. > > > @ControllerAdvice - We can use in both MVC and Rest web services, need to > provide the ResponseBody if we use this in Rest web services.

For Example :

Exception Class:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception{

	private static final long serialVersionUID = 1L;
	public ResourceNotFoundException(String message){
    	super(message);
    }
}

usage of the above exception in Rest Web Service.

 @RestControllerAdvice
 public class MyRestControllerAdviceHandler {
     
 @ExceptionHandler(ResourceNotFoundException.class)
      public ResponseMsg resourceNotFoundException(ResourceNotFoundException ex) {
        ResponseMsg resMsg = new ResponseMsg(ex.getMessage());
        return resMsg;
      }
    
}

usage of the above exception in MVC.

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
	@ExceptionHandler(ResourceNotFoundException.class)
	public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex) {
		return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOm.View Question on Stackoverflow
Solution 1 - SpringdeveloperView Answer on Stackoverflow
Solution 2 - SpringEddy BayonneView Answer on Stackoverflow
Solution 3 - SpringPrasenjit MahatoView Answer on Stackoverflow
Solution 4 - SpringLova ChittumuriView Answer on Stackoverflow