IAmYourFaja
IAmYourFaja

Reputation: 56914

Jersey/JAX-RS resource method input bean validation

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?

Upvotes: 6

Views: 10329

Answers (1)

Adam
Adam

Reputation: 44929

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.

Upvotes: 9

Related Questions