在Jersey Client 2中编码花括号

问题描述 投票:4回答:5

我们正在使用Jersey Client 2.21。我注意到当我们将花括号(又名花括号)作为参数值时,它不会被正确编码。不仅如此,花括号内的任何内容都不会被编码。对于我测试过的常规括号或其他不安全字符,情况并非如此。

请参阅下面的示例。在这个例子中,我输入三个参数。一个只有空格的控制参数。一个用花括号,一个用常规括号。

public static void testJerseyEncoding() {
    Client client = ClientBuilder.newClient();
    String url = "http://foo.com/path";
    Map<String, String> map = new HashMap<>();
    map.put("paramWithCurly", " {with a space}");
    map.put("paramWithOutCurly", "with a space");
    map.put("paramWithBracket", "[with a space]");
    WebTarget target = client.target(url);
    for (Map.Entry<String, String> entry : map.entrySet()) {
        target = target.queryParam(entry.getKey(), entry.getValue());
    }
    System.out.println(target.toString());
}

这是输出:

JerseyWebTarget { http://foo.com/path?paramWithBracket=%5Bwith+a+space%5D&paramWithOutCurly=with+a+space&paramWithCurly=+{with a space} }

泽西客户端有什么问题,或者我错过了什么?花括号应编码为“%7B”。

java urlencode jersey-2.0 url-encoding jersey-client
5个回答
1
投票

当您使用卷曲值创建参数时,Jersey认为您要使用URL参数。见https://jersey.github.io/documentation/latest/uris-and-links.html

UriBuilder.fromUri("http://localhost/")
 .path("{a}")
 .queryParam("name", "{value}")
 .build("segment", "value");

因此,您应该通过URLEncoder编码花括号,可能如下所述:How to force URIBuilder.path(...) to encode parameters like "%AD"? This method doesn't always encode parameters with percentage, correctly


6
投票

不是手动预编码查询参数值,更好的方法可能是始终使用模板参数,然后使用resolveTemplate()和不安全的值。

Client client = ClientBuilder.newClient();

WebTarget target = client.target("http://server")
            .path("/foo")
            .queryParam("bar", "{bar}")
            .resolveTemplate("bar", "{\"foo\":\"bar\"}");

assertThat(target.getUri().toString())
        .isEqualTo("http://server/foo?bar=%7B%22foo%22%3A%22bar%22%7D");

4
投票

所以,首先,泽西岛默认做模板是疯了。其次,这里的所有解决方案都是错误的......做URLEncoder.encode(..., "UTF-8")不适用于包含空格的查询参数。由于URLEncoder将空间编码为+,泽西岛将其解释为加号,因此Jersey最终将其编码为%2B。请参阅https://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html以供参考。

我提议的解决方案,我不满意(与Java一样)是分别用{}替换所有%7B%7D,如下所示:

Map<String, String> map = new HashMap<>();
map.put("paramWithCurly", " {with a space}".replaceAll("\\{", "%7B").replaceAll("\\}", "%7D"));
map.put("paramWithOutCurly", "with a space");
map.put("paramWithBracket", "[with a space]");
WebTarget target = client.target(url);
for (Map.Entry<String, String> entry : map.entrySet()) {
    target = target.queryParam(entry.getKey(), entry.getValue());
}

0
投票

您可以使用URLEncoder.encode(“...”,“UTF-8”)方法解决此问题

String java.net.URLEncoder.encode(String s,String enc)throws UnsupportedEncodingException

使用特定的编码方案将字符串转换为application / x-www-form-urlencoded格式。此方法使用提供的编码方案来获取不安全字符的字节。

使用URLEncoder.encode更新代码

    try { 
        map.put("paramWithCurly", URLEncoder.encode(" {with a space}", "UTF-8"));
        map.put("paramWithOutCurly", URLEncoder.encode("with a space", "UTF-8"));
        map.put("paramWithBracket", URLEncoder.encode("[with a space]", "UTF-8"));
    } catch (UnsupportedEncodingException e1) {
         System.err.println("........");
    }

这是输出:

JerseyWebTarget { http://foo.com/path?paramWithBracket=%5Bwith%2Ba%2Bspace%5D&paramWithOutCurly=with%2Ba%2Bspace&paramWithCurly=%2B%7Bwith%2Ba%2Bspace%7D }

暗示:-

using UTF-8 as the encoding scheme the string "The string ü@foo-bar" would get converted to "The+string+%C3%BC%40foo-bar" 

参考:https://stackoverflow.com/a/607403/6638958


0
投票

似乎Karim的上述解决方案无法正常工作,正如Tapped预测的那样。这些空格由URLEncoder.encode编码为加号,然后由Jersey编码为%2B字符,这是不正确的。

© www.soinside.com 2019 - 2024. All rights reserved.