Jersey App Engine:实体验证不起作用
Jersey App Engine: entity validation not working
我正在使用 Google App Engine、Jersey、Objectify 和 Gson(以及其他一些小型库)构建应用程序。
我想使用 @Valid 注释在资源中进行验证。但是,似乎没有执行任何实际验证。特别是,当将 payee
设置为 "123"
的 Entry
发布到 /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()
之前的验证没有。
顺便说一句,我不得不在 Hibernate Validator 和 Google App Engine 之间使用 Apache BVal implementation of the JSR303 Bean Validation, because of some issues。
在泽西 2.X 您必须注册您的分机。然而,验证似乎是个例外,如 here 所述。无论如何我都会尝试一下,看看你可以配置什么:
- CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE
- ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE
- ServerProperties.BV_FEATURE_DISABLE
您的申请 class 可能如下所示:
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");
}
}
我通过仔细清理 pom.xml
并更新所有版本解决了这个问题。正确的依赖关系如下(我使用的是1.9.27版本的Google App Engine):
<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>
请注意最后两个,关于 juel, were needed to avoid this problem (see the comment concerning the message
parameter): see also this related solved issue 关于 Google App Engine 和 juel 之间的不兼容性。
我正在使用 Google App Engine、Jersey、Objectify 和 Gson(以及其他一些小型库)构建应用程序。
我想使用 @Valid 注释在资源中进行验证。但是,似乎没有执行任何实际验证。特别是,当将 payee
设置为 "123"
的 Entry
发布到 /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()
之前的验证没有。
顺便说一句,我不得不在 Hibernate Validator 和 Google App Engine 之间使用 Apache BVal implementation of the JSR303 Bean Validation, because of some issues。
在泽西 2.X 您必须注册您的分机。然而,验证似乎是个例外,如 here 所述。无论如何我都会尝试一下,看看你可以配置什么:
- CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE
- ServerProperties.FEATURE_AUTO_DISCOVERY_DISABLE
- ServerProperties.BV_FEATURE_DISABLE
您的申请 class 可能如下所示:
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");
}
}
我通过仔细清理 pom.xml
并更新所有版本解决了这个问题。正确的依赖关系如下(我使用的是1.9.27版本的Google App Engine):
<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>
请注意最后两个,关于 juel, were needed to avoid this problem (see the comment concerning the message
parameter): see also this related solved issue 关于 Google App Engine 和 juel 之间的不兼容性。