Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query parameter validation with Jersey

I'm using Jersey in Java for a simple web server. I'm trying to handle with url query parameters, like

/path?value1=X&value2=Y&value3=Z

I was able to extract the query value by using the @QueryParam annotation like

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/path")
public String getUpdate(@QueryParam("All") List<String> allParam, 
        @QueryParam(value = "value1") String value1, 
        @QueryParam(value = "value2") String value2, 
        @QueryParam(value = "value3") String value3) {
        ...
}

Now my problem is I need to validate the input. I'm mainly trying to make sure that value1, value2 and value3 follow a specific format. I also want to make sure that those parameters are not empty.

I checked out the Bean Validation documentation for Jersey, but it seemed hard to follow. This is probably because I'm still somewhat new to Jersey framework.

So how can I set up query parameter validations? Are there easier to follow resources/examples I could be directed to? Thanks

like image 584
user5317970 Avatar asked Sep 09 '15 16:09

user5317970


People also ask

Which type of Bean Validation method provided in Jersey?

Validation is the process of verifying that some data obeys one or more pre-defined constraints. It is, of course, a very common use case in most applications. The Java Bean Validation framework (JSR-380) has become the de-facto standard for handling this kind of operations in Java.

What is @QueryParam?

@QueryParam: QueryParam is used when the requirement is to filter the request based on certain criteria/criterias.


1 Answers

As @VA31 linked, you can use bean validation like this, if your jersey is latest enough.

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/path")
public Response getUpdate(
    @QueryParam("value1")
    @NotNull
    @Size(min = 1, max = 255)
    @Pattern(regexp = "\\d+")
    String value1, 
    @QueryParam("value2")
    @Min(52)
    @Max(53)
    int value2, 
    @QueryParam("value3")
    @NotNull
    Integer value3) {


}
like image 191
Jin Kwon Avatar answered Oct 19 '22 19:10

Jin Kwon