spring resttemplate url encoding

There is no easy way to do this. URI template variables are usually meant for path elements or a query string parameters. You’re trying to pass a host. Ideally, you’d find a better solution for constructing the URI. I suggest Yuci’s solution.

If you still want to work with Spring utilities and template expansion, one workaround is to use UriTemplate to produce the URL with the URI variables as you have them, then URL-decode it and pass that to your RestTemplate.

String url = "http://{enpointUrl}?method=logout&session={sessionId}";
URI expanded = new UriTemplate(url).expand(endpointUrl, sessionId); // this is what RestTemplate uses 
url = URLDecoder.decode(expanded.toString(), "UTF-8"); // java.net class
template.getForObject(url, Object.class);

Leave a Comment