Jersey Queryparam 不接受带“+”号的整数值

Jersey Queryparam does not accept integer values with "+" sign

我有如下方法

@GET
@Path("/display")
@Produces(value = { "application/json;qs=1", "application/xml;qs=.5" })
public Response displayItems(@QueryParam("limit") int limit,
                             @QueryParam("offset") int offset){
   //Code to display the requested number of Items
}

当我用 +sign 传递查询参数时,

 http://{host}:{post}/{context}/display?limit=+1&offset=+2

limit=+2 或 offset=+2。它抛出一个错误。当我尝试调试时,它将值设为 {Space}2(因此它无法将 {space}2 映射到 int)。它适用于负值(limit=-2 或 offset=-2)。这与编码有关吗?我错过了什么?

是的,这是一个编码问题。根据 RFC3986,'+' 是保留字符,这意味着如果您希望它被正确理解,则必须对其进行百分比编码,对于 '+' 表示编码为 '%2B'。 '-' 字符是明确未保留的,因此 Jersey 可以按原样理解它。

那么,您的 URI 看起来像...

http://{host}:{post}/{context}/display?limit=+1&offset=+2

真的应该写...

http://{host}:{post}/{context}/display?limit=%2B1&offset=%2B2