Jersey - validate REST service parameters

2019-07-29 22:31发布

问题:

is there any tool to simply validate query params using Jersey (and Maven)? I'm trying to do something like this using @NotNull annotation (jersey-bean-validation):

@GET
@Path("/count")
@Produces(MediaType.APPLICATION_XML)
public Response count(@NotNull @QueryParam("my_param") String my_param) {
    //TODO automatically return 400 Bad Request if my_param is null
    return Response.ok("This is a response", MediaType.APPLICATION_XML).build();
}

I'd like the method to automatically validate query parameters and return 400 Bad Request if no "my_param" is provided.

回答1:

My guess is you are using Jersey 1.x, which does not support annotated validations against PathParams or QueryParams.

See Nick Telfords comments in https://groups.google.com/forum/#!topic/dropwizard-user/wb6iwrpSrSE