title: Spring Boot method validation
body: <h1>Problem</h1>
  
  <p>I have the following constraints on <code>userUuid</code> and <code>itemUuid</code>:</p>
  
  <ul>
  <li>Both strings must not be <code>null</code>.</li>
  <li>Both strings must be UUIDs (eg. <code>f1aecbba-d454-40fd-83d6-a547ff6ff09e</code>).</li>
  <li>The composition <code>(userUuid, itemUuid)</code> must be unique.</li>
  </ul>
  
  <p>I tried to implement the validation in my controller like:</p>
  
  <pre><code>@RestController
  @Validated // (1)
  public class CartItemController {
  
      @PostMapping("/me/carts/{itemUuid}")
      @ResponseStatus(HttpStatus.CREATED)
      public void addItem(@PathVariable("itemUuid") String itemUuid,
                          Authentication auth) {
          CartItemId id = getCartItemId(getUserUuidFrom(auth), itemUuid);
          ...
      }
  
      @Unique // (4)
      public CartItemId getCartItemId(@NotNull @Uuid String userUuid, // (2)
                                      @NotNull @Uuid String itemUuid) { // (3)
          return new CartItemId(userUuid, itemUuid);
      }
      ...
  }
  </code></pre>
  
  <p><code>@Uuid</code> and <code>@Unique</code> are custom constraints. Method validation is enabled in <code>(1)</code>. <code>(2)</code> are the contraints for the user UUID. <code>(3)</code> are the constraints for the item UUID. The unique constraint is applied to the returned <code>CartItemId</code> in <code>(4)</code>. However, the parameters and the return value are never validated. Neither for the standard <code>@NotNull</code> constraint nor for my custom constraints. I receive HTTP status <code>201 Created</code> instead of <code>400 Bad Request</code>.</p>
  
  <p><em>What am I doing wrong?</em></p>
  
  <h1>Stuff that works</h1>
  
  <p>The following code works for the item UUID:</p>
  
  <pre><code>@RestController
  @Validated
  public class CartItemController {
  
      @PostMapping("/me/{itemUuid}")
      @ResponseStatus(HttpStatus.CREATED)
      public void addItem(@PathVariable("itemUuid") @Uuid String itemUuid, // (1)
                          Authentication auth) {
          ...
      }
  
  }
  </code></pre>
  
  <p>Adding the <code>@Uuid</code> to the path variable parameter works. Values like <code>anInvalidUuid</code> are rejected. I also tested the <code>@Unique</code> constraint in other use cases and it worked perfectly.</p>
  
  <p><em>What is the difference between <code>addItem()</code> and <code>toId()</code>?</em></p>
  
  <h1>Versions</h1>
  
  <p>I am using Java <code>1.8</code> and Spring Boot <code>2.0.0.RELEASE</code>. <code>org.hibernate.validator:hibernate-validator:6.0.7.Final</code> is on my classpath.</p>