Jersey/JAX-RS resource method input bean validation

半世苍凉 提交于 2019-12-01 02:52:40

问题


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.
}

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?
}

Can DropWizard/Jersey be configured to validate my widget instance, without me having to write any validation code here?


回答1:


Add @Valid before @PathParam to validate with Jersey.

See https://jersey.java.net/documentation/latest/bean-validation.html#d0e12201

You may have to do some configuration.



来源:https://stackoverflow.com/questions/27389629/jersey-jax-rs-resource-method-input-bean-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!