I have an enum field in my Spring DTO which I use in my @RestController
. I wish to create custom error message upon failed validation for this enum field:
public class ConversionInputDto {
// validation annotations
private BigDecimal sourceAmount;
// enum field
@NotNull(message = ERROR_EMPTY_VALUE)
private CurrencyFormat targetCurrency;
// no-args constructor and getters
}
Receiving input as a String and making a custom annotation seems an overkill in my case, and the other alternative, which I know, catches all InvalidFormatException
errors via @ControllerAdvise
and returns the same error for them (thus, a user submitting e.g. String input for numeric property will get the same error message):
@ExceptionHandler(InvalidFormatException.class)
public void handleInvalidEnumAndAllOtherInvalidConversions(HttpServletResponse response) throws IOException {
response.sendError(HttpStatus.BAD_REQUEST.value(), ERROR_MESSAGE);
}
The current default validation error is too long and I would like to make it more user-friendly like "Invalid currency format value. Please choose between....":
"Invalid JSON input: Cannot deserialize value of type
com.foreignexchange.utilities.CurrencyFormat
from String \"test\": not one of the values accepted for Enum class: [AUD, PLN, MXN, USD, CAD]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of typecom.foreignexchange.utilities.CurrencyFormat
from String \"test\": not one of the values accepted for Enum class: [AUD, PLN, MXN, USD, CAD]\n at [Source: (PushbackInputStream); line: 3, column: 20] (through reference chain: com.foreignexchange.models.ConversionInputDto[\"targetCurrency\"])",
Is there an elegant way to solve this? Perhaps with some additional logic in the @ExceptionHandler
checking which field has failed validation?