【问题标题】:Spring Integration - http inboundSpring 集成 - http 入站
【发布时间】:2022-01-14 14:28:40
【问题描述】:

我试图做一个 http 入站,但是当我打印我的消息时,有效负载是空的,我在这里缺少什么?

@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 类:

@Data
@NoArgsConstructor
public class PayloadObj {

    String name;
    String job;
}

编辑 1:根据要求

邮递员的卷发

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

从处理程序打印:

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}

【问题讨论】:

  • 请与我们分享具体的请求。它是否具有所需的Content-Type = application/json 标头?你的意思是你真的得到了你的PayloadObj 实例,但是它的namejob 是空的?
  • 完成,看起来payloadObj inst 正在进入处理程序

标签: spring spring-integration


【解决方案1】:

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

您提出GET 请求。这样请求的主体就被忽略了。 并且只有请求参数作为有效负载传输。由于您也没有这些,这意味着有效负载为空Map

考虑改用POSTPUT。 并详细了解 HTTP 协议及其方法细节。

【讨论】:

  • 我知道我错过了一些愚蠢的事情,天哪。谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
相关资源
最近更新 更多