我知道我可以解决这个问题,但是与从参数映射中提取参数(应根据 javadoc 进行解码)相比,如果使用带注释的查询参数,行为会有所不同,这似乎很奇怪。这是一个错误,还是只是一个怪癖?
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getAssets(@Context UriInfo info, @QueryParam("q") String searchQuery) {
// The request URI is http://myhost.com/appRoot?q=foo+bar%20baz
// At this point seachQuery="foo bar baz"
// The + has been decoded (along with any % encoded characters)
// Here searchQuery2="foo+bar baz", the '+' has not been decoded
// but the %20 has been
MultivaluedMap<String, String> params = info.getQueryParameters();
String searchQuery2 = params.get("q").get(0);
最佳答案
根据 UrlInfo.getQueryParameters
的 Javadocs仅“对参数名称和值中的转义八位字节序列进行解码”。
另一方面,QueryParam Javadocs 声明“除非使用 Encoded 注释禁用,否则值将URL 解码”。
所以,回答你的问题,这看起来像是一个规范决定。
无论如何,也许您应该在 JAX-RS mailing lists 上提出该讨论.
关于java - 为什么 UriInfo.getQueryParameters() 不解码 '+' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29824160/