title: Jersey App Engine: entity validation not working
body: <p>I am building an application using Google App Engine, Jersey, Objectify, and Gson (and some other tiny library).</p>
  
  <p>I would like to do validation in the resource using the <a href="http://docs.jboss.org/hibernate/beanvalidation/spec/1.1/api/javax/validation/Valid.html" rel="nofollow noreferrer">@Valid</a> annotation. However, no actual validation seems to be performed. In particular, when posting an <code>Entry</code> with <code>payee</code> set to <code>"123"</code> to both <code>/entries</code> and <code>/entries/nocheck</code>, the app does not raise any exception and the entity is being saved in the datastore.</p>
  
  <p>Here is a snippet of the entity:</p>
  
  <pre><code>@Entity
  public class Entry {
  
    @Id
    private Long id;
    private LocalDate date;
    @com.sappenin.objectify.annotation.Money
    private Money amount;
    @NotNull @Pattern(regexp = "[a-zA-Z][a-zA-Z0-9]*") @Length(min = 5)
    private String payee;
    private String description;
    private String note;
  
    //...
  }
  </code></pre>
  
  <p>Here is a snippet of the resource:</p>
  
  <pre><code>@Path("/entries")
  @Produces(MediaType.APPLICATION_JSON)
  public class TestApi {
  
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response create(@Valid Entry entry) {
      OfyService.ofy().save().entities(entry).now();
      return Response.ok(entry).build();
    }
  
    @POST
    @Path("/nocheck")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createNoCheck(Entry entry) {
      OfyService.ofy().save().entities(entry).now();
      return Response.ok(entry).build();
    }
  
  }
  </code></pre>
  
  <p>Here is the application:</p>
  
  <pre><code>public class MyApplication extends ResourceConfig {
  
    public MyApplication() {
      packages(
              "it.newfammulfin.model",
              "it.newfammulfin.api",
              "it.newfammulfin.api.util");
    }
  
  }
  </code></pre>
  
  <p>Note that the pom.xml includes this dependency:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
    &lt;artifactId&gt;jersey-bean-validation&lt;/artifactId&gt;
    &lt;version&gt;2.22.1&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>which should cause, <a href="https://jersey.java.net/documentation/latest/bean-validation.html#d0e13371" rel="nofollow noreferrer">due to the auto-discovery feature</a>, the validation to be enabled without further configuration.</p>
  
  <p>What am I missing?</p>
  
  <h2>Update</h2>
  
  <p>I added a method in the resource which explicitly validates the entity:</p>
  
  <pre><code>  @POST
    @Path("validate")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response testValidate(@Valid Entry entry) {
      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
      Validator validator = factory.getValidator();
      System.out.println("validator is "+validator.getClass().getName());
      System.out.println(validator.validate(entry));
      return Response.ok(entry).build();
    }
  </code></pre>
  
  <p>The validation through the invocation of <code>validate()</code> works. Yet, the validation before the actual invocation of <code>testValidate()</code> does not.
  As an aside, I had to use the <a href="http://bval.apache.org/" rel="nofollow noreferrer">Apache BVal</a> implementation of the JSR303 Bean Validation, because of some <a href="https://stackoverflow.com/a/19814199/1003056">issues</a> between Hibernate Validator and Google App Engine.</p>