Load a Static Resource Example - Mule 4

The following example illustrates how to configure the Anypoint Connector for HTTP (HTTP Connector) Listener source and the Load static resource operation to serve up static content for use with HTTP, using the request path to lookup the resource. To accomplish this example, you must create the Mule app, configure an HTTP global element, run, and test the app with curl commands.

The following screenshot shows the Anypoint Studio app flow for this example:

Load a static resource flow
Figure 1. Load a static resource flow

Create the Mule Application

To create the Mule flow:

  1. In the Mule Palette view, select the HTTP Listener source and drag it onto the canvas.
    The source initiates the flow by listening for incoming HTTP message attributes.

  2. Set the Path field to /trigger to start the app from the web browser.

  3. Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of the HTTP Listener in the app.

  4. On the General tab, configure the following fields:

    • Host: All Interfaces [] (default)

    • Port: 8081

  5. Click OK.

    HTTP Listener global configuration
    Figure 2. HTTP Listener global configuration
  6. Drag a Load static resource operation to the right of the HTTP Listener source.

  7. Set the Resource base path field to /Users/max/Sites/index.html.

  8. Set the Default file field to the name of a backup file in the same directory as the resource, for example index.html.
    If you need to use a relative path to the Mule project, use the variable ${app.workDir}. For example, resourceBasePath="${app.workDir}/images/". In the event of a failure, the app loads the backup file.

    HTTP Load static resource configuration
    Figure 3. HTTP Load static resource configuration
  9. Save your Mule app.

  10. Click the project name in Package Explorer and then click Run > Run As > Mule Application.

  11. In a browser window, type the URL
    The contents of the resource appear in the browser.

XML for Loading a Static Resource

Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="" port="8081" />
	<core:flow name="Flow" >
		<http:listener doc:name="Listener"  config-ref="HTTP_Listener_config"/>
		<http:load-static-resource doc:name="Load static resource" resourceBasePath="/Users/max/Sites/index.html" attributes="attributes"/>

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub