Fluent API for configuring HTTP integration components using Java DSL with builders and specifications. The Java DSL provides a concise and type-safe way to define inbound and outbound HTTP endpoints, eliminating the need for verbose bean configuration.
DSL Factory:
Http class provides static factory methodsBuilder Pattern:
this for chainingIntegrationFlow.get() is calledDefault Behaviors:
expectReply=true for gateways, false for adaptersRequest Mapping:
Consumer<RequestMappingSpec> lambdaRequestMappingSpec for configurationCORS Configuration:
Consumer<CrossOriginSpec> lambdaCrossOriginSpec for configurationExpression Support:
Type Safety:
ParameterizedTypeReferenceIntegration with Flows:
IntegrationFlow.from(), .handle(), .enrich() methodsEdge Cases:
The main entry point for creating HTTP integration components using Java DSL. This factory class provides static methods for building inbound and outbound HTTP endpoints with fluent configuration.
public final class Http {
/**
* Creates one-way outbound channel adapter for URI.
* Fire-and-forget pattern without expecting reply.
*
* @param uri the target URI
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundChannelAdapter(URI uri);
/**
* Creates one-way outbound channel adapter for String URI.
*
* @param uri the target URI as string
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundChannelAdapter(String uri);
/**
* Creates one-way outbound channel adapter with Function to evaluate URI at runtime.
* Function is evaluated for each message.
*
* @param uriFunction function to generate URI from message
* @param <P> the message payload type
* @return spec builder for configuration
*/
public static <P> HttpMessageHandlerSpec outboundChannelAdapter(
Function<Message<P>, ?> uriFunction);
/**
* Creates one-way outbound channel adapter with SpEL expression for URI.
*
* @param uriExpression SpEL expression to evaluate URI
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundChannelAdapter(
Expression uriExpression);
/**
* Creates one-way outbound channel adapter with URI and custom RestTemplate.
*
* @param uri the target URI
* @param restTemplate custom RestTemplate instance
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundChannelAdapter(
URI uri, RestTemplate restTemplate);
/**
* Creates one-way outbound channel adapter with String URI and custom RestTemplate.
*
* @param uri the target URI as string
* @param restTemplate custom RestTemplate instance
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundChannelAdapter(
String uri, RestTemplate restTemplate);
/**
* Creates one-way outbound channel adapter with Function and custom RestTemplate.
*
* @param uriFunction function to generate URI from message
* @param restTemplate custom RestTemplate instance
* @param <P> the message payload type
* @return spec builder for configuration
*/
public static <P> HttpMessageHandlerSpec outboundChannelAdapter(
Function<Message<P>, ?> uriFunction, RestTemplate restTemplate);
/**
* Creates one-way outbound channel adapter with expression and custom RestTemplate.
*
* @param uriExpression SpEL expression to evaluate URI
* @param restTemplate custom RestTemplate instance
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundChannelAdapter(
Expression uriExpression, RestTemplate restTemplate);
/**
* Creates request-reply outbound gateway for URI.
* Expects response from HTTP service.
*
* @param uri the target URI
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundGateway(URI uri);
/**
* Creates request-reply outbound gateway for String URI.
*
* @param uri the target URI as string
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundGateway(String uri);
/**
* Creates request-reply outbound gateway with Function to evaluate URI at runtime.
*
* @param uriFunction function to generate URI from message
* @param <P> the message payload type
* @return spec builder for configuration
*/
public static <P> HttpMessageHandlerSpec outboundGateway(
Function<Message<P>, ?> uriFunction);
/**
* Creates request-reply outbound gateway with SpEL expression for URI.
*
* @param uriExpression SpEL expression to evaluate URI
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundGateway(Expression uriExpression);
/**
* Creates request-reply outbound gateway with URI and custom RestTemplate.
*
* @param uri the target URI
* @param restTemplate custom RestTemplate instance
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundGateway(
URI uri, RestTemplate restTemplate);
/**
* Creates request-reply outbound gateway with String URI and custom RestTemplate.
*
* @param uri the target URI as string
* @param restTemplate custom RestTemplate instance
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundGateway(
String uri, RestTemplate restTemplate);
/**
* Creates request-reply outbound gateway with Function and custom RestTemplate.
*
* @param uriFunction function to generate URI from message
* @param restTemplate custom RestTemplate instance
* @param <P> the message payload type
* @return spec builder for configuration
*/
public static <P> HttpMessageHandlerSpec outboundGateway(
Function<Message<P>, ?> uriFunction, RestTemplate restTemplate);
/**
* Creates request-reply outbound gateway with expression and custom RestTemplate.
*
* @param uriExpression SpEL expression to evaluate URI
* @param restTemplate custom RestTemplate instance
* @return spec builder for configuration
*/
public static HttpMessageHandlerSpec outboundGateway(
Expression uriExpression, RestTemplate restTemplate);
/**
* Creates one-way inbound controller adapter with view name.
* Returns specified view for rendering without expecting reply.
*
* @param viewName the MVC view name to render
* @param path URL path patterns for mapping
* @return spec builder for configuration
*/
public static HttpControllerEndpointSpec inboundControllerAdapter(
String viewName, String... path);
/**
* Creates one-way inbound controller adapter with view expression.
* Expression evaluates view name dynamically.
*
* @param viewExpression SpEL expression for view name
* @param path URL path patterns for mapping
* @return spec builder for configuration
*/
public static HttpControllerEndpointSpec inboundControllerAdapter(
Expression viewExpression, String... path);
/**
* Creates request-reply inbound controller gateway with view name.
* Processes message and returns specified view for rendering.
*
* @param viewName the MVC view name to render
* @param path URL path patterns for mapping
* @return spec builder for configuration
*/
public static HttpControllerEndpointSpec inboundControllerGateway(
String viewName, String... path);
/**
* Creates request-reply inbound controller gateway with view expression.
* Expression evaluates view name dynamically.
*
* @param viewExpression SpEL expression for view name
* @param path URL path patterns for mapping
* @return spec builder for configuration
*/
public static HttpControllerEndpointSpec inboundControllerGateway(
Expression viewExpression, String... path);
/**
* Creates one-way inbound channel adapter for path mappings.
* Receives HTTP requests without sending replies.
*
* @param path URL path patterns for mapping
* @return spec builder for configuration
*/
public static HttpRequestHandlerEndpointSpec inboundChannelAdapter(
String... path);
/**
* Creates request-reply inbound gateway for path mappings.
* Receives HTTP requests and sends replies.
*
* @param path URL path patterns for mapping
* @return spec builder for configuration
*/
public static HttpRequestHandlerEndpointSpec inboundGateway(String... path);
}Usage Example - Outbound Gateway:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.http.dsl.Http;
import org.springframework.http.HttpMethod;
@Configuration
public class OutboundDslConfig {
@Bean
public IntegrationFlow httpOutboundFlow() {
return IntegrationFlow
.from("requestChannel")
.handle(Http.outboundGateway("https://api.example.com/data/{id}")
.httpMethod(HttpMethod.GET)
.uriVariable("id", "payload.customerId")
.expectedResponseType(String.class)
.headerMapper(headerMapper()))
.channel("responseChannel")
.get();
}
}Usage Example - Inbound Gateway:
@Bean
public IntegrationFlow httpInboundFlow() {
return IntegrationFlow
.from(Http.inboundGateway("/api/orders")
.requestMapping(m -> m
.methods(HttpMethod.POST)
.consumes("application/json"))
.requestPayloadType(Order.class)
.headerMapper(headerMapper()))
.handle("orderService", "processOrder")
.get();
}Specification builder for HttpRequestExecutingMessageHandler in Java DSL. Used for configuring outbound HTTP adapters and gateways.
public class HttpMessageHandlerSpec
extends BaseHttpMessageHandlerSpec<HttpMessageHandlerSpec>
implements ComponentsRegistration {
/**
* Sets ClientHttpRequestFactory for RestTemplate.
* Allows customization of HTTP client behavior (timeouts, pooling, etc.).
*
* @param requestFactory the request factory
* @return the spec for method chaining
*/
public HttpMessageHandlerSpec requestFactory(
ClientHttpRequestFactory requestFactory);
/**
* Sets ResponseErrorHandler for RestTemplate.
* Customizes error handling for HTTP responses.
*
* @param errorHandler the error handler
* @return the spec for method chaining
*/
public HttpMessageHandlerSpec errorHandler(ResponseErrorHandler errorHandler);
/**
* Sets list of HttpMessageConverters for RestTemplate.
* Replaces default converters for request/response conversion.
*
* @param messageConverters the message converters
* @return the spec for method chaining
*/
public HttpMessageHandlerSpec messageConverters(
HttpMessageConverter<?>... messageConverters);
}Usage Example:
@Bean
public IntegrationFlow customRestTemplateFlow() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(10000);
return IntegrationFlow
.from("httpRequestChannel")
.handle(Http.outboundGateway("https://api.example.com/process")
.httpMethod(HttpMethod.POST)
.requestFactory(factory)
.messageConverters(
new MappingJackson2HttpMessageConverter(),
new StringHttpMessageConverter())
.expectedResponseType(ProcessingResult.class))
.get();
}Base specification for HTTP message handler builders in Java DSL. Provides common configuration methods for outbound HTTP communication.
public abstract class BaseHttpMessageHandlerSpec<S extends BaseHttpMessageHandlerSpec<S>>
extends MessageHandlerSpec<S, AbstractHttpRequestExecutingMessageHandler>
implements ComponentsRegistration {
/**
* Sets URI encoding mode for template processing.
*
* @param encodingMode the encoding mode
* @return the spec for method chaining
*/
public S encodingMode(DefaultUriBuilderFactory.EncodingMode encodingMode);
/**
* Sets SpEL expression to determine HttpMethod dynamically.
* Expression is evaluated against each message.
*
* @param httpMethodExpression the HTTP method expression
* @return the spec for method chaining
*/
public S httpMethodExpression(Expression httpMethodExpression);
/**
* Sets Function to determine HttpMethod dynamically.
*
* @param httpMethodFunction function returning HttpMethod
* @param <P> the message payload type
* @return the spec for method chaining
*/
public <P> S httpMethodFunction(Function<Message<P>, ?> httpMethodFunction);
/**
* Sets the HttpMethod for requests.
* Default: POST.
*
* @param httpMethod the HTTP method
* @return the spec for method chaining
*/
public S httpMethod(HttpMethod httpMethod);
/**
* Controls if payload should be extracted when preparing request body.
* When false, entire Message is used.
* Default: true.
*
* @param extractPayload true to extract payload
* @return the spec for method chaining
*/
public S extractPayload(boolean extractPayload);
/**
* Sets charset for converting String payloads to bytes.
* Default: UTF-8.
*
* @param charset the charset name
* @return the spec for method chaining
*/
public S charset(String charset);
/**
* Sets expected response type for conversion.
*
* @param expectedResponseType the response type class
* @return the spec for method chaining
*/
public S expectedResponseType(Class<?> expectedResponseType);
/**
* Sets expected response type with generics support.
* Use for generic types like List<User>.
*
* @param expectedResponseType parameterized type reference
* @return the spec for method chaining
*/
public S expectedResponseType(
ParameterizedTypeReference<?> expectedResponseType);
/**
* Sets expression to determine response type dynamically.
* Expression is evaluated against each message.
*
* @param expectedResponseTypeExpression the response type expression
* @return the spec for method chaining
*/
public S expectedResponseTypeExpression(
Expression expectedResponseTypeExpression);
/**
* Sets Function to determine response type dynamically.
*
* @param expectedResponseTypeFunction function returning response type
* @param <P> the message payload type
* @return the spec for method chaining
*/
public <P> S expectedResponseTypeFunction(
Function<Message<P>, ?> expectedResponseTypeFunction);
/**
* Sets HeaderMapper for HTTP/Message header mapping.
*
* @param headerMapper the header mapper
* @return the spec for method chaining
*/
public S headerMapper(HeaderMapper<HttpHeaders> headerMapper);
/**
* Sets patterns for request headers to map.
* Supports wildcards (e.g., "X-Custom-*").
*
* @param patterns header name patterns
* @return the spec for method chaining
*/
public S mappedRequestHeaders(String... patterns);
/**
* Sets patterns for response headers to map.
* Supports wildcards (e.g., "X-Custom-*").
*
* @param patterns header name patterns
* @return the spec for method chaining
*/
public S mappedResponseHeaders(String... patterns);
/**
* Sets Map of URI variable expressions.
* Each expression is evaluated against the message.
*
* @param uriVariableExpressions map of variable name to expression
* @return the spec for method chaining
*/
public S uriVariableExpressions(Map<String, Expression> uriVariableExpressions);
/**
* Sets expression for single URI variable.
*
* @param variable the variable name
* @param expression the SpEL expression
* @return the spec for method chaining
*/
public S uriVariable(String variable, Expression expression);
/**
* Sets SpEL string expression for single URI variable.
*
* @param variable the variable name
* @param expression the expression string
* @return the spec for method chaining
*/
public S uriVariable(String variable, String expression);
/**
* Sets Function for single URI variable.
*
* @param variable the variable name
* @param valueFunction function to evaluate variable value
* @param <P> the message payload type
* @return the spec for method chaining
*/
public <P> S uriVariable(
String variable, Function<Message<P>, ?> valueFunction);
/**
* Sets expression to evaluate URI variables Map.
* Expression should return Map<String, ?>.
*
* @param uriVariablesExpression the URI variables expression string
* @return the spec for method chaining
*/
public S uriVariablesExpression(String uriVariablesExpression);
/**
* Sets expression to evaluate URI variables Map.
*
* @param uriVariablesExpression the URI variables expression
* @return the spec for method chaining
*/
public S uriVariablesExpression(Expression uriVariablesExpression);
/**
* Sets Function to evaluate URI variables Map.
*
* @param uriVariablesFunction function returning URI variables map
* @param <P> the message payload type
* @return the spec for method chaining
*/
public <P> S uriVariablesFunction(
Function<Message<P>, Map<String, ?>> uriVariablesFunction);
/**
* Controls if Set-Cookie headers should be transferred as Cookie headers.
* Useful for maintaining sessions across requests.
* Default: false.
*
* @param transferCookies true to transfer cookies
* @return the spec for method chaining
*/
public S transferCookies(boolean transferCookies);
/**
* Controls if ResponseEntity body should be extracted for reply.
* When false, entire ResponseEntity (with status, headers) is returned.
* Default: true.
*
* @param extractResponseBody true to extract body only
* @return the spec for method chaining
*/
public S extractResponseBody(boolean extractResponseBody);
/**
* Returns components to register in application context.
*
* @return collection of components
*/
public Collection<Object> getComponentsToRegister();
}Usage Example:
@Bean
public IntegrationFlow complexOutboundFlow() {
return IntegrationFlow
.from("orderChannel")
.handle(Http.outboundGateway("https://api.example.com/{version}/orders/{id}")
.httpMethod(HttpMethod.PUT)
.uriVariable("version", "headers['api-version']")
.uriVariable("id", "payload.orderId")
.expectedResponseType(OrderConfirmation.class)
.mappedRequestHeaders("Authorization", "X-*")
.mappedResponseHeaders("X-Transaction-Id", "X-Status")
.transferCookies(true)
.charset("UTF-8")
.encodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY))
.get();
}Specification builder for HttpRequestHandlingMessagingGateway in Java DSL. Used for configuring inbound HTTP gateways and channel adapters.
public class HttpRequestHandlerEndpointSpec
extends BaseHttpInboundEndpointSpec<HttpRequestHandlerEndpointSpec> {
/**
* Sets flag to determine if exceptions should be converted and written to response.
* When true, exceptions are converted to HTTP error responses.
* Default: false.
*
* @param convertExceptions true to convert exceptions
* @return the spec for method chaining
*/
public HttpRequestHandlerEndpointSpec convertExceptions(
boolean convertExceptions);
}Usage Example:
@Bean
public IntegrationFlow inboundGatewayFlow() {
return IntegrationFlow
.from(Http.inboundGateway("/api/process")
.requestMapping(m -> m
.methods(HttpMethod.POST)
.consumes("application/json")
.produces("application/json"))
.requestPayloadType(ProcessRequest.class)
.convertExceptions(true)
.headerMapper(headerMapper())
.mappedRequestHeaders("Authorization", "X-Request-Id"))
.handle("processingService", "process")
.get();
}Specification builder for HttpRequestHandlingController in Java DSL. Used for configuring controller-based HTTP endpoints that render views.
public class HttpControllerEndpointSpec
extends BaseHttpInboundEndpointSpec<HttpControllerEndpointSpec> {
/**
* Sets key for reply Message/payload in model map.
* Default: "reply".
*
* @param replyKey the model key
* @return the spec for method chaining
*/
public HttpControllerEndpointSpec replyKey(String replyKey);
/**
* Sets key for Errors in model map.
* Default: "errors".
*
* @param errorsKey the errors key
* @return the spec for method chaining
*/
public HttpControllerEndpointSpec errorsKey(String errorsKey);
/**
* Sets error code for message handling errors.
*
* @param errorCode the error code
* @return the spec for method chaining
*/
public HttpControllerEndpointSpec errorCode(String errorCode);
}Usage Example:
@Bean
public IntegrationFlow controllerFlow() {
return IntegrationFlow
.from(Http.inboundControllerGateway("resultView", "/forms/submit")
.requestMapping(m -> m
.methods(HttpMethod.POST)
.params("action=submit"))
.requestPayloadType(FormData.class)
.replyKey("result")
.errorsKey("validationErrors")
.errorCode("form.processing.error")
.validator(formValidator()))
.handle("formService", "processForm")
.get();
}Base specification for HTTP inbound endpoint builders in Java DSL. Provides common configuration methods for receiving HTTP requests.
public abstract class BaseHttpInboundEndpointSpec<S extends BaseHttpInboundEndpointSpec<S>>
extends HttpInboundEndpointSupportSpec<S> {
/**
* Sets custom message body converters.
* Replaces default converters unless mergeWithDefaultConverters is true.
*
* @param messageConverters the message converters
* @return the spec for method chaining
*/
public S messageConverters(HttpMessageConverter<?>... messageConverters);
/**
* Controls if default converters should be available after custom converters.
* When true, custom converters are tried first, then defaults.
* Default: false.
*
* @param mergeWithDefaultConverters true to merge
* @return the spec for method chaining
*/
public S mergeWithDefaultConverters(boolean mergeWithDefaultConverters);
/**
* Sets the MultipartResolver for handling multipart requests.
* Required for file upload support.
*
* @param multipartResolver the multipart resolver
* @return the spec for method chaining
*/
public S multipartResolver(MultipartResolver multipartResolver);
}Base specification for HTTP inbound endpoint support builders in Java DSL. Provides configuration methods common to all inbound endpoints.
public abstract class HttpInboundEndpointSupportSpec<S extends HttpInboundEndpointSupportSpec<S>>
extends MessagingGatewaySpec<S, BaseHttpInboundEndpoint>
implements ComponentsRegistration {
/**
* Configures RequestMapping via Consumer.
* Consumer receives RequestMappingSpec for configuration.
*
* @param mapping the request mapping configuration consumer
* @return the spec for method chaining
*/
public S requestMapping(Consumer<RequestMappingSpec> mapping);
/**
* Configures CrossOrigin via Consumer.
* Consumer receives CrossOriginSpec for CORS configuration.
*
* @param crossOrigin the cross-origin configuration consumer
* @return the spec for method chaining
*/
public S crossOrigin(Consumer<CrossOriginSpec> crossOrigin);
/**
* Sets SpEL expression to generate Message payload.
* Expression is evaluated against HttpEntity.
*
* @param payloadExpression the payload expression string
* @return the spec for method chaining
*/
public S payloadExpression(String payloadExpression);
/**
* Sets Expression to generate Message payload.
*
* @param payloadExpression the payload expression
* @return the spec for method chaining
*/
public S payloadExpression(Expression payloadExpression);
/**
* Sets Function to generate Message payload.
*
* @param payloadFunction function to evaluate payload from HttpEntity
* @param <P> the HttpEntity body type
* @return the spec for method chaining
*/
public <P> S payloadFunction(Function<HttpEntity<P>, ?> payloadFunction);
/**
* Sets Map of SpEL expressions for headers.
* Expressions are evaluated against HttpEntity.
*
* @param expressions map of header name to expression
* @return the spec for method chaining
*/
public S headerExpressions(Map<String, Expression> expressions);
/**
* Sets SpEL expression for specific header.
*
* @param header the header name
* @param expression the expression string
* @return the spec for method chaining
*/
public S headerExpression(String header, String expression);
/**
* Sets Expression for specific header.
*
* @param header the header name
* @param expression the expression
* @return the spec for method chaining
*/
public S headerExpression(String header, Expression expression);
/**
* Sets Function for specific header.
*
* @param header the header name
* @param headerFunction function to evaluate header value
* @param <P> the HttpEntity body type
* @return the spec for method chaining
*/
public <P> S headerFunction(
String header, Function<HttpEntity<P>, ?> headerFunction);
/**
* Sets HeaderMapper for HTTP/Message header mapping.
*
* @param mapper the header mapper
* @return the spec for method chaining
*/
public S headerMapper(HeaderMapper<HttpHeaders> mapper);
/**
* Sets patterns for request headers to map.
* Supports wildcards (e.g., "X-Custom-*").
*
* @param patterns header name patterns
* @return the spec for method chaining
*/
public S mappedRequestHeaders(String... patterns);
/**
* Sets patterns for response headers to map.
* Supports wildcards (e.g., "X-Custom-*").
*
* @param patterns header name patterns
* @return the spec for method chaining
*/
public S mappedResponseHeaders(String... patterns);
/**
* Sets payload type for inbound request conversion.
*
* @param requestPayloadType the payload type class
* @return the spec for method chaining
*/
public S requestPayloadType(Class<?> requestPayloadType);
/**
* Sets ResolvableType for inbound request payload.
* Use for generic types with type parameters.
*
* @param requestPayloadType the resolvable type
* @return the spec for method chaining
*/
public S requestPayloadType(ResolvableType requestPayloadType);
/**
* Controls if only reply payload should be passed in HTTP response.
* When false, entire Message including headers is used.
* Default: true.
*
* @param extractReplyPayload true to extract only payload
* @return the spec for method chaining
*/
public S extractReplyPayload(boolean extractReplyPayload);
/**
* Sets expression to resolve status code.
* Expression is evaluated against RequestEntity.
*
* @param statusCodeExpression the status code expression string
* @return the spec for method chaining
*/
public S statusCodeExpression(String statusCodeExpression);
/**
* Sets Expression to resolve status code.
*
* @param statusCodeExpression the status code expression
* @return the spec for method chaining
*/
public S statusCodeExpression(Expression statusCodeExpression);
/**
* Sets Function to resolve status code.
*
* @param statusCodeFunction function to evaluate status code
* @return the spec for method chaining
*/
public S statusCodeFunction(Function<RequestEntity<?>, ?> statusCodeFunction);
/**
* Sets Validator for payload validation.
* Validates payload before processing.
*
* @param validator the validator
* @return the spec for method chaining
*/
public S validator(Validator validator);
}Inner specification class for configuring RequestMapping in Java DSL. Used to configure URL patterns, HTTP methods, headers, and content types.
public class RequestMappingSpec {
/**
* Sets HTTP methods for the mapping.
*
* @param supportedMethods the HTTP methods
* @return the spec for method chaining
*/
public RequestMappingSpec methods(HttpMethod... supportedMethods);
/**
* Sets request parameters that must be present.
* Format: "param=value" or "param" (any value).
*
* @param params the request parameters
* @return the spec for method chaining
*/
public RequestMappingSpec params(String... params);
/**
* Sets request headers that must be present.
* Format: "header=value" or "header" (any value).
*
* @param headers the request headers
* @return the spec for method chaining
*/
public RequestMappingSpec headers(String... headers);
/**
* Sets consumable media types (Content-Type).
* Example: "application/json", "text/xml".
*
* @param consumes the consumable media types
* @return the spec for method chaining
*/
public RequestMappingSpec consumes(String... consumes);
/**
* Sets producible media types (Accept).
* Example: "application/json", "text/xml".
*
* @param produces the producible media types
* @return the spec for method chaining
*/
public RequestMappingSpec produces(String... produces);
}Usage Example:
@Bean
public IntegrationFlow advancedInboundFlow() {
return IntegrationFlow
.from(Http.inboundGateway("/api/data")
.requestMapping(m -> m
.methods(HttpMethod.POST, HttpMethod.PUT)
.params("version=2")
.headers("X-API-Key")
.consumes("application/json", "application/xml")
.produces("application/json"))
.requestPayloadType(DataRequest.class))
.handle("dataService", "processData")
.get();
}Inner specification class for configuring CORS in Java DSL. Used to configure Cross-Origin Resource Sharing settings for inbound endpoints.
public class CrossOriginSpec {
/**
* Sets allowed origins for cross-origin requests.
* Use "*" to allow all origins (not recommended for production).
*
* @param origin the allowed origins
* @return the spec for method chaining
*/
public CrossOriginSpec origin(String... origin);
/**
* Sets allowed request headers for preflight requests.
*
* @param allowedHeaders the allowed headers
* @return the spec for method chaining
*/
public CrossOriginSpec allowedHeaders(String... allowedHeaders);
/**
* Sets headers that can be exposed to the client.
*
* @param exposedHeaders the exposed headers
* @return the spec for method chaining
*/
public CrossOriginSpec exposedHeaders(String... exposedHeaders);
/**
* Sets supported HTTP methods for CORS.
*
* @param method the request methods
* @return the spec for method chaining
*/
public CrossOriginSpec method(RequestMethod... method);
/**
* Sets whether to allow credentials (cookies, authorization headers).
*
* @param allowCredentials true to allow credentials
* @return the spec for method chaining
*/
public CrossOriginSpec allowCredentials(Boolean allowCredentials);
/**
* Sets max age in seconds for preflight request caching.
* Default: 1800 (30 minutes).
*
* @param maxAge the max age in seconds
* @return the spec for method chaining
*/
public CrossOriginSpec maxAge(long maxAge);
}Usage Example:
@Bean
public IntegrationFlow corsEnabledFlow() {
return IntegrationFlow
.from(Http.inboundGateway("/api/public/data")
.requestMapping(m -> m
.methods(HttpMethod.GET, HttpMethod.POST))
.crossOrigin(c -> c
.origin("https://app.example.com", "https://admin.example.com")
.allowedHeaders("Authorization", "Content-Type", "X-Custom-Header")
.exposedHeaders("X-Total-Count", "X-Page-Number")
.method(RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS)
.allowCredentials(true)
.maxAge(3600))
.requestPayloadType(DataRequest.class))
.handle("publicService", "getData")
.get();
}import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.http.dsl.Http;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.util.DefaultUriBuilderFactory;
@Configuration
public class CompleteOutboundConfig {
@Bean
public IntegrationFlow completeOutboundFlow() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(5000);
factory.setReadTimeout(10000);
return IntegrationFlow
.from("orderChannel")
.handle(Http.outboundGateway("https://api.example.com/{version}/orders/{id}")
.httpMethod(HttpMethod.POST)
.uriVariable("version", "headers['api-version']")
.uriVariable("id", "payload.orderId")
.expectedResponseType(OrderResponse.class)
.requestFactory(factory)
.mappedRequestHeaders("Authorization", "Content-Type", "X-*")
.mappedResponseHeaders("X-Transaction-Id", "X-Status")
.transferCookies(true)
.extractResponseBody(true)
.charset("UTF-8")
.encodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY)
.extractPayload(true))
.channel("responseChannel")
.get();
}
}@Configuration
public class CompleteInboundConfig {
@Bean
public IntegrationFlow completeInboundFlow() {
return IntegrationFlow
.from(Http.inboundGateway("/api/orders/{id}")
.requestMapping(m -> m
.methods(HttpMethod.GET, HttpMethod.POST)
.params("version=2")
.headers("X-API-Key")
.consumes("application/json")
.produces("application/json"))
.crossOrigin(c -> c
.origin("https://app.example.com")
.allowedHeaders("Authorization", "Content-Type")
.exposedHeaders("X-Transaction-Id")
.method(RequestMethod.GET, RequestMethod.POST)
.allowCredentials(true)
.maxAge(3600))
.payloadExpression("#pathVariables.id")
.headerExpression("orderId", "#pathVariables.id")
.headerExpression("timestamp", "T(System).currentTimeMillis()")
.requestPayloadType(OrderRequest.class)
.mappedRequestHeaders("Authorization", "X-Request-Id")
.mappedResponseHeaders("X-Transaction-Id")
.extractReplyPayload(true)
.statusCodeExpression("payload.success ? T(org.springframework.http.HttpStatus).OK : T(org.springframework.http.HttpStatus).BAD_REQUEST")
.validator(orderValidator())
.convertExceptions(true))
.handle("orderService", "processOrder")
.get();
}
}@Bean
public IntegrationFlow dynamicFlow() {
return IntegrationFlow
.from("dynamicChannel")
.handle(Http.outboundGateway(m -> m.getHeaders().get("target_url"))
.httpMethodFunction(m -> HttpMethod.valueOf(
(String) m.getHeaders().get("http_method")))
.expectedResponseTypeFunction(m ->
(Class<?>) m.getHeaders().get("response_type"))
.uriVariablesFunction(m ->
(Map<String, ?>) m.getHeaders().get("uri_vars"))
.extractResponseBody(true))
.get();
}@Bean
public IntegrationFlow fileUploadFlow() {
MultipartAwareFormHttpMessageConverter converter =
new MultipartAwareFormHttpMessageConverter();
converter.setMultipartFileReader(new FileCopyingMultipartFileReader());
return IntegrationFlow
.from(Http.inboundGateway("/upload")
.requestMapping(m -> m
.methods(HttpMethod.POST)
.consumes("multipart/form-data"))
.messageConverters(converter)
.requestPayloadType(MultiValueMap.class))
.handle("fileService", "processUpload")
.get();
}