2. Rest Full Webservice
● A web service is a collection of open protocols and standards used for
exchanging data between applications or systems
● Web services based on REST Architecture are known as RESTful
Web Services. These web services use HTTP methods to implement
the concept of REST architecture.
● A RESTful web service usually defines a URI (Uniform Resource
Identifier), which is a service that provides resource representation
such as JSON and a set of HTTP Methods.
3. HTTP Methods:
● The following HTTP methods are most commonly used in a
REST based architecture.
>GET − Provides a read only access to a resource.
>PUT − Used to update the existing resource.
>DELETE − Used to remove a resource.
>POST − Used to update an existing resource or create a new
resource.
4. REST service in MULE:
● In the context of web services, this generally means that
RESTful web services communicate via pure HTTP using
XML or JSON to encapsulate the data and metadata.
Publishing/Implementing a Rest Service: Two ways
1) APIkit
- APIkit can create an API based on a RAML (RESTful API
Modeling Language) API definition and produces a Mule
application with an HTTP endpoint, exception strategies
2) REST component:
- Use a REST API to publish an external RESTful web service.
- A REST component publishes a RESTful web service via
JAX-RS annotations and using Jersey by specifying the REST
component class.
5. Consuming a REST service:
To consume a REST API using Mule, use one of the following
techniques:
● Configure an HTTP connector to send requests to, and accept
responses from an API from within your Mule application.
● Build a connector using Anypoint Connector DevKit to perform
operations against the API.
6. Rest Component
● Mule hosts RESTful web services using Jersey, which is a
JAX-RS (JSR-311) implementation.
● JAX-RS is a specification that provides a series of annotations
and classes that make it possible to build RESTful services
● Using the annotations present in JAX-RS we can create unique
signatures(unique URI) for every type of request.
Annotations:
● @GET - Annotate your Get request methods with @GET
Eg: @GET
public String getHTML() {
...
}
7. ● @Produces:specifies the type of output this method (or web
service) will produce
Eg:@GET
@Produces("application/xml")
public Contact getXML() {
...
}
● @Path: specify the URL path on which this method will be
invoked.
Eg: @GET
@Produces("application/xml")
@Path("xml/{firstName}")
public Contact getXML() {
...
8. ● @PathParam-We can bind REST-style URL parameters to
method arguments using @PathParam annotation as shown
below.
Eg:
@GET
@Produces("application/xml")
@Path("xml/{firstName}")
public Contact getXML(@PathParam("firstName") String
firstName) {
Contact contact =
contactService.findByFirstName(firstName);
return contact;
}
9. ● @QueryParam- Request parameters in query string can be
accessed using @QueryParam annotation as shown below.
Eg:
@GET
@Produces("application/json")
@Path("json/companyList")
public CompanyList getJSON(@QueryParam("start") int start,
@QueryParam("limit") int limit) {
CompanyList list = new
CompanyList(companyService.listCompanies(start, limit));
return list;
}
● @POST -Annotate POST request methods with @POST.
● @PUT-Annotate PUT request methods with @PUT
10. ● @DELETE- Annotate DELETE request methods with
@DELETE.
● @Consumes-The @Consumes annotation is used to specify the
MIME media types a REST resource can consume.
Eg:
@PUT
@Consumes("application/json")
@Produces("application/json")
@Path("{contactId}")
public RestResponse<Contact> update(Contact contact) {
...
}
NOTE: used for POST and PUT operations.
11. Rest-Client:
● RESTClient is a simple application for talking to RESTful
web services.
● It is designed to fill a gap in existing offerings by offering
support for GET/POST/PUT/DELETE, making it a useful
tool when exploring RESTful web services which use a
wider range of HTTP verbs.
● RESTClient aims to fill this gap by allowing you to enter
*any* URL and POST/PUT/DELETE to it, you can move
data around more easily.
● By providing a simple user interface for copy pasting data,
you can fetch data from one source, copy it, change your
URL and post the data to another source.
Creating REST Clients
12. Rest Client in mule
● Step1: Create a rest service
- this rest service should be available for consuming it
● Step2:Consume the created rest service by creating a rest client
- write a java class for creating a rest client.
- using invoke you can call the rest client created.
Dynamic Endpoints:
● An outbound endpoint can also be dynamic. This means that the
endpoint’s URI is the value of an expression, which is
evaluated just before a message is sent to it.
● This allows the target of a message to be determined by its
contents or the value of any message property.
13. Dynamic endpoints can use either of the endpoint formats shown
above.
● <outbound-endpoint
address="smtp://user:secret@#[message.outboundProperties['h
ost']]"/>
● <jms:outbound-endpoint host="localhost"
queue="#[app.registry['defaultJmsQueue']]"/>