Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error message on enum field upon failed validation in Spring

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 type com.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?

like image 211
Petar Bivolarski Avatar asked May 26 '26 02:05

Petar Bivolarski


1 Answers

Thanks to the comments, I found a solution by using isAssignableFrom() to provide custom error messages for the failed enum validation:

@ExceptionHandler(InvalidFormatException.class)
public void handleOfflineBankApi(HttpServletResponse response, InvalidFormatException ex) throws IOException {
    if (ex.getTargetType().isAssignableFrom(CurrencyFormat.class)) {
        response.sendError(HttpStatus.BAD_REQUEST.value(), Constants.ERROR_INVALID_CURRENCY_FORMAT);
    } else {
        response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
    }
}
like image 91
Petar Bivolarski Avatar answered May 28 '26 14:05

Petar Bivolarski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!