【问题标题】:Bean Validation for POST requisition in JAX-RS with Jersey implementation带有 Jersey 实现的 JAX-RS 中 POST 申请的 Bean 验证
【发布时间】:2013-04-16 20:52:40
【问题描述】:

我正在为 JAX-RS 使用 Jersey 实现,我正在寻找一个可以在 POST 申请中使用 Bean Validation 的示例。我有这个操作,例如:

@POST
@Path("document/annotations/save")
@Produces("application/json")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Map<String, Object> saveAnnotation(
        @FormParam("user") String userStr,
        @FormParam("documentId") String documentId,
        @FormParam("documentPage") Integer documentPage,
        @FormParam("annotationContent") String annotationContent,
        @FormParam("annotationId") Long annotationId,
        @FormParam("isMobile") Boolean isMobile) { // the code... }

我想为每个方法参数使用验证约束(@NotNull@Pattern 等)。我看到了doc,他们正在使用 Seam 框架来做到这一点。

目前,我正在尝试使用 javax.validation 实现来验证我的请求,但它不起作用。

有没有办法将 JSR-303 规范与 JAX-RS 一起使用?

Tnks。

【问题讨论】:

  • AspectJ这样使用aop怎么样?

标签: validation http jersey jax-rs


【解决方案1】:

目前使用泽西岛是不可能的;一种可能的替代方法是编写客户资源过滤器并绑定到@NotNull 等注释。

如果将其封装在资源类中会更简单,因为您可以绑定到方法上的 @Valid 注释并一次性验证 bean。

因为 JSR-303 旨在处理 bean 而不是参数集合,所以当您尝试将其弯曲到您的意愿时,它最终会变得非常冗长。

恕我直言,最好不要将验证保留在您的类中,而是使用管道和过滤器模式,即ContainerRequestFilter,或者按照@Willy 的建议使用 AspectJ 之类的东西。

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 2015-02-07
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 2018-12-11
    相关资源
    最近更新 更多