在Jersey Client 2中编码花括号

时间:2016-02-26 18:38:44

标签: java urlencode jersey-2.0 url-encoding jersey-client

我们正在使用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} }

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

5 个答案:

答案 0 :(得分: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");

答案 1 :(得分:4)

因此,首先,Jersey在默认情况下执行模板是很疯狂的。其次,这里的所有解决方案都是错误的...对于包含空格的查询参数,URLEncoder.encode(..., "UTF-8")无效。由于URLEncoder会将空格编码为+,Jersey会将其解释为加号,因此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());
}

答案 2 :(得分: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

答案 3 :(得分:0)

您可以使用 URLEncoder.encode(“ ...”,“ UTF-8”)方法

解决此问题
  

String java.net.URLEncoder.encode(String s,String enc)抛出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

答案 4 :(得分:0)

似乎完全如Tapped所预测的那样,上面的Karim解决方案不起作用。空格由URLEncoder.encode编码为加号,然后由Jersey编码为不正确的%2B字符。