简体   繁体   English

Rest服务中的Spring RestTemplate postForObject调用

[英]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. 我目前正在使用Rest-Service,它需要调用同一服务器上提供的另一个Rest-Method。 So for example the method on path /myPath/foo needs to request something from the method at path /myPath/restApi/bar 因此,例如,路径/ myPath / foo中的方法需要向路径/ 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). 我该如何使用Spring RestTemplate(或其他方法)而不在RestTemplate.postForObject(...)方法中使用完整路径(无法使用完整路径,因为没有工件的url和端口)在构建工件时已知)。

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? 首先,为什么要使用URL而不是直接在第二个URL后面调用service方法来访问同一服务器(看起来是同一应用程序)上的REST方法? If this is the same servlet container but multiple WAR files than fine. 如果这是相同的servlet容器,但多个WAR文件则不理想。

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 . 您说的是,当客户端尝试访问/myPath/foo ,服务器应访问/myPath/restApi/bar When you are in a controller/action/handler method/whatever your REST framework provides obtain HttpServletRequest somehow. 当您处于控制器/动作/处理程序方法/您的REST框架提供的任何内容中时,都会以某种方式获取HttpServletRequest Having request object you can find out which host and port your client used to connect to your first web service. 具有请求对象后,您可以找出客户端用于连接到第一个Web服务的主机和端口。 You can use that information to call the second web service on the same server. 您可以使用该信息在同一服务器上调用第二个Web服务。

Second thought: if this is the same server, can't you just use localhost ? 再想一想:如果这是同一台服务器,就不能只使用localhost吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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