Bean验证不起作用?

时间:2014-08-10 20:06:45

标签: java validation java-ee jboss bean-validation

我有一个services模块,ClientRequest类为

public class ClientRequest {
    @NotNull(message = "email can not be empty")
    private String email;
    @NotNull(message = "userExternalId can not be empty")
    private String userExternalId;
    @NotNull(message = "password can not be empty")
    private String password;


    @SuppressWarnings("UnusedDeclaration")
    public ClientRequest() {
        // used by jax-rs
    }

services / pom.xml

<properties>
    <validation-api.version>1.1.0.Final</validation-api.version>
</properties>

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>${validation-api.version}</version>
</dependency>

我有integration模块用于编写集成测试 integration / pom.xml 看起来像

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.0.2.GA</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j-api.version}</version>
    <scope>test</scope>
</dependency>  

我的测试看起来像

    @Test
    public void testRegisterClientInValid() throws IOException {
        final Client client = ClientBuilder.newClient();
        final Invocation.Builder request = client.target("http://localhost:9090/application/oauth/register").request(MediaType.APPLICATION_JSON);
        final Map<String, String> parameters = new HashMap<>();
        parameters.put("email", "integrationTest@gmail.com");
        final Response response = request.post(Entity.entity(parameters, MediaType.APPLICATION_JSON));
        assertEquals(HttpStatus.SC_OK, response.getStatus());

        final JsonNode jsonReply = getObjectMapper().readTree(response.readEntity(String.class));
        System.out.println(jsonReply);
    }  

由于我期望它与bean validation失败,我看到了日志必须说的内容。

我看到了

[INFO] [talledLocalContainer] Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
[INFO] [talledLocalContainer]   at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3587) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:103) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:453) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:345) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1218) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:421) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]
[INFO] [talledLocalContainer]   at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:110) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]

这意味着bean validation已传递,错误发生在数据库层,因为数据库需要not null

问题
为什么bean验证不起作用,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

根据您的示例,您正在使用Hibernate Validator 4.0.2.GA以及Bean Validation 1.1.0.Final。那不行。要使用Bean Validation 1.1,您将需要Hibernate Validator 5.x(5.1.2.Final是最新的)。我希望你的例子甚至不能正确引导或至少吃掉日志中有一些警告。