gpt4 book ai didi

java - Jersey:使用百分比编码解码 QueryParam

转载 作者:搜寻专家 更新时间:2023-10-31 20:34:19 28 4
gpt4 key购买 nike

对于以下示例(使用 Jersey 2.6),百分比编码的查询参数未解码,相反,+ 被空格替换。

@Path("test")
public class TestResource {
@Path("/")
@GET
public void test(@QueryParam("param") String param) {
System.out.println(param);
}
}

// http://localhost:8080/test?param=hello+world // prints "hello world"
// http://localhost:8080/test?param=hello%20world // prints "hello%20world"

有什么原因,为什么只有 + 会自动转义?有没有一种简单的方法可以使所有查询参数都被完全解码,而不必在每个方法开始时都这样做?

最佳答案

在我的解决方案中,我禁用查询参数的自动解码,然后自己进行。

@Path("test")
public class TestResource {
@Path("/")
@GET
public void test(@Encoded @QueryParam("param") String param) {
try {
param = URLDecoder.decode(param, "UTF-8").replace("+", " ");
System.out.println(param);
} catch (UnsupportedEncodingException e) {
// The exception should never happened since UTF-8 is definitely
// supported.
}
}
}

虽然这可能不是一个很好的解决方案,但它确实有效。

关于java - Jersey:使用百分比编码解码 QueryParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899934/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com