java – Jersey 1. X is replacing the plus sign with a space symbol How can I stop this?

I am using Jersey client to send query parameters to my jersey server This is the query:? sort = id ASC

But in my code that retrieves this query parameter, I return uriinfo getQueryParameters(). getFirst(“sort”);, This value is calculated as ID ASC Why does this happen and how can I prevent it?

Solution

In addition to @ ianroberts' suggestion, you can use the @ encoded annotation to get the original decoded value of your parameter (by default, Jersey will decode the value, which is why ID ASC becomes ID ASC in your code)

The following example retrieves the decoded value of 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, simply 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();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>