Dnyaneshwar Jadhav
Dnyaneshwar Jadhav

Reputation: 353

I am trying to get Header info from Request Controller and read into IntegrationFlow

I wanted to understand where is best location to read headers and use them inside my IntegrationFlow layer.

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();
    }
}

From Consumer I am receiving some Header meta data. I want to know in above flow whether it is good idea from following approaches:

  1. Read headers in Controller and then pass through into my IntegrationFlow: For this I am not aware how to pass through.

  2. Is there best or any way exist to read request headers into IntegrationFlow layer?

For this second approach I have tried below code but runtime I am getting error as channel is one way and hence stopping the flow.

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();

My problem is how to send request parameters from incoming call to carry those internally invoking another rest call. Here I wanted to transform the data from request headers and construct into new json body and then send this to http://localhost:8081/hellos/simpler URL.

The flow:

enter image description here

I am trying to construct this RequestBody before sending to internal REST POST call:

enter image description here

Upvotes: 0

Views: 513

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

A gateway method with no paylaod is for receiving data, not requesting it.

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

Add a @Header annotated parameter to the gateway.

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);

}

This will send a message with an empty string as the payload with the header set.

Upvotes: 1

Related Questions