简体   繁体   English

使用JSR303验证JAX-RS(Apache Wink)资源?

[英]Validating JAX-RS (Apache Wink) resources with JSR303?

Is it possible to make effective use of JSR303 (Bean Validation) annotations on JAX-RS resources? 是否可以在JAX-RS资源上有效使用JSR303(Bean Validation)注释?

So for example, if I have a resource member that I've annotated @NotEmpty, generate an error back to the client if this constraint is not met? 例如,如果我有一个资源成员,我注释了@NotEmpty,如果不满足此约束,则会向客户端生成错误?

This seems like the obvious thing to do, but also happy to be told better ways (I don't want to move the validation down to the ORM/database level) 这似乎是显而易见的事情,但也乐于被告知更好的方式(我不想将验证移到ORM /数据库级别)

Do you really mean validating the resource members? 你真的是要验证资源成员吗? Usually the resource members are injected in this way or another (it's either contexts, or entity, or path/query/matrix params), as long as the JAX-RS framework works you'll get these members properly injected. 通常资源成员以这种方式或其他方式注入(它是上下文,实体或路径/查询/矩阵参数),只要JAX-RS框架工作,您就可以正确地注入这些成员。

Personally I think it makes more sense to validate the entity since it has arrived by the wire, filled by a MessageBodyReader and basically you have no idea what's inside, right? 我个人认为验证实体更有意义,因为它已经通过电线到达,由MessageBodyReader填充,基本上你不知道里面是什么,对吧?

So if you decide to validate the entities, there are several approaches that you can take: 因此,如果您决定验证实体,可以采用以下几种方法:

  1. AFAIK, Apache Wink does not support built-in validations. AFAIK,Apache Wink不支持内置验证。 You can implement a handler. 您可以实现处理程序。 See DeploymentConfiguration.initRequestHandlersChain() . 请参阅DeploymentConfiguration.initRequestHandlersChain() It supports adding user handlers. 它支持添加用户处理程序。 In your handler you can perform any validations. 在您的处理程序中,您可以执行任何验证。 I even think that the Wink community will be glad if you contribute this code. 我甚至认为如果你贡献这个代码,Wink社区会很高兴。
    The only problem with this approach - it's bound to the Apache Wink. 这种方法唯一的问题 - 它与Apache Wink绑定。 It won't work if you decide to move to a different JAX-RS framework. 如果您决定转移到不同的JAX-RS框架,它将无法工作。

  2. Another approach is to make this validation in your own MessageBodyReader . 另一种方法是在您自己的MessageBodyReader进行此验证。 All you need to do is registering a special reader for your entities and validate the entity inside. 您需要做的就是为您的实体注册一个特殊的阅读器并验证其中的实体。 You can still take advantage of standard MessageBodyReaders (like JAXB or Jackson) by using @Context Providers.getMessageBodyReader() . 您仍然可以使用@Context Providers.getMessageBodyReader()来利用标准的MessageBodyReaders(如JAXB或Jackson @Context Providers.getMessageBodyReader() The good part of this approach that it's standard JAX-RS. 这种方法的好处在于它是标准的JAX-RS。 The bad that you use MessageBodyReaders for something they were not designed to. 你使用MessageBodyReaders做他们不想要的东西的坏处。

  3. The simplest approach is to validate the entity in first line of each resource method. 最简单的方法是在每个资源方法的第一行验证实体。 It will create some code duplication, but sometimes simplicity wins. 它会创建一些代码重复,但有时简单获胜。

One solution - as I'm using Spring 2.5.x, I can create a wrapper class that implements InitializingBean and delegates to Hibernate's validator. 一个解决方案 - 因为我使用Spring 2.5.x,我可以创建一个实现InitializingBean的包装类,并委托给Hibernate的验证器。 It works - is there a better solution? 它有效 - 有更好的解决方案吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM