Jersey/JAX-RS resource method input bean validation

10,031

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.

Share:
10,031
IAmYourFaja
Author by

IAmYourFaja

my father is a principal at burgoyne intnl and got me this job programming lisp and development. I aspire to unittesting with a concentration in mobile platforms.

Updated on June 17, 2022

Comments

  • IAmYourFaja
    IAmYourFaja about 2 years

    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?

  • IAmYourFaja
    IAmYourFaja over 9 years
    Thanks @Adam (+1) - this is exactly what I was looking/hoping for. Just curious, what kind of general configuration might I have to do? Thanks again!