【问题标题】:Validate object to RESTeasy method using specific validation group使用特定验证组将对象验证为 RESTeasy 方法
【发布时间】:2013-12-24 10:10:07
【问题描述】:

我正在尝试使用一个或多个特定的验证组来验证 REST 方法的输入,但到目前为止,我一直无法弄清楚如何正确地做到这一点。我正在使用 RESTeasy 3.X 和 Hibernate Validator 5.X。

我知道在 Bean Validation 1.1 规范之前,RESTeasy 方法可以使用以下内容进行注释:

 @ValidateRequest(groups=MyGroup.class)

但是,Hibernate Validator 5.X 中不再存在此功能。

假设我有一个像这样的实体:

 @Entity
 @Table(name = "example_table")
 @XmlRootElement
 public class ExampleEntity implements Mappable, Serializable
 {
     // ...

     @Column(name = "title")
     @NotEmpty(groups = Default.class, ExampleEntityGroup.class)
     @Size(max = 255)
     private String title;

     @Column(name = "description")
     @NotEmpty(groups = Default.class)
     @Size(max = 255)
     private String description;

     // ...
 }

现在我想定义一个 REST PUT 方法来更新这个实体,并且我希望“描述”字段允许一个空值。为此,我想使用我定义的 ExampleEntityGroup 组验证实体(而 Default 组将用于验证创建新对象的 POST 请求)。

现在我的更新方法界面看起来像:

 @Path("{id}")
 @PUT
 @Consumes(...)
 @Produces(...)
 ExampleEntity update(@PathParam("id"), ExampleEntity exampleEntity);

但是,按照所写的,它将始终使用 Default 验证组验证 exampleEntity。我该怎么做才能强制update() 方法使用另一个验证组?

我能找到的唯一一个与此相似的文档是example from the Bean Validation spec that uses an interceptor。有没有更好的办法?

【问题讨论】:

    标签: resteasy bean-validation hibernate-validator


    【解决方案1】:

    带有 Bean Validation 1.1 的 RestEasy 3 将自动验证受约束的资源方法,即只需将约束注释放置到资源方法参数或返回值上,它就会被验证。无法验证特定组,它将始终是默认组。您可以为此尝试为 RestEasy 项目打开一个feature request

    【讨论】:

      猜你喜欢
      • 2021-03-09
      • 1970-01-01
      • 2012-05-17
      • 2022-01-10
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      相关资源
      最近更新 更多