title: Custom error message on enum field upon failed validation in Spring
body: <p>I have an enum field in my Spring DTO which I use in my <code>@RestController</code>. I wish to create custom error message upon failed validation for this enum field:</p>
  
  <pre><code>public class ConversionInputDto {
  
      // validation annotations
      private BigDecimal sourceAmount;
  
      // enum field
      @NotNull(message = ERROR_EMPTY_VALUE)
      private CurrencyFormat targetCurrency;
  
      // no-args constructor and getters
  } 
  </code></pre>
  
  <p>Receiving input as a String and making a <a href="https://stackoverflow.com/a/21070806/11812518">custom annotation</a> seems an overkill in my case, and the other alternative, which I know, catches all <code>InvalidFormatException</code> errors via <code>@ControllerAdvise</code> and returns the same error for them (thus, a user submitting e.g. String input for numeric property will get the same error message):</p>
  
  <pre><code>@ExceptionHandler(InvalidFormatException.class)
  public void handleInvalidEnumAndAllOtherInvalidConversions(HttpServletResponse response) throws IOException {
      response.sendError(HttpStatus.BAD_REQUEST.value(), ERROR_MESSAGE);
  }
  </code></pre>
  
  <p>The current default validation error is too long and I would like to make it more user-friendly like <em>"Invalid currency format value. Please choose between...."</em>:</p>
  
  <blockquote>
    <p>"Invalid JSON input: Cannot deserialize value of type
    <code>com.foreignexchange.utilities.CurrencyFormat</code> 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
    <code>com.foreignexchange.utilities.CurrencyFormat</code> 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\"])",</p>
  </blockquote>
  
  <p>Is there an elegant way to solve this? Perhaps with some additional logic in the <code>@ExceptionHandler</code> checking which field has failed validation?</p>