title: How to add domain object's ID to validation message?
body: <p>I have a Spring Boot application, which uses annotations from <code>javax.validation</code> to validate my domain object. Unfortunately, the error message doesn't contain the ID of the domain object. I need the ID for operation and development (debugging).</p>
  <p><strong>Code</strong></p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootApplication
  public class TestApplication {
  
    public static void main(String[] args) {
      SpringApplication.run(TestApplication.class, args);
    }
  
    @Data
    static class Model {
      private String id;
  
      @Min(2)
      private int number;
    }
  
    @RestController
    static class Controller {
  
      @PostMapping(&quot;/test&quot;)
      public int test(@RequestBody @Valid Model model) {
        return 1;
      }
    }
  }
  </code></pre>
  <p><strong>Logs</strong></p>
  <pre><code>2022-05-12 10:39:15.252  WARN 1424 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public int test.TestApplication$Controller.test(test.TestApplication$Model): [Field error in object 'model' on field 'number': rejected value [1]; codes [Min.model.number,Min.number,Min.int,Min]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [model.number,number]; arguments []; default message [number],2]; default message [muss größer-gleich 2 sein]] ]
  </code></pre>
  <p><strong>Research</strong></p>
  <ul>
  <li><p>I can add values of the annotation attributes and the value of the validated property, see <a href="https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-message-interpolation.html#section-interpolation-with-message-expressions" rel="nofollow noreferrer">Hibernate Validator</a>:</p>
  <blockquote>
  <p><strong>4.1.2. Interpolation with message expressions</strong></p>
  <p>As of Hibernate Validator 5 (Bean Validation 1.1) it is possible to use the Unified Expression Language (as defined by JSR 341) in constraint violation messages. This allows to define error messages based on conditional logic and also enables advanced formatting options. The validation engine makes the following objects available in the EL context:</p>
  <ul>
  <li><p>the attribute values of the constraint mapped to the attribute names</p>
  </li>
  <li><p>the currently validated value (property, bean, method parameter etc.) under the name validatedValue</p>
  </li>
  <li><p>a bean mapped to the name formatter exposing the var-arg method format(String format, Object... args) which behaves like java.util.Formatter.format(String format, Object... args).</p>
  </li>
  </ul>
  </blockquote>
  </li>
  <li><p>I can write a <a href="https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-message-interpolation.html#section-custom-message-interpolation" rel="nofollow noreferrer">custom message interpolation</a>, but the <a href="https://docs.oracle.com/javaee/7/api/javax/validation/MessageInterpolator.Context.html" rel="nofollow noreferrer"><code>MessageInterpolator.Context</code></a> only provides the validated value.</p>
  </li>
  </ul>
  <p><strong>Question</strong></p>
  <p>How can I add domain object's ID to the validation message?</p>