Hi everyone, I am new to Spring, and I tried using annotation @NotNull and @Past with my field for validation, but when I posted with null value in PostMan, it gave me 2 error responses instead of 1, is this a problem and if it is, how to fix it? Thank you very much

My code is below.

ErrorHandler:

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    logger.info(ex.getClass().getName());

    final List<FieldNotValid> errors = new ArrayList<>();
    for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
        errors.add(new FieldNotValid(error.getField(),
                 String.format("%s %s", error.getField(), error.getDefaultMessage())));
    }
    return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}

Field that need to validate:

@Past
@NotNull
private Date birthDate;

Json request body:

{
       "birthDate":""
}

response body:

[
    {
        "field": "birthDate",
        "message": "must not be null"
    },
    {
        "field": "birthDate",
        "message": "must not be null"
    }
]

Comment From: scottfrederick

Thanks for getting in touch. Unfortunately we can't provide much help with the limited information provided. It is possible that Spring Framework's default error handling is contributing one of the error fields and your code is contributing the other. We would need to see a complete application to know how you are configuring things.

As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. This feels like a question that would be better suited to Stack Overflow. Please post a question there with a complete sample that reproduces your situation. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Comment From: Sumels010901

Thank you for that, very appreciated.