【发布时间】:2021-08-19 10:41:49
【问题描述】:
我想了解读取标头并在我的 IntegrationFlow 层中使用它们的最佳位置。
ServiceController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/integration")
public class ServiceController {
@Autowired
private ServiceGateway gateway;
@GetMapping(value = "info")
public String info() {
return gateway.info();
}
}
ServiceGateway.java
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.MessagingGateway;
@MessagingGateway
public interface ServiceGateway {
@Gateway(requestChannel = "integration.info.gateway.channel")
public String info();
}
ServiceConfig.java
import java.net.URISyntaxException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.http.dsl.Http;
import org.springframework.messaging.MessageHeaders;
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class ServiceConfig {
@Bean
public IntegrationFlow info() throws URISyntaxException {
String uri = "http://localhost:8081/hellos/simpler";
return IntegrationFlows.from("integration.info.gateway.channel")
.handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST).expectedResponseType(String.class)).get();
}
}
我收到了来自消费者的一些 Header 元数据。我想在上面的流程中了解以下方法是否是个好主意:
-
读取 Controller 中的标头,然后传递到我的 IntegrationFlow:为此我不知道如何传递。
-
是否存在将请求标头读取到 IntegrationFlow 层的最佳方法或任何方法?
对于第二种方法,我尝试了下面的代码,但运行时出现错误,因为通道是一种方式,因此停止了流程。
return IntegrationFlows.from("integration.info.gateway.channel").handle((request) -> {
MessageHeaders headers = request.getHeaders();
System.out.println("-----------" + headers);
}).handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST).expectedResponseType(String.class)).get();
我的问题是如何从传入呼叫发送请求参数以携带那些在内部调用另一个休息呼叫的人。这里我想将请求头中的数据转换成新的json体,然后发送到http://localhost:8081/hellos/simpler URL。
流程:
我正在尝试在发送到内部 REST POST 调用之前构造这个 RequestBody:
【问题讨论】: