I am trying to use javax.validation.validation-api for validating @QueryParam
parameters. I have followed steps as below:
Added dependency:
<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-bean-validation</artifactId> <version>2.12</version> <exclusions> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </exclusion> </exclusions> </dependency>
Jersey resource:
@GET @Produces(MediaType.APPLICATION_JSON) @Path("/{param1}") @ValidateOnExecution public SomeObject get(@PathParam("param1") String param1, @NotNull @QueryParam("q1") String q1, @NotNull @QueryParam("q2") String q2) { try { //Assuming q1 and q2 are NOT Null here ... } catch(Exception exception) { throw new WebApplicationException(exception, Response.Status.INTERNAL_SERVER_ERROR); } return someObject; }
I tried giving various combination of URLs as in q1 and q2 both parameters absent, q1 absent or q2 absent. Each time,
@NotNull
is not getting detected. Means try block code is getting executed irrespective of q1 and q2 beingnull
.
What else needs to be done?
I referred this link - https://jersey.java.net/documentation/latest/bean-validation.html#d0e11956.
How to check Auto-Discoverable feature is on in my environment?