title: Validate only a few properties from user input in a rest service with spring boot
body: <p>how are you?</p>
  
  <p>I'm developing a rest service with Java and Spring Boot and I have a question about the validations of user input with javax.validation.</p>
  
  <p>Suppose I have a User Model with name and age properties.</p>
  
  <p>And suppose also that I have two endpoints (A and B), and that in endpoint A I need to validate only the name and in endpoint B I need to validate just the age.</p>
  
  <p>The problem I'm facing is that using javax.validation I need to validate the two properties on both endpoints. Is there a way to validate only the fields that i need to validate in certain endpoint? or some way to disable validation on certain properties in an endpoint?</p>
  
  <p>Examples</p>
  
  <p><strong>User</strong>
      public class User {</p>
  
  <pre><code>    @NotEmpty(message="You need to pass the name parameter")
      private String name;
  
      @NotEmpty(message="You need to pass the age parameter")
      private int age;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  }
  </code></pre>
  
  <p><strong>User RestController</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/v1/user")
  public class UserController {
  
      @Autowired
      private User _user;
  
      @RequestMapping(value = "/endpoint-a", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
      @ResponseBody
      public ResponseEntity&lt;?&gt; doPostEndpointA(@Valid @RequestBody User user) {
          // only validate the user name
      }
  
      @RequestMapping(value = "/endpoint-b", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
      @ResponseBody
      public ResponseEntity&lt;?&gt; doPostEndpointB(@Valid @RequestBody User user) {
          // only validate the user age
      }
  }
  </code></pre>