0

我是 Spring Integration 的新手,我正在尝试使用 HttpRequestExecutingMessageHandler 和 HttpRequestHandlingMessagingGateway。处理程序正在发送一个 POST 请求并期待回复。网关正在使用该请求。它工作正常,但处理程序没有得到回复。我不知道如何设置我的流程,我可以直接发回回复并继续我的流程。

@Bean
public HttpRequestExecutingMessageHandler httpOutbound() {
  HttpRequestExecutingMessageHandler handler = Http.outboundGateway(restUrl)
        .httpMethod(HttpMethod.POST)
        .messageConverters(new MappingJackson2HttpMessageConverter())
        .mappedRequestHeaders("Content-Type")
        .get();
  handler.setExpectReply(true);
  handler.setOutputChannel(httpResponseChannel());
  return handler;
}

@Bean
public HttpRequestHandlingMessagingGateway httpRequestGateway() {
  HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
  RequestMapping mapping = new RequestMapping();
  mapping.setMethods(HttpMethod.POST);
  mapping.setPathPatterns(httpRequestHandlingPathPattern);
  gateway.setRequestMapping(mapping);
  gateway.setErrorChannel(errorChannel());
  gateway.setReplyChannel(replyChannel());
  gateway.setRequestChannel(requestChannel());
  gateway.setRequestPayloadTypeClass(DocumentConverterInput.class);
  return gateway;
 }

@Bean
public IntegrationFlow documentConverterFlow() {
  return IntegrationFlows
        .from(requestChannel())
        .publishSubscribeChannel(publishSubscribeSpec ->
              publishSubscribeSpec.subscribe(flow -> flow
                          .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header("http_statusCode", HttpStatus.OK))
                          .channel(replyChannel())))
        .enrichHeaders(headerEnricherSpec ->
             headerEnricherSpec.headerExpression(Constants.DOCUMENT_CONVERTER_INPUT, "payload"))
        .handle(Jms.outboundAdapter(jmsTemplate(connectionFactory)))
        .get();
 }

我的 HttpRequestExecutingMessageHandler 已成功发布请求。HttpRequestHandlingMessagingGateway 已成功使用它。起初我有一个错误“超时内没有收到回复”,所以我添加了一个publishSubscriberChannel。我不知道这是否是正确的方法,但我找不到显示如何正确回复的工作示例。

我上面的代码正在运行,但没有将回复发送回 HttpRequestExecutingMessageHandler!

我的目标是收到请求消息并直接发回 200 OK。之后我想继续我的集成流程,做一些事情并将结果发送到队列。

有什么建议么?

4

1 回答 1

1

对于只是200 OK响应,您需要考虑HttpRequestHandlingMessagingGateway使用expectReply = false. 这样,它将作为入站通道适配器工作,但事实上 HTTP 始终是请求-响应,它只会这样做setStatusCodeIfNeeded(response, httpEntity);,而您HttpRequestExecutingMessageHandler在客户端将得到一个空的但OK响应。

不知道为什么你channel(replyChannel())不能按预期工作。可能是您将请求返回palyoad到最终成为 HTTP 响应的回复消息中,但不知何故它在那里失败了,可能是在转换期间......

更新

这是一个简单的 Spring Boot 应用程序,演示了一个回复和处理场景:https ://github.com/artembilan/sandbox/tree/master/http-reply-and-process

于 2019-09-27T14:34:58.813 回答