You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Integration中用Java Config或Spring DSL配置http inbound channel adapter

等效配置实现方案

下面分别提供纯Java Config和Spring Integration DSL两种实现方式,所有配置和你给出的XML功能完全一致:

1. 纯Java Config实现

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.integration.http.inbound.HttpRequestHandlingMessagingGateway;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
import org.springframework.messaging.MessageChannel;

import javax.annotation.Resource;
import com.yourpackage.model.Ticket; // 替换为实际Ticket类的全路径

@Configuration
public class HttpInboundConfig {

    @Resource
    private DefaultHttpHeaderMapper headerMapper;
    @Resource(name = "api_app_integration_request_channel")
    private MessageChannel requestChannel;
    @Resource(name = "internal-client-rest-ticket-error-channel")
    private MessageChannel errorChannel;

    @Bean
    public HttpRequestHandlingMessagingGateway ticketInboundGateway() {
        HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
        // 对应path属性
        gateway.setPathPatterns("/process/ticket");
        // 对应supported-methods属性
        gateway.setSupportedMethods(HttpMethod.PUT);
        // 对应request-payload-type属性
        gateway.setRequestPayloadType(Ticket.class);
        // 对应header-mapper属性
        gateway.setHeaderMapper(headerMapper);
        // 对应channel属性
        gateway.setRequestChannel(requestChannel);
        // 对应error-channel属性
        gateway.setErrorChannel(errorChannel);
        // 对应request-mapping的consumes属性
        gateway.setConsumes(MediaType.APPLICATION_JSON_VALUE);
        return gateway;
    }
}

2. Spring Integration DSL实现

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.http.dsl.Http;
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;

import com.yourpackage.model.Ticket; // 替换为实际Ticket类的全路径

@Configuration
public class HttpInboundDslConfig {

    @Bean
    public IntegrationFlow ticketInboundFlow(DefaultHttpHeaderMapper headerMapper) {
        return IntegrationFlow
                .from(Http.inboundGateway("/process/ticket")
                        .requestMapping(r -> r
                                .methods(HttpMethod.PUT)
                                .consumes(MediaType.APPLICATION_JSON_VALUE)
                        )
                        .requestPayloadType(Ticket.class)
                        .headerMapper(headerMapper)
                        .errorChannel("internal-client-rest-ticket-error-channel")
                )
                .channel("api_app_integration_request_channel")
                .get();
    }
}

注意事项

  • 代码中的com.yourpackage.model.Ticket需要替换为你项目中Ticket类的实际全限定名
  • 确保headerMapper、api_app_integration_request_channel、internal-client-rest-ticket-error-channel对应的Bean已经在Spring上下文中正确注册

内容的提问来源于stack exchange,提问作者JBStonehenge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:36:03