<div dir="ltr" style="text-align: justify;">
<div style="text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/jaxrs-tutorials.gif" border="0" /></div>
<p>Int his REST Web Service tutorial we will demonstrates how <b>RESTful </b>services are created using <b>JAX-RS</b>. We&#8217;ll be using Tomcat as our primary application server. Java API for RESTful Web Services (<b>JAX-RS</b>), is a set if APIs to developer REST service. <b>JAX-RS</b> is part of the Java EE6, and make developers to develop REST web application easily.<br />
<b></b></p>
<h2><b>What Are <i>RESTful </i>Web Services?</b></h2>
<p><b>RESTful web services</b> are built to work best on the Web. <b>Representational State Transfer (REST) </b>is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as <b>performance, scalability, and modifiability</b>, that enable services to work best on the Web. In the <b>REST </b>architectural style, data and functionality are considered resources and are accessed using <b>Uniform Resource Identifiers (URIs)</b>, typically links on the Web. The resources are acted upon by using a set of simple, well-defined operations. The REST architectural style constrains an architecture to a client/server architecture and is designed to use a stateless communication protocol, typically HTTP. In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.</p>
<p>The following principles encourage <i><b>RESTful </b></i>applications to be simple, lightweight, and fast:</p>
<div id="ads-id" align="center"></div>
<ul>
<li><b>Resource identification through URI</b>: A <i><b>RESTful </b></i>web service exposes a set of resources that identify the targets of the interaction with its clients. Resources are identified by URIs, which provide a global addressing space for resource and service discovery. See The <i><b>@Path </b></i>Annotation and URI Path Templates for more information.</li>
<li><b>Uniform interface</b>: Resources are manipulated using a fixed set of four <b>create, read, update, delete</b> operations: <b>PUT, GET, POST, and DELETE</b>. <b>PUT creates a new resource</b>, which can be then <b>deleted by using DELETE</b>. <b>GET retrieves the current state of a resource in some representation</b>. <b>POST transfers a new state onto a resource</b>. See Responding to HTTP Methods and Requests for more information.</li>
<li><b>Self-descriptive messages</b>: Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as <b>HTML, XML, plain text, PDF, JPEG, JSON, and others</b>. Metadata about the resource is available and used, for example, to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control. See Responding to HTTP Methods and Requests and Using Entity Providers to Map HTTP Response and Request Entity Bodies for more information.</li>
<li><b>Stateful interactions through hyperlinks</b>: Every interaction with a resource is stateless; that is, request messages are self-contained. Stateful interactions are based on the concept of explicit state transfer. Several techniques exist to exchange state, such as URI rewriting, cookies, and hidden form fields. State can be embedded in response messages to point to valid future states of the interaction. See Using Entity Providers to Map HTTP Response and Request Entity Bodies and “Building URIs” in the JAX-RS Overview document for more information.</li>
</ul>
<h2><strong>RESTful web API HTTP methods</strong></h2>
<table border="1">
<tbody>
<tr>
<th>Resource</th>
<th>GET</th>
<th>PUT</th>
<th>POST</th>
<th>DELETE</th>
</tr>
<tr>
<th>Collection URI, such as http://example.com/resources/</th>
<td><b>List</b> the URIs and perhaps other details of the collection&#8217;s members.</td>
<td><b>Replace</b> the entire collection with another collection.</td>
<td><b>Create</b> a new entry in the collection. The new entry&#8217;s URI is assigned automatically and is usually returned by the operation.</td>
<td><b>Delete</b> the entire collection.</td>
</tr>
<tr>
<th>Element URI, such as http://example.com/resources/item17</th>
<td><b>Retrieve</b> a representation of the addressed member of the collection, expressed in an appropriate Internet media type.</td>
<td><b>Replace</b> the addressed member of the collection, or if it doesn&#8217;t exist, <b>create</b> it.</td>
<td>Not generally used. Treat the addressed member as a collection in its own right and <b>create</b> a new entry in it.</td>
<td><b>Delete</b> the addressed member of the collection.</td>
</tr>
</tbody>
</table>
<h2><b>Creating a <i>RESTful </i>Root Resource Class</b></h2>
<p><b>Root resource classes</b> are <i><b>POJOs </b></i>that are either annotated with<i><b> @Path</b></i> or have at least one method annotated with <i><b>@Path</b></i> or a <b>request method designator</b>, such as <i><b>@GET, @PUT, @POST, or @DELETE</b></i>. <b>Resource methods</b> are methods of a resource class annotated with a request method designator. This section explains how to use <i><b>JAX-RS</b></i> to annotate Java classes to create <b><i>RESTful </i></b>web services.<br />
<b></b></p>
<h2><b>Developing <i>RESTful </i>Web Services with <i>JAX-RS</i></b></h2>
<p><b>JAX-RS</b> is a Java programming language API designed to make it easy to develop applications that use the REST architecture.<br />
The<b> JAX-RS API</b> uses Java programming language annotations to simplify the development of <i><b>RESTful</b></i> web services. Developers decorate Java programming language class files with <b>JAX-RS</b> annotations to define resources and the actions that can be performed on those resources. JAX-RS annotations are runtime annotations; therefore, runtime reflection will generate the helper classes and artifacts for the resource. A Java EE application archive containing <b>JAX-RS </b>resource classes will have the resources configured, the helper classes and artifacts generated, and the resource exposed to clients by deploying the archive to a Java EE server.</p>
<table border="1">
<tbody>
<tr>
<th scope="column" align="left" valign="top">Annotation</th>
<th scope="column" align="left" valign="top">Description</th>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1217"></a>@Path</td>
<td scope="row" align="left" valign="top">The @Path annotation’s value is a relative URI<br />
path indicating where the Java class will be hosted: for example, /helloworld.<br />
You can also embed variables in the URIs to make a URI<br />
path template. For example, you could ask for the name of a user<br />
and pass it to the application as a variable in the URI: /helloworld/{username}.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1218"></a>@GET</td>
<td scope="row" align="left" valign="top">The @GET annotation is a request method designator and corresponds to the similarly<br />
named HTTP method. The Java method annotated with this request method designator will<br />
process HTTP GET requests. The behavior of a resource is determined by the<br />
HTTP method to which the resource is responding.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1219"></a>@POST</td>
<td scope="row" align="left" valign="top">The @POST annotation is a<br />
request method designator and corresponds to the similarly named HTTP method. The Java<br />
method annotated with this request method designator will process HTTP POST requests. The<br />
behavior of a resource is determined by the HTTP method to which the<br />
resource is responding.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1220"></a>@PUT</td>
<td scope="row" align="left" valign="top">The @PUT annotation is a request method designator and corresponds to<br />
the similarly named HTTP method. The Java method annotated with this request method<br />
designator will process HTTP PUT requests. The behavior of a resource is determined<br />
by the HTTP method to which the resource is responding.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1221"></a>@DELETE</td>
<td scope="row" align="left" valign="top">The @DELETE annotation<br />
is a request method designator and corresponds to the similarly named HTTP method.<br />
The Java method annotated with this request method designator will process HTTP DELETE<br />
requests. The behavior of a resource is determined by the HTTP method to<br />
which the resource is responding.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1222"></a>@HEAD</td>
<td scope="row" align="left" valign="top">The @HEAD annotation is a request method designator and corresponds<br />
to the similarly named HTTP method. The Java method annotated with this request<br />
method designator will process HTTP HEAD requests. The behavior of a resource is<br />
determined by the HTTP method to which the resource is responding.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1223"></a>@PathParam</td>
<td scope="row" align="left" valign="top">The @PathParam<br />
annotation is a type of parameter that you can extract for use in<br />
your resource class. URI path parameters are extracted from the request URI, and<br />
the parameter names correspond to the URI path template variable names specified in<br />
the @Path class-level annotation.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1224"></a>@QueryParam</td>
<td scope="row" align="left" valign="top">The @QueryParam annotation is a type of parameter that you can<br />
extract for use in your resource class. Query parameters are extracted from the<br />
request URI query parameters.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1225"></a>@Consumes</td>
<td scope="row" align="left" valign="top">The @Consumes annotation is used to specify the MIME media<br />
types of representations a resource can consume that were sent by the client.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1226"></a>@Produces</td>
<td scope="row" align="left" valign="top">The @Produces annotation is used to specify the MIME media types of representations a<br />
resource can produce and send back to the client: for example, &#8220;text/plain&#8221;.</td>
</tr>
<tr>
<td scope="row" align="left" valign="top"><a href="https://www.blogger.com/null" name="indexterm-1227"></a>@Provider</td>
<td scope="row" align="left" valign="top">The @Provider<br />
annotation is used for anything that is of interest to the JAX-RS runtime,<br />
such as MessageBodyReader and MessageBodyWriter. For HTTP requests, the MessageBodyReader is<br />
used to map an HTTP request entity body to method parameters. On the<br />
response side, a return value is mapped to an HTTP response entity body<br />
by using a MessageBodyWriter. If the application needs to supply additional metadata, such<br />
as HTTP headers or a different status code, a method can return a Response that wraps the entity and that can be built using Response.ResponseBuilder.</td>
</tr>
</tbody>
</table>
<h2><b>Contents-</b><br />
<b><br />
</b> <b>REST</b></h2>
<ol style="text-align: left;">
<li><a href="https://dineshonjava.com/what-is-rest-and-rest-architecture-and-rest-constraints/" target="_blank" rel="noopener"><b>What is REST and REST Architecture and REST Constraints</b></a></li>
<li><b><a href="https://dineshonjava.com/jax-rs-jersey-hello-world-example/" target="_blank" rel="noopener">JAX-RS &; Jersey Hello World Example</a></b></li>
<li><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/overview-of-jax-rs-application/" target="_blank" rel="noopener">Overview of a JAX-RS Application</a> </b></li>
<li><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/using-consumes-and-produces-to/" target="_blank" rel="noopener">Using @Consumes and @Produces to Customize Request and Response</a> </b></li>
<li><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/annotations-for-field-and-bean/" target="_blank" rel="noopener">Annotations for Field and Bean Properties of Resource Classses</a> </b></li>
</ol>
<h2><b><br />
Working with JSON</b></h2>
<p>Create a RESTful Java client to perform &#8220;GET&#8221; and &#8220;POST&#8221; request to manipulate json data.</p>
<ol>
<li><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/restful-web-services-with-jersey-jax-rs/" target="_blank" rel="noopener">RESTful Web Services with Jersey JAX-RS JSON on Tomcat</a></b></li>
</ol>
<h2><b><br />
RESTful Java clients</b></h2>
<p>Create a RESTful Java client to perform &#8220;GET&#8221; and &#8220;POST&#8221; request to manipulate json data.</p>
<ol>
<li><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/create-restful-java-client-with-jersey/" target="_blank" rel="noopener">Create RESTful Java Client With Jersey Client Example</a> </b></li>
</ol>
<h2><b><br />
Working with XML</b></h2>
<p>XML support in JAX-RS.</p>
<ol>
<li><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/using-jax-rs-with-jaxb/" target="_blank" rel="noopener">Using JAX-RS With JAXB</a> </b></li>
<li><b><b><a class="GIL3GQOBOB" href="https://dineshonjava.com/restful-web-services-with-jersey-jax-rs/" target="_blank" rel="noopener">RESTful Web Services with Jersey JAX-RS using XML on Tomcat</a></b> </b></li>
</ol>
<h2><b><br />
Basic Examples</b></h2>
<p>Basic annotations and functions to develop REST service.</p>
<ol>
<li><b><a href="https://dineshonjava.com/jax-rs-path-uri-matching-example/" target="_blank" rel="noopener">JAX-RS @Path URI matching example</a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-pathparam-example/" target="_blank" rel="noopener">JAX-RS @PathParam example</a><br />
Simple way to inject URI parameter that defined in @Path into Java method.</b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-queryparam-example/" target="_blank" rel="noopener">JAX-RS @QueryParam example</a></b><br />
Example to get query paremeter in URI path, and also how to define an optional paramater.</li>
<li><b><a href="https://dineshonjava.com/jax-rs-matrixparam-example/" target="_blank" rel="noopener">JAX-RS @MatrixParam example</a></b><br />
Example to get matrix parameters in URI path.</li>
<li><b><a href="https://dineshonjava.com/jax-rs-formparam-example/" target="_blank" rel="noopener">JAX-RS @FormParam example</a></b><br />
Example to get HTML post form parameter values.</li>
<li><b><a href="https://dineshonjava.com/get-http-header-in-jax-rs/" target="_blank" rel="noopener">Get HTTP headers in JAX-RS</a></b><br />
Show the use of @HeaderParam and @Context to get HTTP headers.</li>
<li><b><a href="https://dineshonjava.com/download-text-file-from-jax-rs/" target="_blank" rel="noopener">Download text file from JAX-RS</a></b><br />
Example to output a text file for user to download.</li>
<li><b><a href="https://dineshonjava.com/download-image-file-from-jax-rs/" target="_blank" rel="noopener">Download image file from JAX-RS</a></b><br />
Example to output an image file for user to download.</li>
<li><b><a href="https://dineshonjava.com/download-pdf-file-from-jax-rs/" target="_blank" rel="noopener">Download pdf file from JAX-RS</a></b><br />
Example to output pdf file for user to download.</li>
<li><b><a href="https://dineshonjava.com/download-excel-file-from-jax-rs/" target="_blank" rel="noopener">Download excel file from JAX-RS</a></b><br />
Example to output excel file for user to download.</li>
</ol>
<p><i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank" rel="noopener">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank" rel="noopener">Wikipedia for REST Web Service</a></b></i></p>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;Previous <;<; || <a href="https://dineshonjava.com/jax-rs-web-service-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/jax-rs-jersey-hello-world-example/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/types-supported-by-jax-ws/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/jax-rs-jersey-hello-world-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '432'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…