For the following example (using Jersey 2.6), percent encoded query parameters are not decoded, where in contrast, a + is replaced by a space.
@Path("test")
public class TestResource {
@Path("/")
@GET
public void test(@QueryParam("param") String param) {
System.out.println(param);
}
}
// http://localhost:8080/test?param=hello+world // prints "hello world"
// http://localhost:8080/test?param=hello%20world // prints "hello%20world"
Is there a reason, why only the + is unescaped automatically? Is there a simple way, to make all query parameters to be fully decoded, without having to do that at every method's beginning?
In my solution, I disable automatic decoding of query parameter, and then do it myself.
@Path("test")
public class TestResource {
@Path("/")
@GET
public void test(@Encoded @QueryParam("param") String param) {
try {
param = URLDecoder.decode(param, "UTF-8").replace("+", " ");
System.out.println(param);
} catch (UnsupportedEncodingException e) {
// The exception should never happened since UTF-8 is definitely
// supported.
}
}
}
Though it may not be a pretty solution, but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With