【问题标题】:I am trying to get Header info from Request Controller and read into IntegrationFlow我正在尝试从请求控制器获取标头信息并读入 IntegrationFlow
【发布时间】: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 元数据。我想在上面的流程中了解以下方法是否是个好主意:

  1. 读取 Controller 中的标头,然后传递到我的 IntegrationFlow:为此我不知道如何传递。

  2. 是否存在将请求标头读取到 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:

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    没有paylaod的网关方法是用于接收数据,而不是请求它。

    https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-calling-no-argument-methods

    给网关添加@Header注解参数。

    https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#gateway-configuration-annotations

    @MessagingGateway
    public interface ServiceGateway {
        
        @Gateway(requestChannel = "integration.info.gateway.channel")
        public String info("", @Header("x-api") String xApi);
    
    }
    

    这将发送带有空字符串的消息作为带有标头集的有效负载。

    【讨论】:

    • 嘿@Gary 感谢您的帮助,但我的下一个问题是如何在 IntegrationFlow 方法中获取这些 Header 属性,是隐式可用还是如何获取?
    • 我应该如何从 Controller 传递这个 Header 值?
    • 参见baeldung.com/spring-rest-http-headers 添加@RequestHeader 参数以获取标题。在网关参数中传递您想要的标头。您已经有代码可以访问处理程序中的消息头。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 2020-07-13
    相关资源
    最近更新 更多