使用Resteasy验证请求的正确方法是什么?

时间:2022-10-22 10:47:25

I use Resteasy in combination with Google Guice using Resteasy-Guice. I have been looking for ways to validate my request bodies. I want to do for example:

我使用Resteasy-Guice将Resteasy与Google Guice结合使用。我一直在寻找验证我的请求机构的方法。我想做的例如:

public static class MyPojo {

   @NotEmpty private String contents;

}

And then use in my resource

然后在我的资源中使用

@POST
@ValidateRequest
public void doPost(@Valid MyPojo myPojo) {
   // use myPojo only if valid
}

I have been working with the resteasy-hibernate-validator-provider. But since I switched to newer versions, this introduced the (unwanted?) dependency to EJB. See also: RESTEASY-1056. In the comments is stated that you should switch to the newer validator-11 instead:

我一直在使用resteasy-hibernate-validator-provider。但是,由于我切换到了更新的版本,因此向EJB引入了(不需要的?)依赖项。另请参见:RESTEASY-1056。在评论中声明您应该切换到较新的验证器-11:

Switch to resteasy-validator-provider-11, which implements the newer Bean Validation 1.1 specification.

切换到resteasy-validator-provider-11,它实现了更新的Bean Validation 1.1规范。

The docs say:

文档说:

Validation is turned on by default (assuming resteasy-validator-provider-11-.jar is available), though parameter and return value validation can be turned off or modified in the validation.xml configuration file. See the Hibernate Validator documentation for the details.

默认情况下启用验证(假设resteasy-validator-provider-11-.jar可用),但可以在validation.xml配置文件中关闭或修改参数和返回值验证。有关详细信息,请参阅Hibernate Validator文档。

I however do not manage to get this working to my configuration, because I find myself including dependencies like hibernate-validator, javax.el-api, javax.el and hibernate-validator-cdi and annotations like ValidateOnExecution. I however do not find any of this being instantiated or invalid requests being rejected.

然而,我没有设法让这个工作到我的配置,因为我发现自己包括依赖关系,如hibernate-validator,javax.el-api,javax.el和hibernate-validator-cdi以及类似ValidateOnExecution的注释。但是,我没有发现任何实例化或无效请求被拒绝。

What is the preferred, lightweight, and working way to do validation with Resteasy?

使用Resteasy进行验证的首选,轻量级和工作方式是什么?

2 个解决方案

#1


4  

You do not have to specify any annotation on the resource itself or do additional configuration. Just the constraint annotations on POJOs are enough get it working.

您不必在资源本身上指定任何注释或执行其他配置。只需对POJO进行约束注释即可使其正常工作。

My setup is as follows:

我的设置如下:

The resource method:

资源方法:

@POST
public void doPost(@Valid MyPojo myPojo) {
   // use myPojo only if valid
}

The POJO:

public static class MyPojo {
   @NotEmpty private String contents;
}

Tested with the following dependencies:

使用以下依赖项进行测试:

javax.validation version 1.1.0.Final

javax.validation版本1.1.0.Final

resteasy-validator-provider-11 version 3.0.11.Final

resteasy-validator-provider-11 version 3.0.11.Final

hibernate-validator version 5.0.0.Final and 5.0.1.Final

hibernate-validator版本5.0.0.Final和5.0.1.Final

#2


3  

  • I accidentally had a transitive dependency to the hibernate-validator-provider which caused previous tries to fail. Ensure that you do not have a transitive dependency to the hibernate-validator-provider. For me this caused the following exception: issues.jboss.org/browse/RESTEASY-826 .
  • 我不小心对hibernate-validator-provider有一个传递依赖,导致以前的尝试失败。确保您没有对hibernate-validator-provider的传递依赖。对我来说,这导致了以下异常:issues.jboss.org/browse/RESTEASY-826。

  • Based on Thomas answer I added dependencies to javax.validation, resteasy-validator-provider-11, hibernate-validator.
  • 根据Thomas的回答,我在javax.validation,resteasy-validator-provider-11,hibernate-validator中添加了依赖项。

  • Then I still received exceptions (java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException). Based on this answer I added javax.el-api and el-impl as dependencies. I think this is because I use an embedded servlet container.
  • 然后我仍然收到异常(java.lang.NoClassDefFoundError:javax / el / PropertyNotFoundException)。基于这个答案,我添加了javax.el-api和el-impl作为依赖项。我想这是因为我使用嵌入式servlet容器。

  • I had to remove the @ValidateOnRequest annotation on the resources, they are not necessary anymore
  • 我不得不删除资源上的@ValidateOnRequest注释,它们不再是必需的

Final working configuration:

最终工作配置:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-validator-provider-11</artifactId>
        <version>3.0.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>
    </dependency>

#1


4  

You do not have to specify any annotation on the resource itself or do additional configuration. Just the constraint annotations on POJOs are enough get it working.

您不必在资源本身上指定任何注释或执行其他配置。只需对POJO进行约束注释即可使其正常工作。

My setup is as follows:

我的设置如下:

The resource method:

资源方法:

@POST
public void doPost(@Valid MyPojo myPojo) {
   // use myPojo only if valid
}

The POJO:

public static class MyPojo {
   @NotEmpty private String contents;
}

Tested with the following dependencies:

使用以下依赖项进行测试:

javax.validation version 1.1.0.Final

javax.validation版本1.1.0.Final

resteasy-validator-provider-11 version 3.0.11.Final

resteasy-validator-provider-11 version 3.0.11.Final

hibernate-validator version 5.0.0.Final and 5.0.1.Final

hibernate-validator版本5.0.0.Final和5.0.1.Final

#2


3  

  • I accidentally had a transitive dependency to the hibernate-validator-provider which caused previous tries to fail. Ensure that you do not have a transitive dependency to the hibernate-validator-provider. For me this caused the following exception: issues.jboss.org/browse/RESTEASY-826 .
  • 我不小心对hibernate-validator-provider有一个传递依赖,导致以前的尝试失败。确保您没有对hibernate-validator-provider的传递依赖。对我来说,这导致了以下异常:issues.jboss.org/browse/RESTEASY-826。

  • Based on Thomas answer I added dependencies to javax.validation, resteasy-validator-provider-11, hibernate-validator.
  • 根据Thomas的回答,我在javax.validation,resteasy-validator-provider-11,hibernate-validator中添加了依赖项。

  • Then I still received exceptions (java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException). Based on this answer I added javax.el-api and el-impl as dependencies. I think this is because I use an embedded servlet container.
  • 然后我仍然收到异常(java.lang.NoClassDefFoundError:javax / el / PropertyNotFoundException)。基于这个答案,我添加了javax.el-api和el-impl作为依赖项。我想这是因为我使用嵌入式servlet容器。

  • I had to remove the @ValidateOnRequest annotation on the resources, they are not necessary anymore
  • 我不得不删除资源上的@ValidateOnRequest注释,它们不再是必需的

Final working configuration:

最终工作配置:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-validator-provider-11</artifactId>
        <version>3.0.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>
    </dependency>