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

后端 未结 3 876
南笙
南笙 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:30

    Handel Exception

    @ControllerAdvice
    @RestController
    @Slf4j
    public class ExceptionHandler extends ResponseEntityExceptionHandler {
        @Override
        protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
            List errorList = ex
                    .getBindingResult()
                    .getFieldErrors()
                    .stream()
                    .map(fieldError -> fieldError.getDefaultMessage())
                    .collect(Collectors.toList());
            ErrorDetails errorDetails = new ErrorDetails(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errorList);
            return handleExceptionInternal(ex, errorDetails, headers, errorDetails.getStatus(), request);
        }
    }
            }
    
    
    

    Customize Error Message

    @Data
    public class ErrorDetails {
        private HttpStatus status;
        private String message;
        private List errors;
    
        public ErrorDetails(HttpStatus status, String message, List errors) {
            super();
            this.status = status;
            this.message = message;
            this.errors = errors;
        }
    
        public ErrorDetails(HttpStatus status, String message, String error) {
            super();
            this.status = status;
            this.message = message;
            errors = Arrays.asList(error);
        }
    }
    

    Sample Response

      {
          "status": "BAD_REQUEST",
          "message": "Validation failed for argument [0] in public void com.ns.hospitalmanagement.resource.PatientResource.createPatient(com.ns.hospitalmanagement.entity.Patient) with 2 errors: [Field error in object 'patient' on field 'name': rejected value [null]; codes [NotNull.patient.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [patient.name,name]; arguments []; default message [name]]; default message [Name Can not be Null]] [Field error in object 'patient' on field 'name': rejected value [null]; codes [NotEmpty.patient.name,NotEmpty.name,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [patient.name,name]; arguments []; default message [name]]; default message [Name Can not be Empty]] ",
          "errors": [
            "Name Can not be Null",
            "Name Can not be Empty"
          ]
        }
    

    提交回复
    热议问题