@Controlleradvice exception [How to Solve]

ControllerAdvice

 @ControllerAdviceSpring 3.2 provides a new annotation, which is a Controller enhancer that adds some logic to the methods annotated by @RequestMapping in the controller. The most common one is exception handling

2: @controlleradvice with exception handler

When throwing exceptions to the controller, you can unify the exception handling by specifying the returned json format or jumping to an error page

3: Examples

@Slf4j
 @ControllerAdvice
 public class ExceptionHandlerAdvice {

     @ResponseStatus(HttpStatus.OK)
     @ResponseBody
     @ExceptionHandler({MethodArgumentNotValidException.class})
     public <T> ResponseEntity<ResultDTO<T>> handleBusinessException(MethodArgumentNotValidException method) {
         String defaultMessage = method.getBindingResult().getFieldError().getDefaultMessage();
         log.error("Error in parameters: {}", defaultMessage);
         return ResponseEntity.ok(ResultDTO.getErrorResult(defaultMessage));
     }

     @ResponseStatus(HttpStatus.OK)
     @ResponseBody
     @ExceptionHandler(ValidationException.class)
     public <T> ResponseEntity<ResultDTO<T>> handleBusinessException(ValidationException exception) {
         String defaultMessage = exception.getMessage();
         log.error("Error in parameters,{}", defaultMessage);
         return ResponseEntity.ok(ResultDTO.getErrorResult(defaultMessage));
     }
 }

Read More: