在Jersey Client 2中编码花括号 [英] Encoding curly braces in Jersey Client 2

查看:172
本文介绍了在Jersey Client 2中编码花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are using Jersey Client 2.21. I am noticing that when we put curly braces (aka curly brackets) as a param value, then it does not get properly encoded. Not only that, but anything inside the curly braces does not get encoded either. This is not true for regular brackets or other unsafe characters that I have tested with.

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

Please see the example below. In this example I enter three params. A control param with just spaces. One with curly braces, and one with regular brackets.

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。

Is something broken with the Jersey Client or am I missing something? The curly braces should have been encoded to "%7B".

推荐答案

当你创建一个卷曲值为的参数时,泽西岛认为你想使用URL参数。请参阅 https://jersey.java.net/documentation/latest/uris -and-links.html

When you create a parameter with a value in curly, Jersey thinks you want to use a URL parameter. See https://jersey.java.net/documentation/latest/uris-and-links.html.

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

所以你应该通过URLEncoder对花括号进行编码,可能如下所述:如何强制URIBuilder.path(...)编码像%AD这样的参数?此方法并不总是正确编码百分比参数

So you should encode curly braces yourselves via URLEncoder, probably as described there: How to force URIBuilder.path(...) to encode parameters like "%AD"? This method doesn't always encode parameters with percentage, correctly.

这篇关于在Jersey Client 2中编码花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆