Questions tagged [controller-advice]
113 questions
15
votes
3 answers
Spring boot Rest responding with empty body for exceptions other than the ones overridden in my @ControllerAdvice
I have a @ControllerAdvice extending ResponseEntityExceptionHandler as an attempt for me to control standard response for any exception raised with in the API call workflow.
Without the Controller advice. I get HTML based generic response generated…

Gagandeep Singh
- 151
- 1
- 1
- 4
9
votes
5 answers
@ControllerAdvice and @ExceptionHandler not getting triggered for my RestController
In order to have unified exception handling throughout the application I am using Error Handling for REST with Spring solution#3 which uses @ControllerAdvice along with @ExceptionHandler.
Spring version: 4.3.22.RELEASE
Spring Boot version:…

Learner
- 1,503
- 6
- 23
- 44
5
votes
1 answer
How to pass checked exception from CompletableFuture to ControllerAdvice
How can I make the controllerAdvice class catch the exception that is thrown from completablefutrue.
In the code below I have a method checkId that throws a checked exception. I call this method using completablefuture and wrap the checked…

MA1
- 926
- 10
- 28
5
votes
1 answer
Spring Boot Controller Advice - How to return XML instead of JSON?
I have a controller advice class, but I can't seem to get it to return XML, even though I've used the @RequestMapping annotation. Here's a stripped-down example.
@RestControllerAdvice
public class ControllerAdvice {
…

geekTechnique
- 850
- 1
- 11
- 38
4
votes
2 answers
When to use ResponseStatusException and ControllerAdvice
Spring5 has introduced ResponseStatusException, which has put me in a dilemma as to in what scenario I can use a ResponseStatusException and ControllerAdvice as both of them are quiet similar.
Can anyone help me with this.
Thanks in advance.

Sonali
- 73
- 1
- 6
4
votes
2 answers
ControllerAdvice conditionally handle exception
I have a controller advice that handles the exceptional behavior in my REST controller and I came across a situation when I have to conditionally process SQLIntegrityConstraintViolationException that have a certain message (the one for duplicate…

Daniel Pop
- 456
- 1
- 6
- 23
4
votes
0 answers
How to use @ControllerAdvice if there is no HTTP body?
I'd like to have an @ControllerAdvice that is called for all HTTP requests on all controllers. Unfortunately it only triggers if there is a HTTP body in the request. If not, it's completely ignored. Isn't spring supposed to call handleEmptyBody() in…

BetaRide
- 16,207
- 29
- 99
- 177
4
votes
2 answers
Spring Boot @ControllerAdvice / @Valid
I am working on sample demo application for Exception Handling in Spring Boot.I am trying Exception Handling With @ControllerAdvice.
I would like to handle exception thrown by validator. It handles other exceptions but not…

Airam
- 41
- 1
- 3
4
votes
0 answers
How to catch all the exceptions in Spring boot 2 webflux with @ControllerAdvice
My application is made by Spring Boot 2 webflux and thymeleaf, I want to catch all the exception and render the error to a customized error page.
I use @ControllerAdvice and @ExceptionHandler to catch exceptions and handle errors in a central…

user2592010
- 41
- 1
- 3
4
votes
1 answer
How to extract the field name and the error message from the MethodArgumentNotValidException class?
I am using javax.validation.Size annotation to perform String size validation.
@Data
public class EventRequestBean {
@Size( max = 40 )
private String title;
@Size( max = 50 )
private String topic;
}
And I am using a global…

Naanavanalla
- 1,412
- 2
- 27
- 52
3
votes
2 answers
HttpMediaTypeNotAcceptableException: Could not find acceptable representation for @ControllerAdvice
I need to add @ControllerAdvice class to handle exceptions and return body with a message from the exception.
But when checking via postman, I keep getting the default body with 500Internal Server Error even though the breakpoint inside my…

NeverSleeps
- 1,439
- 1
- 11
- 39
3
votes
0 answers
Warning .m.m.a.ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler in my Spring Boot Application
I am trying to under why am getting this warning from my general exception handler method.
I am running SpringBoot version 2.6.1. and using @ControllerAdvice to handle exceptions globally.
This is the general exception handler method:
…

Denis Kisina
- 350
- 4
- 19
3
votes
2 answers
What is the best way to handle exception in Spring Boot?
I am developing an application in Spring Boot. I was wondering what is the best way to handle exceptions. So following is my code,
ExceptionHandler.java
I am using @ControllerAdvice. What is the best to use? @ControllerAdvice or…

H Athukorala
- 739
- 11
- 32
3
votes
2 answers
How do I change only the status code on a Spring MVC error with Boot?
I'm writing a Web application that makes downstream calls using RestTemplate. If the underlying service returns a 401 Unauthorized, I want to also return a 401 to the calling application; the default behavior is to return a 500. I want to keep the…

chrylis -cautiouslyoptimistic-
- 75,269
- 21
- 115
- 152
3
votes
2 answers
Spring boot: Overridden handleMethodArgumentNotValid not executed
I am validating request parameters for a rest endpoint as:
@Valid BoundingBox boundingBox
A list of request parameters are mapped to an instance of BoundingBox.
In class BoundingBox I have used annotations for field validations, like @Max, @Min…

Mandroid
- 6,200
- 12
- 64
- 134