0

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?

WildDev
  • 2,250
  • 5
  • 35
  • 67
  • Hello WildDev, it doesn't. What are you trying to accomplish? – de.la.ru Apr 28 '15 at 12:44
  • @Kirill, hello. I want to filter incoming Query data to protect the app from incorrect data. I know that there's Filter exists, but the param matching approach looks better so i would to know is there the same approach not only for PathParams, but for QueryParams as well – WildDev Apr 28 '15 at 12:58

2 Answers2

2

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

Anton Sarov
  • 3,712
  • 3
  • 31
  • 48
1

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
}
de.la.ru
  • 2,994
  • 1
  • 27
  • 32