Spring 将 Http 与 Spring 引导和@RequestMapping 集成

Spring Integration Http with Spring Boot and @RequestMapping

我尝试使用 Spring 集成 HTTP -> inboundGateway 开发 SpringBoot Rest 服务器。

我有一个控制器,用“@Controller”和“@RequestMapping”注释并尝试创建以下流程:

GET 请求“/”-> 通道:httpRequestChannel -> 运行 IndexController -> 通道:httpReplyChannel -> 返回浏览器

但是没用。

我的积分Xml:

<int:channel id="httpRequestChannel">
  <int:interceptors>
    <int:wire-tap channel="logHttpRequestChannel" />
  </int:interceptors>
</int:channel>

<int:channel id="httpReplyChannel">
  <int:interceptors>
    <int:wire-tap channel="logHttpReplyChannel" />
  </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="logHttpRequestChannel" level="INFO" logger-name="httpRequestChannel" log-full-message="true" />

<int:logging-channel-adapter id="logHttpReplyChannel" level="INFO" logger-name="httpReplyChannel" log-full-message="true" />

<int-http:inbound-gateway id="inboundGateway"
    request-channel="httpRequestChannel" reply-channel="httpReplyChannel"
    auto-startup="true" supported-methods="GET" path="/">
    <int-http:request-mapping produces="application/json" />
</int-http:inbound-gateway>

错误是:

Dispatcher has no subscribers

但在我看来,控制器应该是通过 RequestMapping 注释的订阅者...

我上传了一个示例github项目:https://github.com/marcelalburg/spring-boot-integration-rest-server

谢谢你的帮助 马塞尔

更新

你好,

我在文档中看到了一些东西:

The parsing of the HTTP Inbound Gateway or the HTTP Inbound Channel Adapter registers an integrationRequestMappingHandlerMapping bean of type IntegrationRequestMappingHandlerMapping, in case there is none registered, yet. This particular implementation of the HandlerMapping delegates its logic to the RequestMappingInfoHandlerMapping. The implementation provides similar functionality as the one provided by the org.springframework.web.bind.annotation.RequestMapping annotation in Spring MVC.

所以,我更改了以下内容:

    <int-http:inbound-gateway id="indexGateway"
    request-channel="httpRequestChannel" reply-channel="httpReplyChannel"
    auto-startup="true" supported-methods="GET" path="/, /test" reply-timeout="100" />

和我的控制器

@ServiceActivator( inputChannel = "httpRequestChannel", outputChannel = "httpReplyChannel" )
@RequestMapping( value = "/", method = RequestMethod.GET, produces = "application/json" )
public String testGateway( LinkedMultiValueMap payload, @Headers Map<String, Object> headerMap )
{
    // IntegrationRequestMappingHandlerMapping

    System.out.println( "Starting process the message [reciveing]" );

    return "{HelloMessage: \"Hello\"}";
}

@ServiceActivator( inputChannel = "httpRequestChannel", outputChannel = "httpReplyChannel" )
@RequestMapping( value = "/test", method = RequestMethod.GET, produces = "application/json" )
public String testGateway2( LinkedMultiValueMap payload, @Headers Map<String, Object> headerMap )
{
    // IntegrationRequestMappingHandlerMapping

    System.out.println( "Starting process the message [reciveing]" );

    return "{HelloMessage: \"Test\"}";
}

现在,我收到了回复,但它 returns 随机化了 "Test" 和 "Hello" ...

谢谢

否;你似乎有一个基本的误解。

通过 Spring 集成,入站网关 替换 @Controller,并将入站(可能已转换)对象作为消息的有效负载发送到 requestChannel.

一些其他组件(不是控制器)订阅该频道以接收消息。

因此,您可以将 POJO 配置为 <service-activator input-channel="httpRequestChannel" .../> 或将方法注释为 @ServiceActivator.

,而不是配置 @Controller

然后它将使用该消息,并且可以选择将回复发送到输出通道(省略输出通道将导致它被路由回网关)。

请参阅 http sample 示例。