Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey: Decoding of QueryParam with percent encoding

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?

like image 714
qqilihq Avatar asked May 27 '14 22:05

qqilihq


1 Answers

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.

like image 170
user1802604 Avatar answered Oct 21 '22 09:10

user1802604