Int his REST Web Service tutorial we will demonstrates how RESTful services are created using JAX-RS. We’ll be using Tomcat as our primary application server. Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.
What Are RESTful Web Services?
RESTful web services are built to work best on the Web. Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability, and modifiability, that enable services to work best on the Web. In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs), 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.
The following principles encourage RESTful applications to be simple, lightweight, and fast:
- Resource identification through URI: A RESTful 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 @Path Annotation and URI Path Templates for more information.
- Uniform interface: Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE. PUT creates a new resource, which can be then deleted by using DELETE. GET retrieves the current state of a resource in some representation. POST transfers a new state onto a resource. See Responding to HTTP Methods and Requests for more information.
- Self-descriptive messages: Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others. 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.
- Stateful interactions through hyperlinks: 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.
RESTful web API HTTP methods
Resource | GET | PUT | POST | DELETE |
---|---|---|---|---|
Collection URI, such as http://example.com/resources/ | List the URIs and perhaps other details of the collection’s members. | Replace the entire collection with another collection. | Create a new entry in the collection. The new entry’s URI is assigned automatically and is usually returned by the operation. | Delete the entire collection. |
Element URI, such as http://example.com/resources/item17 | Retrieve a representation of the addressed member of the collection, expressed in an appropriate Internet media type. | Replace the addressed member of the collection, or if it doesn’t exist, create it. | Not generally used. Treat the addressed member as a collection in its own right and create a new entry in it. | Delete the addressed member of the collection. |
Creating a RESTful Root Resource Class
Root resource classes are POJOs that are either annotated with @Path or have at least one method annotated with @Path or a request method designator, such as @GET, @PUT, @POST, or @DELETE. Resource methods are methods of a resource class annotated with a request method designator. This section explains how to use JAX-RS to annotate Java classes to create RESTful web services.
Developing RESTful Web Services with JAX-RS
JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture.
The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services. Developers decorate Java programming language class files with JAX-RS 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 JAX-RS 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.
Contents-
REST
- What is REST and REST Architecture and REST Constraints
- JAX-RS & Jersey Hello World Example
- Overview of a JAX-RS Application
- Using @Consumes and @Produces to Customize Request and Response
- Annotations for Field and Bean Properties of Resource Classses
Working with JSON
Create a RESTful Java client to perform “GET” and “POST” request to manipulate json data.
RESTful Java clients
Create a RESTful Java client to perform “GET” and “POST” request to manipulate json data.
Working with XML
XML support in JAX-RS.
Basic Examples
Basic annotations and functions to develop REST service.
- JAX-RS @Path URI matching example
- JAX-RS @PathParam example
Simple way to inject URI parameter that defined in @Path into Java method. - JAX-RS @QueryParam example
Example to get query paremeter in URI path, and also how to define an optional paramater. - JAX-RS @MatrixParam example
Example to get matrix parameters in URI path. - JAX-RS @FormParam example
Example to get HTML post form parameter values. - Get HTTP headers in JAX-RS
Show the use of @HeaderParam and @Context to get HTTP headers. - Download text file from JAX-RS
Example to output a text file for user to download. - Download image file from JAX-RS
Example to output an image file for user to download. - Download pdf file from JAX-RS
Example to output pdf file for user to download. - Download excel file from JAX-RS
Example to output excel file for user to download.
References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service
Thanks Dinesh for providing wonderful information on Spring and webservices, need small favour from you, procedure and example on spring+ webservice (RestFull) Application. please can you help me out..
Hi Pallavi,
Thanks for this nice compliment.
Pallavi could you plz send your requirement in detail at admin@dineshonjava.com
Thanks,
Dinesh
struts bull sheet! use spring….
Hi, This is Kalyani.
Can you please send me the code for the same if you don't mind. Here is my email address kalyanikas786@gmail.com
Hi Kalyani,
Please look into this url. have code as well
https://www.dineshonjava.com/2013/05/spring-crud-example-using-one-to-one.html