In this chapter, we will discuss Representational State Transfer (REST) web services, specifically those based on Java API for RESTful web services (JAX-RS 1.x), which is defined in JSR 311 (http://jcp.org/en/jsr/detail?id=311). The new version of JAX-RS (2.0) has become available and is discussed in Chapter 9, Using JAX-RS 2.0 in Java EE 7 with RESTEasy. The previous version is discussed first, as it forms the basis for the new version and also because a lot of developers are still using the previous version. Migration to a new version of existing applications does not happen as and when a new version becomes available. REST is a protocol independent, loosely coupled, software architecture style for distributed systems. Protocol independent implies that REST supports any protocol that supports transfer of representational state, but we will discuss REST over HTTP only. In comparison to SOAP, REST is less strongly typed, does not require XML parsing, and does not require message headers. RESTful web services are based on the REST principle and are simple, lightweight, and fast. A RESTful web service exposes a set of resources, which are simply sources of information, identified by URIs in HTTP. A resource could be a database record or a Plain Old Java Object (POJO) for example. What are exchanged are not resources themselves, but representations of resources, which are typically documents in HTML, XML, JSON, and plain text, but they could be an image or some other format. Based on the representations of resources and included metadata, a client makes changes to the resources.
RESTful web services follow these RESTful principles:
GET
, PUT
, POST
, and DELETE
are used.In this chapter, we will use the Jersey JAX-RS (JSR 311) Reference Implementation (RI) to create RESTful web services in Java. This chapter has the following sections:
We need to download and install the following software:
wildfly-8.1.0.Final.zip
from http://wildfly.org/downloads/.Set the environment variables JAVA_HOME
, JBOSS_HOME
, and MAVEN_HOME
. Add %JAVA_HOME%/bin
, %MAVEN_HOME%/bin
, and %JBOSS_HOME%/bin
to the PATH
environment variable.
Create a WildFly 8.1.0 runtime as discussed in Chapter 1, Getting Started with EJB 3.x.