Restful Web Services Tutorial
Restful Web Services Tutorial
i
RESTful Web Services
This tutorial will teach you the basics of RESTful Web Services and contains chapters
discussing all the basic components of RESTful Web Services with suitable examples.
Audience
This tutorial is designed for Software Professionals who are willing to learn RESTful Web
Services in simple and easy steps. This tutorial will give you great understanding on
RESTful Web Services concepts and after completing this tutorial you will be at
intermediate level of expertise from where you can take yourself at higher level of
expertise.
Prerequisites
Before proceeding with this tutorial, you should have a basic understanding of Java
Language, Text Editor, etc. Because we are going to develop web services applications
using RESTful, so it will be good if you have understanding on other web technologies like
HTML, CSS, AJAX, etc.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
RESTful Web Services
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
ii
RESTful Web Services
Specifications ........................................................................................................................................ 40
iii
1. RESTful Web Services – Introduction RESTful Web Services
What is REST?
REST stands for REpresentational State Transfer. REST is a web standards based
architecture and uses HTTP Protocol for data communication. It revolves around resources
where every component is a resource and a resource is accessed by a common interface
using HTTP standard methods. REST was first introduced by Roy Fielding in year 2000.
In REST architecture, a REST Server simply provides access to resources and the REST
client accesses and presents the resources. Here each resource is identified by URIs/
Global IDs. REST uses various representations to represent a resource like Text, JSON and
XML. JSON is now the most popular format being used in Web Services.
HTTP Methods
The following HTTP methods are most commonly used in a REST based architecture.
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.
1
RESTful Web Services
Sr. HTTP
URI Operation Operation Type
No. Method
2
2. RESTful Web Services – Environment Setup RESTful Web Services
This tutorial will guide you on how to prepare a development environment to start your
work with Jersey Framework to create RESTful Web Services. Jersey framework
implements JAX-RS 2.0 API, which is a standard specification to create RESTful Web
Services. This tutorial will also teach you how to setup JDK, Tomcat and Eclipse on your
machine before you the Jersey Framework is setup.
If you are running Windows and installed the JDK in C:\jdk1.7.0_75, you would have to
put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk1.7.0_75\bin;%PATH%
set JAVA_HOME=C:\jdk1.7.0_75
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.7.0_75 and you use
the C Shell, you would put the following into your .cshrc file.
3
RESTful Web Services
%C:\eclipse\eclipse.exe
Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.)
machine:
$/usr/local/eclipse/eclipse
After a successful startup, if everything is fine, then your screen should display the
following result:
Make a choice whether you want to install Jersey on Windows, or Unix and then
proceed to the next step to download the .zip file for windows and then the .tz file
for Unix.
Download the latest version of Jersey framework binaries from the following link –
https://jersey.java.net/download.html.
You will find all the Jersey libraries in the directories C:\jaxrs-ri-2.17\jaxrs-ri\lib and
dependencies in C:\jaxrs-ri-2.17\jaxrs-ri\ext. Make sure you set your CLASSPATH
variable on this directory properly otherwise you will face problem while running your
application. If you are using Eclipse, then it is not required to set the CLASSPATH because
all the settings will be done through Eclipse.
%CATALINA_HOME%\bin\startup.bat
or
C:\apache-tomcat-7.0.59\bin\startup.bat
Tomcat can be started by executing the following commands on a Unix (Solaris, Linux,
etc.) machine:
$CATALINA_HOME/bin/startup.sh
or
/usr/local/apache-tomcat-7.0.59/bin/startup.sh
After a successful startup, the default web applications included with Tomcat will be
available by visiting http://localhost:8080/. If everything is fine then it should display
the following result:
5
RESTful Web Services
Further information about configuring and running Tomcat can be found in the
documentation included on this page. This information can also be found on the Tomcat
website: http://tomcat.apache.org.
%CATALINA_HOME%\bin\shutdown
or
C:\apache-tomcat-7.0.59\bin\shutdown
Tomcat can be stopped by executing the following commands on Unix (Solaris, Linux, etc.)
machine:
$CATALINA_HOME/bin/shutdown.sh
or
/usr/local/apache-tomcat-7.0.59/bin/shutdown.sh
Once you are done with this last step, you are ready to proceed for your first Jersey
example which you will see in the next chapter.
6
3. RESTful Web Services – First Application RESTful Web Services
Let us start writing the actual RESTful web services with Jersey Framework. Before you
start writing your first example using the Jersey Framework, you have to make sure that
you have setup your Jersey environment properly as explained in the RESTful Web
Services - Environment Setup chapter. Here, I am also assuming that you have a little
working knowledge of Eclipse IDE.
So, let us proceed to write a simple Jersey Application which will expose a web service
method to display the list of users.
7
RESTful Web Services
8
RESTful Web Services
Once your project is created successfully, you will have the following content in
your Project Explorer:
\jaxrs-ri-2.17\jaxrs-ri\api
\jaxrs-ri-2.17\jaxrs-ri\ext
\jaxrs-ri-2.17\jaxrs-ri\lib
Now, right click on your project name UserManagement and then follow the option
available in context menu: Build Path Configure Build Path to display the Java Build
Path window.
Now use Add JARs button available under Libraries tab to add the JARs present in WEB-
INF/lib directory.
User.java
package com.tutorialspoint;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User implements Serializable {
public User(){}
this.id = id;
this.name = name;
this.profession = profession;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
10
RESTful Web Services
UserDao.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
11
RESTful Web Services
}
else{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
userList = (List<User>) ois.readObject();
ois.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return userList;
}
private void saveUserList(List<User> userList){
try {
File file = new File("Users.dat");
FileOutputStream fos;
UserService.java
package com.tutorialspoint;
import java.util.List;
12
RESTful Web Services
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
}
There are two important points to be noted about the main program,
UserService.java
The first step is to specify a path for the web service using @Path annotation to the
UserService.
The second step is to specify a path for the particular web service method using
@Path annotation to method of UserService.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>User Management</display-name>
13
RESTful Web Services
<servlet>
<servlet-name>Jersey RESTful Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.tutorialspoint</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
To create a WAR file using eclipse, follow the option File export Web War File and
finally select project UserManagement and destination folder. To deploy a war file in
Tomcat, place the UserManagement.war in the Tomcat Installation Directory
webapps directory and start the Tomcat.
14
RESTful Web Services
15
4. RESTful Web Services – Resources RESTful Web Services
What is a Resource?
REST architecture treats every content as a resource. These resources can be Text Files,
Html Pages, Images, Videos or Dynamic Business Data. REST Server simply provides
access to resources and REST client accesses and modifies the resources. Here each
resource is identified by URIs/ Global IDs. REST uses various representations to represent
a resource where Text, JSON, XML. The most popular representations of resources are
XML and JSON.
Representation of Resources
A resource in REST is a similar Object in Object Oriented Programming or is like an Entity
in a Database. Once a resource is identified then its representation is to be decided using
a standard format so that the server can send the resource in the above said format and
client can understand the same format.
For example, in RESTful Web Services - First Application chapter, a user is a resource
which is represented using the following XML format:
<user>
<id>1</id>
<name>Mahesh</name>
<profession>Teacher</profession>
</user>
{
"id":1,
"name":"Mahesh",
"profession":"Teacher"
}
Understandability: Both the Server and the Client should be able to understand
and utilize the representation format of the resource.
16
RESTful Web Services
However, at present most of the web services are representing resources using either XML
or JSON format. There are plenty of libraries and tools available to understand, parse, and
modify XML and JSON data.
17
5. RESTful Web Services – Messages RESTful Web Services
RESTful Web Services make use of HTTP protocols as a medium of communication between
client and server. A client sends a message in form of a HTTP Request and the server
responds in the form of an HTTP Response. This technique is termed as Messaging. These
messages contain message data and metadata i.e. information about message itself. Let
us have a look on the HTTP Request and HTTP Response messages for HTTP 1.1.
HTTP Request
Verb – Indicates the HTTP methods such as GET, POST, DELETE, PUT, etc.
URI – Uniform Resource Identifier (URI) to identify the resource on the server.
HTTP Version – Indicates the HTTP version. For example, HTTP v1.1.
Request Header – Contains metadata for the HTTP Request message as key-value
pairs. For example, client (or browser) type, format supported by the client, format
of the message body, cache settings, etc.
18
RESTful Web Services
HTTP Response
Status/Response Code – Indicates the Server status for the requested resource.
For example, 404 means resource not found and 200 means response is ok.
HTTP Version – Indicates the HTTP version. For example HTTP v1.1.
Response Header – Contains metadata for the HTTP Response message as key-
value pairs. For example, content length, content type, response date, server type,
etc.
Example
As we have explained in the RESTful Web Services - First Application chapter, let us put
http://localhost:8080/UserManagement/rest/UserService/users in the POSTMAN with a
GET request. If you click on the Preview button which is near the send button of Postman
and then click on the Send button, you may see the following output.
19
RESTful Web Services
Here you can see, the browser sent a GET request and received a response body as XML.
20
6. RESTful Web Services – Addressing RESTful Web Services
Each resource in REST architecture is identified by its URI (Uniform Resource Identifier).
A URI is of the following format:
<protocol>://<service-name>/<ResourceType>/<ResourceID>
Purpose of an URI is to locate a resource(s) on the server hosting the web service. Another
important attribute of a request is VERB which identifies the operation to be performed on
the resource. For example, in RESTful Web Services - First Application chapter, the URI
is http://localhost:8080/UserManagement/rest/UserService/users and the
VERB is GET.
Use Plural Noun – Use plural noun to define resources. For example, we've
used users to identify users as a resource.
Avoid using spaces – Use underscore (_) or hyphen (-) when using a long
resource name. For example, use authorized_users instead of
authorized%20users.
Use HTTP Verb – Always use HTTP Verb like GET, PUT and DELETE to do the
operations on the resource. It is not good to use operations name in the URI.
Example
Following is an example of a poor URI to fetch a user.
http://localhost:8080/UserManagement/rest/UserService/getUser/1
http://localhost:8080/UserManagement/rest/UserService/users/1
21
7. RESTful Web Services – Methods RESTful Web Services
As we have discussed in the earlier chapters that RESTful Web Service uses a lot of HTTP
verbs to determine the operation to be carried out on the specified resource(s). The
following table states the examples of the most commonly used HTTP Verbs.
GET
http://localhost:8080/UserManagement/rest/UserService/users
1
Gets the list of users.
(Read Only)
GET
http://localhost:8080/UserManagement/rest/UserService/users/1
2
Gets the User of Id 1
(Read Only)
PUT
http://localhost:8080/UserManagement/rest/UserService/users/2
3
Inserts User with Id 2
(Idempotent)
POST
http://localhost:8080/UserManagement/rest/UserService/users/2
4
Updates the User with Id 2
(N/A)
DELETE
http://localhost:8080/UserManagement/rest/UserService/users/1
5
Deletes the User with Id 1
(Idempotent)
OPTIONS
http://localhost:8080/UserManagement/rest/UserService/users
6
Lists out the supported operations in a web service.
(Read Only)
HEAD
http://localhost:8080/UserManagement/rest/UserService/users
7
Returns the HTTP Header only, no Body.
(Read Only)
22
RESTful Web Services
PUT and DELETE operations are idempotent, which means their result will always
be the same, no matter how many times these operations are invoked.
PUT and POST operation are nearly the same with the difference lying only in the
result where the PUT operation is idempotent and POST operation can cause a
different result.
Example
Let us update an Example created in the RESTful Web Services - First Application chapter
to create a Web service which can perform CRUD (Create, Read, Update, Delete)
operations. For simplicity, we have used a file I/O to replace Database operations.
Let us update the User.java, UserDao.java and UserService.java files under the
com.tutorialspoint package.
User.java
package com.tutorialspoint;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User implements Serializable {
public User(){}
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getProfession() {
return profession;
}
@XmlElement
public void setProfession(String profession) {
this.profession = profession;
}
@Override
public boolean equals(Object object){
if(object == null){
return false;
}else if(!(object instanceof User)){
return false;
}else {
User user = (User)object;
if(id == user.getId()
&& name.equals(user.getName())
&& profession.equals(user.getProfession())
){
return true;
}
24
RESTful Web Services
}
return false;
}
}
UserDao.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
25
RESTful Web Services
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return userList;
}
26
RESTful Web Services
27
RESTful Web Services
UserService.java
package com.tutorialspoint;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
28
RESTful Web Services
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers(){
return userDao.getAllUsers();
}
@GET
@Path("/users/{userid}")
@Produces(MediaType.APPLICATION_XML)
public User getUser(@PathParam("userid") int userid){
return userDao.getUser(userid);
}
@PUT
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String createUser(@FormParam("id") int id,
@FormParam("name") String name,
@FormParam("profession") String profession,
@Context HttpServletResponse servletResponse) throws IOException{
User user = new User(id, name, profession);
int result = userDao.addUser(user);
if(result == 1){
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
@POST
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
29
RESTful Web Services
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String updateUser(@FormParam("id") int id,
@FormParam("name") String name,
@FormParam("profession") String profession,
@Context HttpServletResponse servletResponse) throws IOException{
User user = new User(id, name, profession);
int result = userDao.updateUser(user);
if(result == 1){
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
@DELETE
@Path("/users/{userid}")
@Produces(MediaType.APPLICATION_XML)
public String deleteUser(@PathParam("userid") int userid){
int result = userDao.deleteUser(userid);
if(result == 1){
return SUCCESS_RESULT;
}
return FAILURE_RESULT;
}
@OPTIONS
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public String getSupportedOperations(){
return "<operations>GET, PUT, POST, DELETE</operations>";
}
}
Now using Eclipse, export your application as a WAR File and deploy the same in Tomcat.
To create a WAR file using eclipse, follow this path – File export Web War
File and finally select project UserManagement and the destination folder. To deploy a
WAR file in Tomcat, place the UserManagement.war in the Tomcat Installation
Directory webapps directory and the start Tomcat.
30
RESTful Web Services
WebServiceTester.java
package com.tutorialspoint;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
tester.init();
31
RESTful Web Services
.path("/{userid}")
.resolveTemplate("userid", 1)
.request(MediaType.APPLICATION_XML)
.get(User.class);
String result = FAIL;
if(sampleUser != null && sampleUser.getId() == user.getId()){
32
RESTful Web Services
result = PASS;
}
System.out.println("Test case name: testGetUser, Result: " + result );
}
//Test: Update User of id 1
//Test: Check if result is success XML.
private void testUpdateUser(){
Form form = new Form();
form.param("id", "1");
form.param("name", "suresh");
form.param("profession", "clerk");
form.param("id", "2");
form.param("name", "naresh");
form.param("profession", "clerk");
33
RESTful Web Services
.put(Entity.entity(form,
MediaType.APPLICATION_FORM_URLENCODED_TYPE),
String.class);
Now run the tester using Eclipse. Right click on the file and follow the option Run as
Java Application. You will see the following result in the Eclipse console:
34
8. RESTful Web Services – Statelessness RESTful Web Services
As per the REST architecture, a RESTful Web Service should not keep a client state on the
server. This restriction is called Statelessness. It is the responsibility of the client to pass
its context to the server and then the server can store this context to process the client's
further request. For example, session maintained by server is identified by session
identifier passed by the client.
RESTful Web Services should adhere to this restriction. We have seen this in the RESTful
Web Services - Methods chapter, that the web service methods are not storing any
information from the client they are invoked from.
http://localhost:8080/UserManagement/rest/UserService/users/1
If you hit the above url using your browser or using a java based client or using Postman,
result will always be the User XML whose Id is 1 because the server does not store any
information about the client.
<user>
<id>1</id>
<name>mahesh</name>
<profession>1</profession>
</user>
Advantages of Statelessness
Following are the benefits of statelessness in RESTful Web Services:
Web services need not maintain the client's previous interactions. It simplifies the
application design.
Disadvantages of Statelessness
Following are the disadvantages of statelessness in RESTful Web Services:
Web services need to get extra information in each request and then interpret to
get the client's state in case the client interactions are to be taken care of.
35
9. RESTful Web Services – Caching RESTful Web Services
Caching refers to storing the server response in the client itself, so that a client need not
make a server request for the same resource again and again. A server response should
have information about how caching is to be done, so that a client caches the response
for a time-period or never caches the server response.
Following are the headers which a server response can have in order to configure a client's
caching:
Date
1
Date and Time of the resource when it was created.
Last Modified
2
Date and Time of the resource when it was last modified.
Cache-Control
3
Primary header to control caching.
Expires
4
Expiration date and time of caching
Age
5
Duration in seconds from when resource was fetched from the server.
Cache-Control Header
Following are the details of a Cache-Control header:
Public
1
Indicates that resource is cacheable by any component.
36
RESTful Web Services
Private
2 Indicates that resource is cacheable only by the client and the server, no
intermediary can cache the resource.
no-cache/no-store
3
Indicates that a resource is not cacheable.
max-age
4 Indicates the caching is valid up to max-age in seconds. After this, client has to
make another request.
must-revalidate
5
Indication to server to revalidate resource if max-age has passed.
Best Practices
Always keep static contents like images, CSS, JavaScript cacheable, with expiration
date of 2 to 3 days.
37
10. RESTful Web Services – Security RESTful Web Services
As RESTful Web Services work with HTTP URL Paths, it is very important to safeguard a
RESTful Web Service in the same manner as a website is secured.
Following are the best practices to be adhered to while designing a RESTful Web Service:
Validation – Validate all inputs on the server. Protect your server against SQL or
NoSQL injection attacks.
No Sensitive Data in the URL – Never use username, password or session token
in a URL, these values should be passed to Web Service via the POST method.
Throw generic Error Messages – A web service method should use HTTP error
messages like 403 to show access forbidden, etc.
HTTP Code
S. No. HTTP Code & Description
200
1
OK – shows success.
201
2 CREATED – when a resource is successfully created using POST or PUT request.
Returns link to the newly created resource using the location header.
204
3
NO CONTENT – when response body is empty. For example, a DELETE request.
304
38
RESTful Web Services
400
5 BAD REQUEST – states that an invalid input is provided. For example,
validation error, missing data.
401
403
7 FORBIDDEN – states that the user is not having access to the method being
used. For example, Delete access without admin rights.
404
8
NOT FOUND – states that the method is not available.
409
9 CONFLICT – states conflict situation while executing the method. For example,
adding duplicate entry.
500
10 INTERNAL SERVER ERROR – states that the server has thrown some
exception while executing the method.
39
11. RESTful Web Services – Java (JAX-RS) RESTful Web Services
JAX-RS stands for JAVA API for RESTful Web Services. JAX-RS is a JAVA based
programming language API and specification to provide support for created RESTful Web
Services. Its 2.0 version was released on the 24th May 2013. JAX-RS uses annotations
available from Java SE 5 to simplify the development of JAVA based web services creation
and deployment. It also provides supports for creating clients for RESTful Web Services.
Specifications
Following are the most commonly used annotations to map a resource as a web service
resource.
@Path
1
Relative path of the resource class/method.
@GET
2
HTTP Get request, used to fetch resource.
@PUT
3
HTTP PUT request, used to create resource.
@POST
4
HTTP POST request, used to create/update resource.
@DELETE
5
HTTP DELETE request, used to delete resource.
@HEAD
6
HTTP HEAD request, used to get status of method availability.
@Produces
7 States the HTTP Response generated by web service. For example,
APPLICATION/XML, TEXT/HTML, APPLICATION/JSON etc.
8 @Consumes
40
RESTful Web Services
@PathParam
9
Binds the parameter passed to the method to a value in path.
@QueryParam
10
Binds the parameter passed to method to a query parameter in the path.
@MatrixParam
11
Binds the parameter passed to the method to a HTTP matrix parameter in path.
@HeaderParam
12
Binds the parameter passed to the method to a HTTP header.
@CookieParam
13
Binds the parameter passed to the method to a Cookie.
@FormParam
14
Binds the parameter passed to the method to a form value.
@DefaultValue
15
Assigns a default value to a parameter passed to the method.
@Context
16
Context of the resource. For example, HTTPRequest as a context.
Note: We have used Jersey, a reference implementation of JAX-RS 2.0 by Oracle, in the
RESTful Web Services - First Application and RESTful Web Services - Methods chapters.
41