title: Presence of BindingResult method parameter determines exception thrown?
body: <p>I have a Spring <code>@RestController</code> that has a POST endpoint defined like this:</p>
  <pre class="lang-java prettyprint-override"><code>@RestController
  @Validated
  @RequestMapping(&quot;/example&quot;)
  public class Controller {
  
      @PostMapping
      @ResponseStatus(HttpStatus.CREATED)
      public ResponseEntity&lt;?&gt; create(@Valid @RequestBody Request request,
                                      BindingResult _unused, // DO NOT DELETE
                                      UriComponentsBuilder uriBuilder) {
          // ...
      }
  }
  </code></pre>
  <p>It also has an exception handler for <code>javax.validation.ConstraintViolationException</code>:</p>
  <pre class="lang-java prettyprint-override"><code>@ExceptionHandler({ConstraintViolationException.class})
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  ProblemDetails handleValidationError(ConstraintViolationException e) {...}
  </code></pre>
  <p>Our Spring-Boot app is using <code>spring-boot-starter-validation</code> for validation. The <code>Request</code> object uses <code>javax.validation.*</code> annotations to apply constraints to the various fields like this:</p>
  <pre class="lang-java prettyprint-override"><code>public class Request {
  
      private Long id;
  
      @Size(max = 64, message = &quot;name length cannot exceed 64 characters&quot;)
      private String name;
  
      // ...
  }
  </code></pre>
  <p>As presented above, if you POST a request with an invalid Request, the validation will throw a ConstraintViolationException, which will be handled by the exception handler. This works, we have unit tests for it, all is good.</p>
  <p>I noticed that the <code>BindingResult</code> in the post method wasn't used (the name <code>_unused</code> and comment <code>//DO NOT DELETE</code> were sort of red flags.) I went ahead and deleted the parameter. All of a sudden, my tests broke -- the inbound request was still validated, but it would no longer throw a ConstraintValidationException ... now it throws a <code>MethodArgumentNotValidException</code>! Unfortunately I can't used this other exception because it doesn't contain the failed validation in the format that I need (and doesn't contain all the data I need either).</p>
  <p>Why does the <code>BindingResult</code> presence in the argument list control which exception is thrown? How can I removed the unused variable and still throw the <code>ConstraintViolationException</code> when the javax.validation determines that the request body is invalid?</p>
  <hr />
  <p>Spring-Boot 2.5.5</p>
  <ul>
  <li>spring-boot-starter-web</li>
  <li>spring-boot-starter-validation</li>
  </ul>
  <p>OpenJDK 17.</p>