WildDev
WildDev

Reputation: 2367

JavaEE Web JAX-RS: how to match Query params

JAX-RS allows to match PathParams using regex like this:

@GET
@Path("/users/{username : [a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByUserName(@PathParam("username") String username) {
     ... 
}

But does it allow to match QueryParams as well somehow?

Upvotes: 0

Views: 396

Answers (2)

de.la.ru
de.la.ru

Reputation: 3144

Unfortunately there is no such way of protecting the QueryParams. What you could do is to use an auxiliary method on your server side class to check their values.

@GET
@Path("/myPath")
public Response yourMethod(@QueryParam("code") long code, @QueryParam("description") String desc){
    if(isParametersValid(code,desc)){
        //proceed with your logic
    }else{
       return Response.status(Response.Status.FORBIDDEN).entity("The passed parameters are not acceptable").build();
    }
}  

private boolean isParametersValid(long code, String desc){
    //check
}

Upvotes: 1

Anton Sarov
Anton Sarov

Reputation: 3748

You are not allowed to specify regex for QueryParams. As a workaround: just define your own Java data type that will serve as a representation of this query param and then convert this to String if you have to deal with Strings

Upvotes: 2

Related Questions