Jersey 1.x replaces the plus sign with a space character. How can I prevent this?

I am using a jersey client to send a request parameter to my jersey server. This is the request:?sort=id+ASC

But in my code that retrieves this query parameter return uriInfo.getQueryParameters().getFirst("sort");, this value evaluates to id ASC. Why is this happening and how can I prevent it?

+3
source share
1 answer

Besides the @IanRoberts suggestion, you can use the annotation @Encodedto go to the original unspecified value of your parameter (by default, Jersey decodes the values ​​and therefore id+ASCbecomes id ASCin your code).

The following example retrieves the decoded value as for the default behavior:

@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response test(@QueryParam("sort") String sort) {
    // sort is "id ASC"
    return Response.ok().entity(sort).build();
}

, @Encoded:

@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response test(@QueryParam("sort") @Encoded String sort) {
    // sort is "id+ASC"
    return Response.ok().entity(sort).build();
}
+6

All Articles