Lecture_Week05
Lecture_Week05
Lecture Week 05
REST API (JAX-RS)
Dr. Hamed Hamzeh
Travel Agency
Application
GET /airline/booking
Deploying a Airline
flight booking Web service Booking
service { Search
“flight_no: 2234”
}
/booking /search
Question!
What kind of web service is suitable for this architecture? RESTful service
What is REST?
End point
protocol Version
Port
Domain number Application Parameter
name Context
Resource
What is JAX-RS?
Definition: • JAX-RS, or Java API for RESTful Web Services, is a set of Java APIs
that facilitate the development of RESTful web services.
Java applications.
Annotation-driven:
HTTP Methods:
• Support for common HTTP methods - GET, POST, PUT, DELETE, etc.
URI Mapping:
Content Negotiation:
Exception Handling:
Java Classes as Web Resources: @Path Annotation: HTTP Methods and Annotations:
In JAX-RS, a resource class is a Java class that The @Path annotation is used to associate a Resource methods within the class are
models a web resource. resource class or method with a URI path. annotated with HTTP methods like @GET,
It encapsulates the functionality of a specific It defines the base URI for the resource, @POST, @PUT, etc.
resource or a collection of related resources. making it accessible via HTTP. These annotations define the type of HTTP
request the method can handle.
Jersey
Jersey is the official reference implementation of JAX-RS.
Jersey
Developed by Sun Microsystems (now Oracle) and maintained by the Eclipse Foundation.
Provides a comprehensive set of APIs that implement JAX-RS, simplifying the development of RESTful services in Java.
Well-documented
Jersey integrates seamlessly with Java EE (Enterprise Edition) and Jakarta EE, making it suitable for enterprise-level applications.
@Path
• Defines the base URI for the resource class or
method.
@Produces, @Consumes
• Specify the media types the resource can produce
or consume.
Dependencies
These dependencies are typically required to compile, run, and deploy JAX-
RS applications.
Web Server
servlet
The URI path template allows you to define variables that appear as placeholders in the URI. These
variables would be replaced at runtime with the values set by the client.
The URI path template looks like /departments/{id}.
At runtime, the client can pass an appropriate value for the id parameter to get the desired resource from
the server.
For instance, the URI path of the /departments/10 format returns the IT department details to the caller.
Answer: the system reports the status back to the caller with
Q: What happens if a specified name does not an appropriate HTTP status code, such as 404 Not Found,
match with the regular expression? which tells the caller that the requested resource could not be
found at this moment.
Media Types
Media types define the format of the data being transmitted between the client and server in RESTful web
services.
They enable the client and server to understand the structure and encoding of the data being exchanged.
Media types can be specified using the @Produces and @Consumes annotations in JAX-RS resource
classes and methods.
@Produces
• is used to specify the media types that the resource method can produce (i.e., send back to the client).
@Consumes
• is used to specify the media types that the resource method can consume (i.e., accept from the client).
Example Media Types
application/json:
application/xml:
text/plain:
multipart/form-data:
application/x-www-form-urlencoded:
The Accept header in the client request specifies the media types
that the client is willing to accept.
• The following example uses the @Produces annotation at the class level in order to set the
default response media type as JSON for all resource methods in this class.
• At runtime, the binding provider will convert the Java representation of the return value to the
JSON format.
We use media/content
type as JSON format
@Consumes Annotation
• The following example illustrates how you can use the @Consumes attribute to designate a
method in a class to consume a payload presented in the JSON media type.
• The binding provider will copy the JSON representation of an input message to the Department
parameter of the createDepartment() method.
• A RESTful system uses the HTTP GET method type for retrieving the resources referenced in the URI path.
• The @javax.ws.rs.GET annotation designates a method of a resource class to respond to the HTTP GET
requests.
• In the following example, the REST URI for accessing the findAllDepartments() method may look like
/departments. The complete URI path may take the following URI pattern:
• The HTTP PUT method is used for updating or creating the resource pointed by the URI.
• The @javax.ws.rs.PUT annotation designates a method of a resource class to respond to the HTTP PUT requests.
• When a request reaches a server, the framework intercepts the request and directs it to the appropriate method
that matches the URI path and the HTTP method type.
• The following code snippet shows how you can use the @PUT annotation to designate the editDepartment()
method to respond to the HTTP PUT request.
• The HTTP POST method posts data to the server. Typically, this method type is used for creating a
resource.
• The @javax.ws.rs.POST annotation designates a method of a resource class to respond to the
HTTP POST requests.
• The following code snippet shows how you can use the @POST annotation to designate the
createDepartment() method to respond to the HTTP POST request.
The REST API call to remove the department resource identified by id=10 looks like:
DELETE /departments/10 HTTP/1.1.
Annotations for accessing request parameters - PathParam
• The @javax.ws.rs.QueryParam annotation injects the value(s) of a HTTP query parameter into a class field, a resource class
bean property (the getter method for accessing the attribute), or a method parameter.
• The following example illustrates the use of @QueryParam to extract the value of the desired query parameter present in the
URI.
• This example extracts the value of the query parameter, name, from the request URI and copies the value into the deptName
method parameter.
• The URI that accesses the IT department resource looks like /departments?name=IT:
@Path("/products/{category}/{productId}")
public class ProductResource {
@GET
public Response getProductDetails( Example URI:
@PathParam("category") String category, •/products/electronics/123;color=red;size=medium
@PathParam("productId") int productId,
@MatrixParam("color") String color,
@MatrixParam("size") String size
) {
// Implementation logic
}
}
Annotations for accessing
request parameters -
FormParam
• Apache Tomcat is an open-source web server and servlet container developed by the
Apache Software Foundation.
• Tomcat is lightweight and easy to use, making it a popular choice for developing and
deploying Java-based web applications.
Glassfish
• GlassFish is an open-source application server project that provides a robust and scalable
platform for developing, deploying, and managing Java EE (Enterprise Edition) and
Jakarta EE (the successor to Java EE) applications.
• It is developed by the Eclipse Foundation and has a long history, initially being a project
sponsored by Sun Microsystems and later Oracle.
Wildfly server
Lightweight application
Scalability
development
Integration Administration
Postman