Controller Advice Not Working as Expected
See original GitHub issueHi 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:
- Created 7 years ago
- Comments:9 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay, I have found the reason.
The Issue
Your
@ControllerAdviceextendsResponseEntityExceptionHandlerwhich 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 ResponseEntityExceptionHandlerand your method will be used…OR
Override all the methods of
ResponseEntityExceptionHandlerwith 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.)ok thanks