Jersey / JAX-RS资源方法输入bean验证 [英] Jersey/JAX-RS resource method input bean validation
问题描述
我通过DropWizard 0.7.1使用Jersey / JAX-RS来公开RESTful服务端点。我的所有实体POJO都注释了JAX-RS和Hibernate / JSR-303 bean验证注释,如下所示:
I am using Jersey/JAX-RS via DropWizard 0.7.1 to expose RESTful service endpoints. I have all of my entity POJOs annotated with both JAX-RS and Hibernate/JSR-303 bean validation annotations like so:
public class Widget {
@JsonProperty("fizz")
@NotNull
@NotEmpty
private String fizz; // Can't be empty or null
@JsonProperty("buzz")
@Min(value=5L)
private Long buzz; // Can't be less than 5
// etc.
}
当资源方法收到其中一个POJO作为输入时(在引擎盖下,DropWizard已经将HTTP实体JSON反序列化为 Widget
实例),我会喜欢根据Hibernate / Bean验证注释验证它:
When a resource method receives one of these POJOs as input (under the hood, DropWizard has already deserialized the HTTP entity JSON into a Widget
instance), I would like to validate it against the Hibernate/Bean Validation annotations:
@POST
Response saveWidget(@PathParam("widget") Widget widget) {
// Does DropWizard or Jersey have something built-in to automagically validate the
// 'widget' instance?
}
可以将DropWizard / Jersey配置为验证我的小部件
实例,我不必在这里写任何验证码?
Can DropWizard/Jersey be configured to validate my widget
instance, without me having to write any validation code here?
推荐答案
添加 @Valid
在 @PathParam
之前用泽西岛验证。
Add @Valid
before @PathParam
to validate with Jersey.
参见 https://jersey.java.net/documentation/latest/bean-validation.html#d0e12201
您可能需要进行一些配置。
You may have to do some configuration.
这篇关于Jersey / JAX-RS资源方法输入bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!