title: Java 8 Jackson validation
body: <p>I have a springboot rest service. The user passes in a json object that gets deserialized into this java pojo:</p>
  
  <pre><code>public final class Request {
      private String id;
      private double code;
      private String name;
  
      public String getId() {
          return id;
      }
  
      public double getCode() {
          return code;
      }
  
      public String getName() {
          return name;
      }
  }
  </code></pre>
  
  <p>So the user needs to pass in the following json:</p>
  
  <pre><code>{
      "id": “123457896”,
      "code": "Foo",
      "name": "test"
  } 
  </code></pre>
  
  <p>I want to make all of those fields required. Providing anything less or more will throw an exception. Is there a way to tell jackson to validate the input when it deserializes? I've tried <code>@JsonProperty(required=true)</code> but this doesn't work; apparently from <a href="https://github.com/FasterXML/jackson-annotations/issues/17" rel="noreferrer">here</a> and <a href="https://github.com/FasterXML/jackson-annotations/issues/17" rel="noreferrer">here</a> it seems the <code>JsonProperty</code> annotation is not respected.</p>
  
  <p>I have this validator that I call in my controller:</p>
  
  <pre><code>@Component
  public class RequestValidator implements Validator {
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          return false;
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          String id = ((Request) target).getId();
          if(id == null || id.isEmpty()) {
              throw new InvalidRequestException("A valid id is missing. Please provide a non-empty or non-null id.");
          }
      }
  }
  </code></pre>
  
  <p>But that just seems tedious and ugly to check every field. So given I'm using java 8, spring boot and latest version of jackson what is the best practice in terms of validating an incoming json input? Or am I already doing it in the most up to date manner?</p>