liveBook · Manning

API gateway

As explained in Spring in Action, Sixth Edition

A gateway interface is an interface whose methods provide an application-facing entry point for sending messages into an integration flow.

An API gateway is an application-facing interface for sending messages into an integration flow. In Spring Integration, it is declared as a Java interface; the framework generates its implementation at runtime, so application code calls methods instead of constructing messages directly. section 10.1 section 10.2.7

A gateway controls how method calls become messages:

  • Route requests through a named message channel with @MessagingGateway(defaultRequestChannel="textInChannel"). section 10.1
  • Carry ordinary method arguments in the message payload, while @Header places selected arguments in message headers such as FileHeaders.FILENAME. section 10.1
  • Choose one-way behavior with a void method or two-way behavior by returning a value from a reply channel. section 10.2.7

Writing text to a file

A FileWriterGateway accepts a filename and text. The filename becomes a file-name header, the text becomes the payload, and the request enters textInChannel for the rest of the flow to process. A two-way UpperCaseGateway instead sends a String through inChannel and returns the transformed result from outChannel. section 10.1 section 10.2.7

Listing 10.1 Message gateway interface to transform method invocations into messages
package sia6;
 
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.file.FileHeaders;
import org.springframework.messaging.handler.annotation.Header;
 
@MessagingGateway(defaultRequestChannel="textInChannel")   #1
public interface FileWriterGateway {
 
  void writeToFile(
      @Header(FileHeaders.FILENAME) String filename,       #2
      String data);
 
}

Use the complete gateway listing to see @MessagingGateway, the default request channel, and the header annotation together. section 10.1

package sia6;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.stereotype.Component;
 
@Component
@MessagingGateway(defaultRequestChannel="inChannel",
                  defaultReplyChannel="outChannel")
public interface UpperCaseGateway {
  String uppercase(String in);
}

Compare a two-way gateway declaration that specifies both request and reply channels. section 10.2.7

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains interface generation, channels, payloads, headers, and one-way or two-way calls through concrete Java examples.
Examples
Uses FileWriterGateway for file output and UpperCaseGateway with a Java DSL transformer for returned uppercase text.
What it assumes
Assumes Java interfaces and introduces Spring Integration annotations, message channels, payloads, and headers through examples.
What you can do after
Build a Spring Integration interface that sends payloads and headers into named channels and optionally returns a flow response.
How it connects
Connects application methods with message channels, integration flows, transformers, and channel adapters.

Also explained in

Compare how 6 books explain API gateway →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.