如何使用 Spring 集成将请求方法从 POST 更改为 GET 并从正文请求参数中提取?

问题描述 投票:0回答:1

我有一个正在运行的 api,它需要 prop 作为参数。我想解决的任务是使用 spring 集成创建一个代理,该代理接受带有 body 的 post 请求。例如:

POST http://localhost:8080/proxy
    {
       "prop":"someval"
    }

然后将请求方法更改为 GET,提取 prop 值,将其设置为请求参数并将其发送到下游 api

http://localhost:9999/greet/param?prop=someval

从 api 获取响应并将其发送回客户端。

这是已完成的工作:

@Bean
public IntegrationFlow yourIntegrationFlow2() {
    return IntegrationFlow.from(WebFlux.inboundGateway("/proxy")
                    .requestMapping(mapping -> mapping.methods(HttpMethod.GET, HttpMethod.POST))
                    .requestPayloadType(Response.class)
            )
            .handle((GenericHandler<Response>) (payload, headers) -> {
                System.out.println(payload.getProp());
                return MessageBuilder.withPayload("").setHeader("queryParam",payload.getProp());
            })
            .handle(m->WebFlux.outboundGateway("http://localhost:9999/greet/param?{query}")
                    .uriVariable("query","prop="+m.getHeaders().get("queryParam"))
                    .httpMethod(HttpMethod.GET).charset("utf-8")
                    .mappedRequestHeaders("**")
                    .expectedResponseType(String.class))
            .get();
}

@Data
public class Response {
    private String prop;
}

问题是当我使用 .http 文件向代理服务发送请求时,它挂起

POST http://localhost:8080/proxy
Content-Type: application/json

{
  "prop": "val"
} 
spring-integration spring-webflux
1个回答
0
投票

语法

.handle(m->WebFlux
是错误的。您不能使用 lambda 变体,因为您通过
WebFlux.outboundGateway()
提供了整个处理程序。 另外,普通的
handle(m ->)
MessageHandler.handleMessage()
的 lambda,它返回
void
,因此不会产生任何回复,正如入站网关在流程开始时所期望的那样。

修复方法如下:

.handle(WebFlux.outboundGateway("http://localhost:9999/greet/param?{query}")
© www.soinside.com 2019 - 2024. All rights reserved.