简体   繁体   中英

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

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM