1

Is there a String bulder for URL's similar the UriComponentsBuilder? Because the blanks in my URL-String are replaced with %20.

UriComponentsBuilder builder = UriComponentsBuilder
    .fromPath("/test")
    .queryParam("param", "param 1");
String urlWithParams = builder.build().encode().toUriString();

The Result of urlWithParams is /test?param=param%201 but I need /test?param=param 1

My current workaround is urlWithParams.replace("%20", " ");

Eric Darchis
  • 24,537
  • 4
  • 28
  • 49
  • 7
    You can't have spaces in URLs, they have to be encoded. In other words, UriComponentsBuilder is doing the right thing. See: http://stackoverflow.com/questions/497908/is-a-url-allowed-to-contain-a-space – Joe Clay Jan 06 '17 at 13:39
  • Usually web frameworks have similar. Do You use some? BTW. manually connecting can have more problems, not only %20. One of answers is use URLEncode – Jacek Cz Jan 06 '17 at 13:42
  • 1
    It appears to me that you are trying to get something invalid (URI with spaces). Not sure if I can understand correctly; If you are about to build String - perhaps you should use StringBuilder? If you are trying to decode spaces etc. - check this out: http://stackoverflow.com/questions/6138127/how-to-do-url-decoding-in-java – Witold Kaczurba Jan 06 '17 at 13:45
  • 1
    What are you actually trying to do? – Witold Kaczurba Jan 06 '17 at 13:50
  • i use the RestTemplate.exchange Function. I cant use the URI parameter becourse in the last step i combine the base path with the expanded path like "http://server/domain" + urlWithParams. This will be done in another Class. – Manuel Weiß Jan 06 '17 at 13:56
  • The URLDecoder works perfect. Thanks a lot for the link Vito. – Manuel Weiß Jan 06 '17 at 14:00

0 Answers0