我正在尝试创建一个包含“#”的 URI。我知道需要用“%23”替换,所以我这样做了。但是,当使用 WebClient 连接到端点时,它会自动将“%”替换为“%25”,因此我最终得到“%2523”。
我尝试只保留“#”字符,认为它用编码格式替换了所有非法字符(我可能在这里使用了错误的术语),但它并没有替换它。以下是我所拥有的,没什么太复杂的。
if(tag.contains("#")){
tag = tag.replace("#", "%23");
}
UserStatistics user = new UserStatistics();
String playersEndpoint = "players/";
JSONObject json = webClient.get()
.uri(playersEndpoint + tag)
.retrieve()
.bodyToMono(JSONObject.class)
.block();
我的 URI 的其余部分是在实例化 WebClient 对象时创建的,位于类之前。
你可以尝试这样的事情:
String url = "https://example.com?q=" + URLEncoder.encode(q, StandardCharsets.UTF_8);
发生这种情况的原因是因为 Java spring 使用所谓的百分比编码 - 这就是为什么 % 被转换为 %25,更多信息请参见这里: https://en.wikipedia.org/wiki/Percent-encoding
如果您可以提供有关在何处以及如何定义标签的更多详细信息,我也许可以提供其他具体建议。