Configuring HTTP Endpoints with Java

以下示例演示了如何使用 Java 配置一个入站网关:

Inbound Gateway Using Java Configuration
@Bean
public HttpRequestHandlingMessagingGateway inbound() {
    HttpRequestHandlingMessagingGateway gateway =
        new HttpRequestHandlingMessagingGateway(true);
    gateway.setRequestMapping(mapping());
    gateway.setRequestPayloadType(String.class);
    gateway.setRequestChannelName("httpRequest");
    return gateway;
}

@Bean
public RequestMapping mapping() {
    RequestMapping requestMapping = new RequestMapping();
    requestMapping.setPathPatterns("/foo");
    requestMapping.setMethods(HttpMethod.POST);
    return requestMapping;
}

以下示例演示了如何使用 Java DSL 配置一个入站网关:

Inbound Gateway Using the Java DSL
@Bean
public IntegrationFlow inbound() {
    return IntegrationFlow.from(Http.inboundGateway("/foo")
            .requestMapping(m -> m.methods(HttpMethod.POST))
            .requestPayloadType(String.class))
        .channel("httpRequest")
        .get();
}

以下示例演示了如何使用 Java 配置一个出站网关:

Outbound Gateway Using Java Configuration
@ServiceActivator(inputChannel = "httpOutRequest")
@Bean
public HttpRequestExecutingMessageHandler outbound() {
    HttpRequestExecutingMessageHandler handler =
        new HttpRequestExecutingMessageHandler("http://localhost:8080/foo");
    handler.setHttpMethod(HttpMethod.POST);
    handler.setExpectedResponseType(String.class);
    return handler;
}

以下示例演示了如何使用 Java DSL 配置一个出站网关:

Outbound Gateway Using the Java DSL
@Bean
public IntegrationFlow outbound() {
    return IntegrationFlow.from("httpOutRequest")
        .handle(Http.outboundGateway("http://localhost:8080/foo")
            .httpMethod(HttpMethod.POST)
            .expectedResponseType(String.class))
        .get();
}