Shankar Shankar - 2 months ago 16
reST (reStructuredText) Question

Spring Restful service - replace queryString with dynamic values

I am trying to consume restful web service using RestTemplate class.

Sample code:

String quote = restTemplate.getForObject("http://musicbrainz.org/ws/2/artist/f27ec8db-af05-4f36-916e-3d57f91ecf5e?&fmt=json&inc=url-rels+release-groups", String.class);
log.info(quote.toString());


In this queryString i want to dynamically pass
f27ec8db-af05-4f36-916e-3d57f91ecf5e
. How do i do that?

Answer

Any url variable can be wrapped in {} and passed in as a variable. If you have more than one it's replace them in order.

If you need a more complicated logic use UriComponentsBuilder to build your url.

String quote = restTemplate.getForObject("http://musicbrainz.org/ws/2/artist/{dynamic-param}?&fmt=json&inc=url-rels+release-groups", String.class, "f27ec8db-af05-4f36-916e-3d57f91ecf5e");