如何在 Spring Boot 中将 POST 请求从一个 Web 应用程序正确转发到另一个 Web 应用程序?

Alf*_*oon 6 java model-view-controller spring http spring-boot

让我们考虑一下我有两个 Web 应用程序 A 和 B 的情况

初始数据来到 A,在那里我有下一个控制器:

@RestController
public class restController {
@RequestMapping(path = "/testA", method = RequestMethod.POST)
public final void test(*inputdata*) { "redirect POST to B app" }
}
Run Code Online (Sandbox Code Playgroud)

此数据必须发送到应用程序 B:

@RestController
public class restController {
@RequestMapping(path = "/testB", method = RequestMethod.POST)
public final void test(*inputdata*) { 'some logic' }
}
Run Code Online (Sandbox Code Playgroud)

并且必须将逻辑结果发送回 A 应用程序。

通信必须以 RESTful 格式进行。

据我通过“谷歌搜索”发现,Spring 无法做到这一点,我必须创建自定义的“POST”方法,是真的吗?因为这个链接http://www.baeldung.com/spring-redirect-and-forward包含有关“重定向 HTTP POST 请求”的信息,但我无法理解他们使用它的方式。

谢谢!

Meh*_*lik 1

您可以使用RestTemplateFeign Client

RestTemplate 帖子示例:

Foo foo = restTemplate.postForObject("/testB", request, Foo.class);
Run Code Online (Sandbox Code Playgroud)

但是,当您使用微服务时,您应该使用 Feign 客户端进行微服务通信。


编辑

Feign 比 RestTemplate 提供了许多额外的好处。

示例:Feign 客户端提供开箱即用的负载平衡。

你可以在这里阅读更多