简体   繁体   English

Spring RestTemplate的“#”字符编码

[英]Spring RestTemplate character encoding for “#”

I'm really having a tough time with some character encoding issues with "#". 对于“#”的某些字符编码问题,我真的很难过。 I'm trying to "GET" from a url "URL/path/#, with literally the "#" in front of the param. The endpoint is expecting the character encoded value for "#", or "%23". However, the RestTemplate is either a) not encoding "#" which results in other exceptions from the API (the API really wants "%23" and not "#", or b) double encoding the "%". How can I use the RestTemplate to literally send "%23". 我正在尝试从URL“ URL / path /#”中获取“ GET”,而在参数前使用“#”。端点期望字符编码值为“#”或“%23”。 ,RestTemplate是a)不编码“#”,从而导致API出现其他异常(API确实需要“%23”而不是“#”,或者b)对“%”进行了双重编码。 RestTemplate从字面上发送“%23”。

The code is pretty simple: 代码很简单:

restTemplate.exchange(url, HttpMethod.GET, httpEntityWithSomeAuthHeader, 
    new ParameterizedTypeReference<SomeObject>() {}); 

url is as described above... 网址如上所述。

Thanks! 谢谢!

Hope not to late ;) 希望不要迟到;)

try this one: 试试这个:

URI uri = new URI(url);
restTemplate.exchange(uri, HttpMethod.GET, httpEntityWithSomeAuthHeader, 
new ParameterizedTypeReference<SomeObject>() {}); 

And important, use %23 in url param. 重要的是,在URL参数中使用%23。 It's seems that if you use # (with string or uri) it discard all after... but with %23 and "new URI" it put in query (inside uri as %23) and the server decode as #. 看来,如果您使用#(带有字符串或uri),它将在之后丢弃所有内容……但是使用%23和“ new URI”,它将放入查询(在uri内部为%23),服务器将解码为#。

Hope it's help ;) 希望能有所帮助;)

PD: A co-worker remembered me this https://xkcd.com/979/ when we saw your answerd question ;) and now we have the answerd PD:一位同事记得我这个https://xkcd.com/979/,当我们看到您回答的问题时;)现在我们已经回答了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM