Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot 3.0.0+ @Override error in ResponseEntityExceptionHandler

I am following this example (Validate Request Body and Parameter in Spring Boot in medium) and this example to implement a Handler to handle the correctness of a JSON as a parameter. The concepts are somewhat clear but having reached this point the use of @Override for the handleMethodArgumentNotValid method gives me an error like: Method does not override method from its superclass.

I have searched online to figure out how to solve this problem but I am probably missing something.

package com.tericcabrel.hotel.exceptions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
  @Override
  protected ResponseEntity<Object> handleMethodArgumentNotValid(
      MethodArgumentNotValidException ex, HttpHeaders headers,
      HttpStatus status, WebRequest request) {

    Map<String, List<String>> body = new HashMap<>();

    List<String> errors = ex.getBindingResult()
        .getFieldErrors()
        .stream()
        .map(DefaultMessageSourceResolvable::getDefaultMessage)
        .collect(Collectors.toList());

    body.put("errors", errors);

    return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
  }
}
like image 398
Gianni Spear Avatar asked Nov 24 '25 07:11

Gianni Spear


2 Answers

I report the solution if possibly other people follow the tutorial. In the official documentation the method (ResponseEntityExceptionHandler) reports the class HttpStatusCode and not HttpStatus

like image 123
Gianni Spear Avatar answered Nov 26 '25 22:11

Gianni Spear


I had a similar problem today.

I've solved it NOT extending from ResponseEntityExceptionHandler, replacing the @Override annotation with @ExceptionHandler(MethodArgumentNotValidException.class), and removing the parameters headers and status from the method.

I didn't try your particular code. Maybe you have to modify it a little bit, but basically you should write something like this:

@ControllerAdvice
public class GlobalExceptionHandler {
    
    @ExceptionHandler(MethodArgumentNotValidException.class)
    protected ResponseEntity<Object> handleMethodArgumentNotValid(
        MethodArgumentNotValidException ex, WebRequest request) {

        Map<String, List<String>> body = new HashMap<>();

        List<String> errors = ex.getBindingResult()
            .getFieldErrors()
            .stream()
            .map(DefaultMessageSourceResolvable::getDefaultMessage)
            .collect(Collectors.toList());

        body.put("errors", errors);

        return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
    }
}
like image 34
donlaiq Avatar answered Nov 26 '25 23:11

donlaiq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!