Hardy
Hardy

Reputation: 477

Java QueryParam with key and value in the same parameter

I'm creating a endpoint (using Jersey annotations) that receives a list of Strings as it's parameter. The strings need to contain a key and a value. So on the url it would look something like

?params=key1=value1&params=key2=value2&params.... 

Two questions:

Obviously the most standard way would be to have the RHS be the key and the LHS of the equal sign would be the value. The problem is I don't want to define every possible key value pair in my method signature. I want to accept a general list of keys and values and let the user pass in the pairs that are relevant to their query.

So far I've tried several google queries but haven't found an answer:

Upvotes: 0

Views: 3052

Answers (1)

Ken Chan
Ken Chan

Reputation: 90447

Yes. Jersey can handle it even = between key and value is not encoded.If query parameter is params=key1=value1&params=key2=value2 , the params list below will be [key1=value1, key2=value2]

@GET
public Response q34211549(@QueryParam("params") List<String> params) {
    //parse params list and convert it into key and value
}

You can also consider to create a ParamConverter to let Jersey to do this conversion for you.

Reference

Upvotes: 1

Related Questions