Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey 1.x is replacing the plus symbol with a space symbol. How can I prevent this?

I'm using the jersey client to send a query param to my jersey server. This is the query: ?sort=id+ASC

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

like image 431
Daniel Kaplan Avatar asked Feb 14 '23 21:02

Daniel Kaplan


1 Answers

Apart from @IanRoberts's suggestion, you could make use of the @Encoded annotation to get to the original undecoded value of you parameter (by default Jersey decodes the values and that's why id+ASC becomes id ASC in 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();
}

To change the behavior you just add @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();
}
like image 151
Bogdan Avatar answered May 01 '23 03:05

Bogdan