Jersey App Engine:实体验证无效

时间:2015-10-15 16:13:20

标签: java validation google-app-engine jersey

我正在使用Google App Engine,Jersey,Objectify和Gson(以及其他一些小型库)构建应用程序。

我想使用@Valid注释在资源中进行验证。但是,似乎没有进行实际验证。特别是,当Entry payee设置为"123" /entries/entries/nocheck时,该应用不会引发任何异常并且该实体正在保存在数据存储区中。

以下是该实体的摘录:

@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;

  //...
}

以下是资源的摘录:

@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();
  }

}

以下是申请表:

public class MyApplication extends ResourceConfig {

  public MyApplication() {
    packages(
            "it.newfammulfin.model",
            "it.newfammulfin.api",
            "it.newfammulfin.api.util");
  }

}

请注意,pom.xml包含此依赖项:

<dependency>
  <groupId>org.glassfish.jersey.ext</groupId>
  <artifactId>jersey-bean-validation</artifactId>
  <version>2.22.1</version>
</dependency>

应该导致due to the auto-discovery feature,无需进一步配置即可启用验证。

我错过了什么?

更新

我在资源中添加了一个显式验证实体的方法:

  @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();
  }

通过调用validate()进行验证有效。然而,在实际调用testValidate()之前的验证却没有。 顺便说一下,我必须使用JSR303 Bean验证的Apache BVal实现,因为Hibernate Validator和Google App Engine之间有一些issues

2 个答案:

答案 0 :(得分:3)

在球衣2.X中,你必须注册你的扩展名。然而,如here所述,验证似乎是例外。无论如何我都会尝试一下,看看你能配置什么:

  • CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE
  • ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE
  • ServerProperties.BV_FEATURE_DISABLE

您的应用程序类可能如下所示:

public class MyApplication extends ResourceConfig {
  public MyApplication() {
    register(ValidationFeature.class);
    property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true)
    packages(
            "it.newfammulfin.model",
            "it.newfammulfin.api",
            "it.newfammulfin.api.util");
  }
}

答案 1 :(得分:3)

我通过仔细清理WHERE并更新所有版本来解决问题。正确的依赖关系如下(我使用的是1.9.27版本的Google App Engine):

pom.xml

请注意,最后两项需要juel,以避免this problem(请参阅有关<dependency> <groupId>com.google.appengine</groupId> <artifactId>appengine-api-1.0-sdk</artifactId> <version>${appengine.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>com.googlecode.objectify</groupId> <artifactId>objectify</artifactId> <version>5.1.8</version> </dependency> <dependency> <groupId>org.joda</groupId> <artifactId>joda-money</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.sappenin.objectify</groupId> <artifactId>objectify-utils</artifactId> <version>5.1.3</version> </dependency> <dependency> <groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId> <artifactId>gson-jodatime-serialisers</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-bean-validation</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>de.odysseus.juel</groupId> <artifactId>juel-api</artifactId> <version>2.2.7</version> </dependency> <dependency> <groupId>de.odysseus.juel</groupId> <artifactId>juel-impl</artifactId> <version>2.2.7</version> </dependency> 参数的评论):另请参阅this related solved issue有关不兼容的问题Google App Engine和juel。