Search code examples
springspring-integration

Spring Integration - http inbound


im trying to make a http inbound but when i print my message the payload its empty, what am i missing here?

@Slf4j
@Component
public class HttpInboundGateway {

  @Bean
  public IntegrationFlow inGate1() {
      log.info("Initializing inbound gateway...");
      return IntegrationFlows.from(Http.inboundChannelAdapter("/")
            .requestPayloadType(PayloadObj.class))
            .channel("transfer_next_channel")
            .get();
  }

  @Bean
  @ServiceActivator(inputChannel = "transfer_next_channel")
  public MessageHandler handler() {
      return new MessageHandler() {
        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            System.out.println("myHandlerPayload: " + message.getPayload());
            System.out.println("myHandlerHeader: " + message.getHeaders());
        }
    };
  }
}

PayloadObj class expected:

@Data
@NoArgsConstructor
public class PayloadObj {

    String name;
    String job;
}

EDIT 1: As requested

Curl from Postman

curl --location --request GET 'http://localhost:9090/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name":"Marcos",
    "job":"Dev Jr"
}'

Print from handler:

myHandlerPayload: {} --- Payload
myHandlerHeader: {content-length=46, http_requestMethod=GET, 
host=localhost:9090, http_requestUrl=http://localhost:9090/, 
connection=keep-alive, id=e67aef3d-938a-7ac3-eb7d-ddaf9cd74f4e, 
contentType=application/json;charset=UTF-8, accept-encoding=gzip, 
deflate, br, user-agent=PostmanRuntime/7.28.4, accept=*/*, 
timestamp=1642168826837}

Solution

  • curl --location --request GET 'http://localhost:9090/'

    You do GET request. This way the body of the request is ignored. And only request params are transferred as payload. Since you don't have those as well, that means the payload is empty Map.

    Consider to use POST or PUT instead. And learn more about an HTTP protocol and its method specifics.