Issue
I have the following field in my requisition
@NotNull
@JsonDeserialize(using = LocalDateDeserializer.class)
@Schema(description = "BIRTH DATE", example = "1992-02-15")
private LocalDate birthDate;
So I set up the following jackson deserializer:
public class LocalDateDeserializer extends StdDeserializer<LocalDate> {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
protected LocalDateDeserializer() {
super(LocalDate.class);
}
@Override
public LocalDate deserialize(
final JsonParser jsonParser,
final DeserializationContext deserializationContext)
throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String date = node.asText();
try {
return LocalDate.parse(date, DATE_TIME_FORMATTER);
} catch (Exception e) {
throw new InvalidDateFormatException("Invalid format" + date);
}
}
}
This is my custom exception class:
public class InvalidDateFormatException extends RuntimeException {
public InvalidDateFormatException(final String message) {
super(message);
}
}
This is my ApplicationExceptionHandler class:
@ControllerAdvice
public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler {
public static final String MISSING_HEADER_MESSAGE = "{0} header must be informed";
@ExceptionHandler(InvalidDateFormatException.class)
public ResponseEntity<String> handleInvalidDateFormatException(InvalidDateFormatException ex) {
// Customize the response based on your needs
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
When I send the requisition with a birthDate wrong formatted the code enters in the deserializer, the exception is caught but the custom error is not thrown. I just got a generic 400 like the image below:

I don't have any more clues about how to solve this problem.
Solution
What actually solved my problem was to add the following config in bootstrap.yml:
jackson:
deserialization:
wrap-exceptions: false
Answered By - Jossany Moura
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.