4/29/18
Module 4: Building APIs
Goal
Service
with APIs
API
Spec
(RAML)
All contents © MuleSoft Inc. 2
1
4/29/18
At the end of this module, you should be able to
• Use Anypoint Studio to build, run, and test Mule applications
• Use a connector to connect to databases
• Use the graphical DataWeave editor to transform data
• Create RESTful interfaces for applications from RAML files
• Connect API interfaces to API implementations
All contents © MuleSoft Inc. 3
Reviewing Mule 4 applications
2
4/29/18
Review: Mule 4 applications and flows
• Mule applications receive events, process them, and route them to
other endpoints
• Mule applications accept and process a Mule event through a series
of Mule event processors plugged together in a flow with
– A Mule event source that initiates the execution of the flow
– Mule event processors that transform, filter, enrich, and process the event
and its message
Flow
Mule event Mule event Mule event Connector
source processor(s) processor(s) endpoint
Mule event Mule event Mule event
All contents © MuleSoft Inc. 5
Review: Mule 4 event structure
Mule event
The data that passes through flows
Mule message in the app
Attributes Metadata contained in the message
header
Payload
The core info of the message -
the data the app processes
Metadata for the Mule event -
Variables can be defined and referenced in
the app processing the event
All contents © MuleSoft Inc. 6
3
4/29/18
Creating Mule applications with
Anypoint Studio
Introducing Anypoint Studio
• Based on Eclipse, a common Java integrated development environment
• Features include
– Two-way editing between graphical and XML views
– Pre-built tooling to connect to APIs (REST, SOAP), protocols (HTTP, FTP, SMTP,
more), and popular services (Salesforce, Workday, Facebook, more!)
– A data transformation framework and language
– An embedded Mule runtime to test applications without leaving it
– Visual debugging
– One-click deployment of applications to CloudHub
– Templates for common integration patterns
– Integration with Maven for continuous build processes
All contents © MuleSoft Inc. 8
4
4/29/18
Anypoint Studio anatomy
Package
Explorer Canvas Mule
Palette
Console
All contents © MuleSoft Inc. 9
Anatomy of a flow: Visual
Event source
Event processors
Error handling
All contents © MuleSoft Inc. 10
5
4/29/18
Anatomy of a flow: XML
All contents © MuleSoft Inc. 11
Mule application building blocks
• Are separated into categories in
the Core section of the Mule Palette
• By default, projects include HTTP
and Sockets modules
• Can add additional modules
All contents © MuleSoft Inc. 12
6
4/29/18
Running applications
• Anypoint Studio comes with an embedded Mule runtime to test
applications without leaving it
• The console outputs application logs and information
All contents © MuleSoft Inc. 13
Automating testing of applications
• You can automate testing of Mule applications using MUnit
• MUnit is a Mule app testing framework for building automated tests
• MUnit is fully integrated with Anypoint Studio
– You can create, design, and run MUnit tests and suites of tests
just like you do Mule applications
• MUnit is covered in Anypoint Platform Development: Advanced
All contents © MuleSoft Inc. 14
7
4/29/18
Walkthrough 4-1: Create a Mule application with
Anypoint Studio
• Create a new Mule project with Anypoint Studio
• Add a connector to receive requests at an endpoint
• Set the message payload
• Run a Mule application using the embedded Mule runtime
• Make an HTTP request to the endpoint using ARC
All contents © MuleSoft Inc. 15
Connecting to data
8
4/29/18
The Database connector
• Can connect to almost any JDBC relational database
– Any database engine for which you have a driver
• To use
– Add the Database module to your project
– Add a database operation to a flow
– Configure the connection to the database
All contents © MuleSoft Inc. 17
Global configuration elements
• For most operations, a lot of the configuration is encapsulated in
a separate global element
– A reusable configuration that can be used by many operations
– Defines a connection to a network resource
• This is a connector configuration
– Though it is sometimes referred to
simply as the connector
All contents © MuleSoft Inc. 18
9
4/29/18
Walkthrough 4-2: Connect to data (MySQL database)
• Add a Database Select operation
• Configure a Database connector that connects to a MySQL database
– Or optionally an in-memory Derby database if you do not have access to port 3306
• Configure the Database Select operation to use that Database connector
• Write a query to select data from a table in the database
All contents © MuleSoft Inc. 19
Transforming data
10
4/29/18
Transforming data
• DataWeave 2.0 is the expression language for Mule to access, query,
and transform Mule 4 event data
– DataWeave was introduced and used in Module 2
• In Studio, use Transform Message
component for transformations
– Graphical interface with payload-aware development
Input Type(s) Transform Desired Type
All contents © MuleSoft Inc. 21
Walkthrough 4-3: Transform data
• Use the Transform Message component
• Use the DataWeave visual mapper to change the response to a
different JSON structure
All contents © MuleSoft Inc. 22
11
4/29/18
Creating RESTful interfaces
manually for Mule applications
Creating RESTful interfaces
• A RESTful interface for an application will have listeners
for each resource / method pairing defined by the API
– GET: /flights
– POST: /flights
– GET: /flights/{ID}
– DELETE: /flights/{ID}
– PUT: /flights/{ID}
• You can create the interface manually or have it
generated from the API definition
– We will do both in the next two walkthroughs
All contents © MuleSoft Inc. 24
12
4/29/18
Walkthrough 4-4: Create a RESTful interface for a
Mule application
• Route based on path
• Use a URI parameter in the path of a new HTTP Listener
• Route based on HTTP method
All contents © MuleSoft Inc. 25
Generating RESTful interfaces
automatically using APIkit
13
4/29/18
Creating RESTful interfaces automatically using APIkit
• APIkit is an open-source toolkit that includes an Anypoint Studio plugin
• The Anypoint Studio APIkit plugin can generate an
interface automatically from a RAML API definition
– For new or existing projects
• It generates a main routing flow and flows for
each of the API resource / method pairs
• You add processors to the resource flows to hook
up to your backend logic
All contents © MuleSoft Inc. 27
Walkthrough 4-5: Use Anypoint Studio to create a
RESTful API interface from a RAML file
• Add Anypoint Platform credentials to Anypoint Studio
• Import an API from Design Center into an Anypoint Studio project
• Use APIkit to generate a RESTful web service interface from an API
• Test a web service using APIkit console and Advanced REST Client
All contents © MuleSoft Inc. 28
14
4/29/18
Connecting interfaces to
implementations
Passing messages to other flows
• Flows can be broken into multiple flows
– Makes the graphical view more intuitive and the
XML code easier to read
– Promotes code reuse
• All flows are identified by name and can be
called via Flow Reference components in
other flows
All contents © MuleSoft Inc. 30
15
4/29/18
Walkthrough 4-6: Implement a RESTful web service
• Pass an event from one flow to another
• Call the backend flows
• Create new logic for the nested resource call
• Test the web service using APIkit console
All contents © MuleSoft Inc. 31
19
Summary
16
4/29/18
Summary
• Anypoint Studio can be used to build Mule applications for
integrations and API implementations
– Two-way editing between graphical and XML views
– An embedded Mule runtime for testing applications
• Mule applications accept and process events through a series of
event processors plugged together in a flow
– Use the HTTP Listener as an inbound endpoint to trigger a flow with an HTTP
request
– Use the Set Payload transformer to set the payload
– Use the Database connector to connect to JDBC databases
– Use DataWeave and the Transform Message component to transform
messages from one data type and structure to another
All contents © MuleSoft Inc. 33
Summary
• Create RESTful interfaces for applications
– Manually by creating flows with listeners for each resource/method pairing
– Automatically using Anypoint Studio and APIkit
• Connect web service interfaces to implementations using the Flow
Reference component to pass messages to other flows
All contents © MuleSoft Inc. 34
17