1

I'm developing a rest application with Spring boot. This application have a custom filter that allows access, only, at some requests. If the user require a particular resource the filter throw an exception. how can I handle all the exceptions generated in this filter at the global level?

I have tried the @ControllerAdvice annotation but not working.

jhon tonini
  • 67
  • 3
  • 7
  • What exactly doesn't work for you with `@ControllerAdvice`? Could you provide your implementation of exception handler which implements `@ControllerAdvice`? Also why do you need to handle it globally if you care only about exceptions thrown by your filter? – Sergei Sirik Dec 06 '18 at 19:03
  • Take a look at this probably http://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services. Btw, this works for me `@EnableWebMvc @ControllerAdvice public class ResourceExceptionHandlerImpl implements ResourceExceptionHandler`. `ResourceExceptionHandler` is my interface, not Spring. – Sergei Sirik Dec 06 '18 at 19:05
  • Actually, I think your case is a bit different since the exception is thrown at filter level, not at controller level. Here is similar . question https://stackoverflow.com/questions/34595605/how-to-manage-exceptions-thrown-in-filters-in-spring – Sergei Sirik Dec 06 '18 at 19:10

3 Answers3

0

at the first, you should create a custom exception and extend it from runtimeException

public class CustomException extends RuntimeException
{
    ...
}

and then you can catch it like this

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CustomException.class)
    public ResponseEntity handleCustomException(CustomException ex) {
          return ResponseHelper.response(ex.getData(), ex.getMsg(), 
         ex.getStatus());
        }
}
0

First of all you may have your customized Exception Handler class, it will replace the default

ResponseEntityExceptionHandler

Below is one sample code I used during my studies:

@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    public final ResponseEntity<Object> handleAllExceptions(Exception ex, WebRequest request) {
        ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
        return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(UserNotFoundException.class)
    public final ResponseEntity<Object> handleUserNotFoundException(Exception ex, WebRequest request) {
        ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
        return new ResponseEntity<>(exceptionResponse, HttpStatus.NOT_FOUND);
    }

    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,               HttpHeaders headers, HttpStatus status, WebRequest request) {
        ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), "Validation Failed", ex.getBindingResult().toString());
        return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
    }
}

You can then add your customized Exceptions and complement the response object if you need it.

Please find the complete code with the other classes over here

nortontgueno
  • 2,553
  • 1
  • 24
  • 36
0

Global exception handlers can be implemented in spring boot using @ControllerAdvice annotation. Complete source code to create global exception handler in spring boot available here

Saradha
  • 147
  • 3
  • 16