Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]

后端 未结 3 888
南笙
南笙 2020-12-24 04:53

I\'m trying to handle MethodArgumentNotValidException using @ControllerAdvice as code given below:

@ControllerAdvice
public class RestResponseEntityException         


        
3条回答
  •  失恋的感觉
    2020-12-24 05:29

    Springs ResponseEntityExceptionHandler has a method handleException which is annotated with :

    @ExceptionHandler({     
            ...
            MethodArgumentNotValidException.class,
            ... 
        })
    

    Your method handleMethodArgumentNotValidException is also annotated to handle MethodArgumentNotValidException. So spring finds two methods that should be used to handle the same exception, that is the reason for the exception.

    **Solution ** Do not add a new method handleMethodArgumentNotValidException instead just override the method ResponseEntityExceptionHandler.handleMethodArgumentNotValid , and do not annotate it. Your class ErrorWrapper must extend ResponseEntity for that.

提交回复
热议问题