andy
andy

Reputation: 1882

Spring RestTemplate postForObject call within Rest service

I'm currently working on a Rest-Service that needs to call another Rest-Method provided on the same server. So for example the method on path /myPath/foo needs to request something from the method at path /myPath/restApi/bar

How can I do that with the Spring RestTemplate (or something else) without using a full path in the RestTemplate.postForObject(...) method (full path can't be used, because the url and port of the artifact aren't known when building the artifact).

Thanks for your help! Best regards, Andy

Upvotes: 1

Views: 2734

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

First of all why are you accessing a REST method on the same server (it looks like it is the same application) using URL instead of calling the service method behind that second URL directly? If this is the same servlet container but multiple WAR files than fine.

Back to your question: you do have the host and port. You are saying that when a client tries to access /myPath/foo then the server should access /myPath/restApi/bar. When you are in a controller/action/handler method/whatever your REST framework provides obtain HttpServletRequest somehow. Having request object you can find out which host and port your client used to connect to your first web service. You can use that information to call the second web service on the same server.

Second thought: if this is the same server, can't you just use localhost?

Upvotes: 1

Related Questions