Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

968
Views
@RestControllerAdvice vs @ControllerAdvice
  • What are the major difference between @RestControllerAdvice and @ControllerAdvice ??
  • Is it we should always use @RestControllerAdvice for rest services and @ControllerAdvice MVC ?
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

@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.

about 4 years ago · Santiago Trujillo Report

0

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

about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!