23. HTTP Support

23.1 Introduction

The HTTP support allows for the making of HTTP requests and the processing of inbound Http requests. Because interaction over HTTP is always synchronous, even if all that is returned is a 200 status code the Http support consists of two gateway implementations HttpInboundEndpoint and HttpOutboundEndpoint.

23.2 Http Inbound Gateway

To receive messages over http you need to use an HttpInboundEndpoint. In common with the HttpInvoker support the Http Inbound Gateway needs to be deployed within a servlet container. The easiest way to do this is to provide a servlet definition in web.xml, see Section 22.2, “HttpInvoker Inbound Gateway” for further details. Below is an example bean definition for a simple HttpInboundEndpoint

<bean id="httpInbound" class="org.springframework.integration.http.HttpInboundEndpoint">
	<property name="requestChannel" ref="httpRequestChannel" />
	<property name="replyChannel" ref="httpReplyChannel" />	
</bean>

The HttpInboundEndpoint accepts an instance of InboundRequestMapper which allows customisation of the mapping from HttpServletRequest to Message. If none is provided an instance of DefaultInboundRequestMapper will be used. This encapsulates a simple strategy, which for example will create a String message for a POST request where the content type starts with "text", see the Javadoc for full details.

Starting with this release MultiPart File support was implemented. If the request has been wrapped as a MultipartHttpServletRequest, then the 'content type' can be checked. If it is known, and begins with "text", then the MultipartFile will be copied to a String in the parameter map. If the content type does not begin with "text", then the MultipartFile will be copied to a byte array within the parameter map instead.

[Note]Note
The HttpInboundEndpoint will locate a MultipartResolver in the context if one exists with the bean name "multipartResolver" (the same name expected by Spring's DispatcherServlet). If it does in fact locate that bean, then the support for MultipartFiles will be enabled on the inbound request mapper. Otherwise, it will fail when trying to map a multipart-file request to a Spring Integration Message. For more on Spring's support for MultipartResolvers, refer to the Spring Reference Manual.

In sending a response to the client there are a number of ways to customise the behaviour of the gateway. By default the gateway will simply acknowledge that the request was received by sending a 200 status code back. It is possible to customise this response by providing an implementation of the Spring MVC View which will be invoked with the created Message. In the case that the gateway should expect a reply to the Message then setting the expectReply flag will cause the gateway to wait for a response Message before creating an Http response. Below is an example of a gateway configured to use a custom view and to wait for a response. It also shows how to customise the Http methods accepted by the gateway, which are POST and GET by default.

<bean id="httpInbound" class="org.springframework.integration.http.HttpInboundEndpoint">
	<property name="requestChannel" ref="httpRequestChannel" />
	<property name="replyChannel" ref="httpReplyChannel" />
	<property name="view" ref="jsonView" />
	<property name="supportedMethods" >
		<list>
			<value>GET</value>
			<value>DELETE</value>
		</list>
	</property>
	<property name="expectReply" value="true" />
	<property name="requestMapper" ref="customRequestMapper" />
</bean>

The message created from the request will be available in the Model map. The key that is used for that map entry by default is 'requestMessage', but this can be overridden by setting the 'requestKey' property on the endpoint's configuration.

23.3 Http Outbound Gateway

To configure the HttpOutboundEndpoint write a bean definition like this:

<bean id="httpOutbound" class="org.springframework.integration.http.HttpOutboundEndpoint" >
	<property name="outputChannel" ref="responseChannel" />
</bean>

This bean definition will execute Http requests by first converting the message to the Http request using an instance of DefaultOutboundRequestMapper. This will expect to find the request URL in the message header under the key HttpHeaders.REQUEST_URL. It is also possible to set a default target URL as a constructor argument along with other options as shown below.

<bean id="httpOutbound" class="org.springframework.integration.http.HttpOutboundEndpoint" >
	<constructor-arg value="http://localhost:8080/example" />
	<property name="outputChannel" ref="responseChannel" />
	<property name="sendTimeout" value="5000" />
	<property name="requestMapper" ref="customRequestMapper" />
</bean>

By default the Http request will be made using an instance of SimpleHttpRequestExecutor which uses the JDK HttpURLConnection. Use of the Apache Commons Http Client is also supported through the provided CommonsHttpRequestExecutor which can be injected into the outbound gateway.

23.4 Http Namespace Support

Spring Integration provides an "http" namespace and schema definition. To include it in your configuration, simply provide the following URI within a namespace declaration: 'http://www.springframework.org/schema/integration/http'. The schema location should then map to 'http://www.springframework.org/schema/integration/http/spring-integration-http-1.0.xsd'.

To configure an inbound http channel adapter which is an instance of HttpInboundEndpoint configured not to expect a response.

 <http:inbound-channel-adapter id="httpChannelAdapter" channel="requests" supported-methods="PUT, DELETE"/>

To configure an inbound http gateway which expects a response.

 <http:inbound-gateway id="inboundGateway" request-channel="requests" reply-channel="responses"/>

To configure the outbound gateway you can use the namespace support as well. The following code snippet shows the different configuration options for an outbound Http gateway.

<http:outbound-gateway id="fullConfigWithoutMapper"
		request-channel="requests"
		default-url="http://localhost/test"
		extract-request-payload="false"
		charset="UTF-8"
		request-executor="executor"
		request-timeout="1234"
		reply-channel="replies"/>

If you want to provide a custom OutboundRequestMapper, then a reference may be supplied to the 'request-mapper' attribute. In that case however you will not be allowed to set the default URL, charset, and 'extract-request-payload' properties since those are all properties of the default mapper (see the JavaDoc for DefaultOutboundRequestMapper for more information).