On Java Development

All things related to Java development, from the perspective of a caveman.

Creating a REST Service with Client

without comments

Introduction

This post shows how to create a REST service using jar-files provided by the JBoss RESTEasy project.

As described by the JBoss Community site,

RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation of the JAX-RS specification. JAX-RS is a new JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol.

 

Configuration

The example application presented was started using baseproject which provided a pre-configured and working web application framework. With this in place, additional entries for a couple of configuration files are required for a REST service.

Updating the pom.xml file
Open the project’s pom.xml file, add the entries shown and save.

Updating the web.xml file
Open the project’s web.xml file, add the entries shown and save.

 

Create new Package and Classes

Select the root package for the project. For example, select ‘com.mig.[project]’ where [project] is the name given to the package. Add the package rest.policy.

Create a new class called PolicyRecord01.java and replace the code with that shown below and resolve any errors.

This class acts as the template to be used to create the XML returned by the service. The entry @XmlRootElement(name = “policyRecord01”) indicates this is to be used as the root element name. Each use of @XmlElement indicates a named element for data.

 
Create a new class called PolicyService.java and replace the code with that shown below and resolve any errors.

 
This is the service class that retrieves the record for the data supplied on the browser. The path entry @Path(“/policyService”) indicates part of the URL to be supplied that will direct the process to this service class. For example, if this URL is placed into the browser “http://localhost:8080/restpolicysearch/restful-services/policyService/hello” the message returned will be “Hello from the PolicyService class’s hello method !”

 

Invoke the Service Using the Browser

Start the server and place this URL into the browser. “http://localhost:8080/restpolicysearch/restful-services/policyService/policy/123/456/7890123”. Replace 123, 456 and 7890123 with values for a policy and press enter to invoke the service. This example presents the XML shown below to the browser.

 

Invoke the Service Using a Client Application

Shown below is a simple RESTEasy client that calls the service application presented above. In it, the URL is created with the required parameters and then used to invoke the service.

The service returns the same XML that was earlier sent to the browser. The logic in the try/catch block beginning on line 25 converts the XML data and converts it back into the properties of PolicyRecord01.java. The output produced by System.out.println( policy.toString() ) is shown below. (The data has been sanitized.)

 

Summary

This post showed how to create a very simple REST service example which barely scratches the surface of this topic. The classes presented use annotations to describe the XML elements and used the @GET annotation to indicate the use of Get request methods. Other annotations used in this post are listed below.

A simple client application was introduced to show how the service could be invoked from another application and how the XML returned could be turned back into its POJO.

  • @Path – annotation specify the URL path on which this method will be invoked.
  • @PathParam – binds REST-style URL parameters to method arguments
  • @XmlElement – Annotate all fields that needs to be included in XML/JSON output.
  • @Produces – annotation specifies the type of output this method (or web service) will produce.
  • @XmlRootElement – Define the root element for the XML to be produced. The name of the root XML element is derived from the class name.

There are many more.

Written by admin

April 8th, 2014 at 2:02 pm

Leave a Reply

You must be logged in to post a comment.