Chapter 7. Developing a JAX-RS 1.1 Web Service

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:

  • Every resource has a unique base URI.
  • For invoking web service operations, the HTTP protocol methods such as GET, PUT, POST, and DELETE are used.
  • A client sends a request to a service, and the service returns a representation of a resource requested to the client.
  • Client sessions are not stored on the server, which makes it easier to scale the service with less data to replicate in a clustered environment.

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:

  • Setting up the environment
  • Creating a Java EE web project
  • Creating a sample RESTful web service
  • Deploying the RESTful web service
  • Running the RESTful web service
  • Creating a Java client
  • Running the Java client
  • Creating a JSP client
  • Running the JSP client

Setting up the environment

We need to download and install the following software:

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.21.237