【问题标题】:Jersey: Decoding of QueryParam with percent encodingJersey:使用百分比编码解码 QueryParam
【发布时间】:2014-05-27 22:21:48
【问题描述】:

对于以下示例(使用 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"

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

【问题讨论】:

  • 你在 websphere 上吗?

标签: java jersey jersey-2.0


【解决方案1】:

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

@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.
        }
    }
}

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

【讨论】:

    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2021-10-10
    • 2013-10-13
    相关资源
    最近更新 更多