【问题标题】:Kotlin/Spring - Fowarding post request from one service to anotherKotlin/Spring - 将帖子请求从一项服务转发到另一项服务
【发布时间】:2019-12-06 08:12:31
【问题描述】:

我们有一个用于处理所有前端和后端逻辑的 Spring Web 应用程序。由于我们需要扩展,我们将其拆分为两个微服务。我将如何将发布请求“转发”到另一个 url(包括其正文和身份验证标头)。例如:

microservice1 有一个端点 /api/doSomething

microservice2 有一个端点 /privateUrl/doSomething

我希望用户点击microservice1 上的端点,该端点将发布到microservice2 并返回结果。

我尝试使用 RestTemplate 但运气不佳,我不断收到来自微服务 1 的错误“无法编写 JSON:找不到类的序列化程序...”,我怀疑这是因为微服务 1 不知道如何解析微服务 2 需要的主体对象.

微服务1:

@PostMapping("/api/DoSomething")
fun postIT(request: HttpServletRequest, @PathVariable one: String, @PathVariable two: String){ ...}

微服务2:

@CrossOrigin
@PostMapping("/privateUrl/doSomething")
fun postIT(request: HttpServletRequest, @RequestParam one: String, @RequestParam(required = false, defaultValue = "true") two: String,@RequestBody it: IT) { ... }

我知道我可以解析 microservice1 中的整个请求,然后将其发送到 microservice2,但是有没有办法将 http 请求“转发”到新的 url?

【问题讨论】:

  • 重定向有效吗?你可以从你的@PostMapping返回一个RedirectView

标签: spring-boot http kotlin


【解决方案1】:

我不确定你的意思,但是如果你想从一台服务器到另一台服务器进行通信,你总是可以使用 rest 模板(或任何其他模板)并将 http 请求从一个服务发送到另一个服务,你可以在此处查看示例 https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm https://spring.io/guides/tutorials/bookmarks/

看看它应该适合你。

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2012-04-23
    • 2012-12-27
    • 1970-01-01
    • 2022-12-10
    • 2011-09-03
    相关资源
    最近更新 更多