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?
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();
}
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