richersoon richersoon - 3 months ago 30
Java Question

Jersey - Validation is not working for Query Param

It looks like validation for Query Param is not working, but validation for Body is working (Using @POST)

@GET
@Path("{id}")
public Foo get(@PathParam("id") Integer id,
@NotNull @QueryParam("param1") Integer param1) {
....
}


pom.xml

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
</dependency>

Answer

To trigger validation add the @Valid annotation to the param.

@GET
@Path("{id}")
public Foo get(@PathParam("id") Integer id,
               @Valid @NotNull @QueryParam("param1") Integer param1) {
....
}
Comments