【问题标题】:Encoding curly braces in Jersey Client 2在 Jersey Client 2 中编码花括号
【发布时间】:2016-06-10 02:45:00
【问题描述】:

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


    【解决方案1】:

    与其手动预编码查询参数值,更好的方法可能是始终使用模板参数,然后使用带有不安全值的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");
    

    【讨论】:

      【解决方案2】:

      所以,首先,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());
      }
      

      【讨论】:

      • 只转义花括号是一个不错的技巧。你拯救了我的一天!!!
      【解决方案3】:

      当您创建一个带有 curly 值的参数时,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

      【讨论】:

      • 感谢您揭开了为什么它没有对花括号进行编码的奥秘。知道如何关闭这个花括号“功能”吗?对于 jersey 客户端框架以不同方式处理花括号似乎是一个坏主意,花括号是可编码的,因此可能会出现在 URI 中。
      【解决方案4】:

      你可以使用 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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 这不是一个真正的答案。你有什么建议来解决这个问题?
        • 对不起。我使用target = target.queryParam(entry.getKey(), UriComponent.encode(entry.getValue(), UriComponent.Type.QUERY_PARAM)); 而不是target = target.queryParam(entry.getKey(), entry.getValue());
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-18
        • 1970-01-01
        • 1970-01-01
        • 2019-01-26
        • 2012-01-22
        • 2018-12-21
        • 1970-01-01
        相关资源
        最近更新 更多