Search code examples
springweb-servicesrestresttemplate

Consume REST Web Service from relative uri


Is it possible to consume a RESTful web service from a relative uri, instead of an absolute? When I run the following code, I get an exception java.lang.IllegalArgumentException: URI is not absolute.

RestTemplate rt = new RestTemplate();
User x = rt.getForObject("/dispatcher/rest/hello",User.class);

The reason I ask this is because if I prefix the uri with localhost:8080/, when the application is a deployed I have to change all the uri prefixes.


Solution

  • Unless you have a helper method that adds the URI prefix, which I don't see here, the method should throw that error because it doesn't have an absolute URI to use in calling your REST API. I don't know of any way to get around using an absolute URI.

    With regards to deployment, you could use a config table in a database or a config file and read in properties, for instance your URI prefix, based on which environment you are deploying to (DEV, TEST, PROD, etc).