2

I know I can work around this, but it seems very strange that the behaviour is different if you use an annotated query parameter, compared with pulling the parameter out of the parameter map (which should be decoded according to the javadoc). Is this a bug, or just a quirk?

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getAssets(@Context UriInfo info, @QueryParam("q") String searchQuery) {

    // The request URI is http://myhost.com/appRoot?q=foo+bar%20baz
    // At this point seachQuery="foo bar baz"
    // The + has been decoded (along with any % encoded characters)

    // Here searchQuery2="foo+bar baz", the '+' has not been decoded
    // but the %20 has been
    MultivaluedMap<String, String> params = info.getQueryParameters();
    String searchQuery2 = params.get("q").get(0);
ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
idlewis
  • 130
  • 2
  • 8
  • As far as I know query strings are `application/x-www-form-urlencoded`. If you have to send the parameter then you have to send it like `appRoot?q=foo%bar` – Rahul Tripathi Apr 23 '15 at 13:03
  • My understanding (and what this [question](http://stackoverflow.com/questions/1211229/in-a-url-should-spaces-be-encoded-using-20-or) implies is that you can use either a %20 or a + to encode a space in the query string. So in my application I need to be able to deal with both. – idlewis Apr 23 '15 at 13:21
  • If you have to pass a space in the query params, is there a way to decode it using UriInfo? – gjw80 Jun 13 '18 at 16:01

1 Answers1

2

According to the Javadocs for UrlInfo.getQueryParameters only "sequences of escaped octets in parameter names and values are decoded".

On the other hand, QueryParam Javadocs states that "Values are URL decoded unless this is disabled using the Encoded annotation".

So, answering your question, it looks like a specification decision.

Anyway, maybe you should bring up that discussion on JAX-RS mailing lists.

Anthony Accioly
  • 21,918
  • 9
  • 70
  • 118