Controller Advice Not Working as Expected

See original GitHub issue

Hi I am using spring boot micro service in my project. I have created a controller annotated with RestController something like this

@RestController
public class SellerStateResource implements ISellerStateResource {
    public ApiResponseEntity getSellerSate()

And then I have created global controller Advice class

@ControllerAdvice(annotations = RestController.class)
public class SelfHelpControllerAdvice extends ResponseEntityExceptionHandler{


    @SuppressWarnings("rawtypes")
    @ExceptionHandler(Exception.class)
    public ApiResponseEntity handleGenericException(Exception ex, HttpServletRequest request) {
        long startTime = System.currentTimeMillis();
        ApiResponseEntity response = new ApiResponseEntity<>();
        ApiResponseMetaData responseMetaData = ApiResponseMetaDataGenerator
                .getResponseMetadata(System.currentTimeMillis() - startTime, false, request.getRequestURI(), "test");
        response.setResponseMetadata(responseMetaData);
        System.out.println(response);
        return response;
    }

When ever I get an exception,it comes over here but then it returns spring boot by default resposne

{
  "timestamp": 1459879962341,
  "status": 404,
  "error": "Not Found",
  "exception": "java.io.IOException",
  "message": "No message available",
  "path": "/sellers/state"
}

but whenever i place this ExceptionHandler in RestController ,it gives me custom response.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
andderocommented, Mar 18, 2019

Okay, I have found the reason.

The Issue

Your @ControllerAdvice extends ResponseEntityExceptionHandler which defines several methods to handle different kind of exceptions (including your 404 case).

Therefore, if you use that class as your base class, without overriding those methods, then they will get handled in the default way.

The Solution

Either remove the extends ResponseEntityExceptionHandler and your method will be used…

OR

Override all the methods of ResponseEntityExceptionHandler with your custom implementation.

(I personally prefer the first solution, because it is simpler and you can have a more general approach like @ExceptionHandler(Throwable.class) to return an error response without much repeated code.)

0reactions
mun-creativecommented, May 7, 2020

ok thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring 3.2 @ControllerAdvice Not Working - Stack Overflow
I moved the class with the controller to the same package as the controller. I am using 3.2.0 release jars. If I put...
Read more >
Spring MVC Exception Handling - @ControllerAdvice ...
Spring MVC Exception Handling is very important to make sure you are not sending server exceptions to client. Today we will look into...
Read more >
Spring Boot @ControllerAdvice & @ExceptionHandler example
Exception Handler with Controller Advice in Spring​​ The @ExceptionHandler annotation indicates which type of Exception we want to handle. The ...
Read more >
Complete Guide to Exception Handling in Spring Boot
This article showcases various ways to handle exceptions in a Spring Boot Application.
Read more >
Guide to Spring Boot REST API Error Handling - Toptal
ControllerAdvice is an annotation in Spring and, as the name suggests, ... more details about the problem so the client can know which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found