SOA / Web Services / Java

A Technology Blog

Building REST Service made easy using Eclipse Plugin

Posted by Vivek on May 7, 2016

RESTful services have become increasingly popular and as a result there are various frameworks that supports the development of REST Services. Each has its own advantages but some of the widely used are

  • Jersey
  • RestEasy
  • Restlet
  • Spring

Eclipse provides a plugin that leverages these frameworks and helps in rapidly developing services.

If you do not have eclipse installed on your machine, then download the installer from https://eclipse.org/downloads/

In order to start with developing REST Services, please follow the below URL to download the plugin

https://marketplace.eclipse.org/content/restful-plugin-eclipse

Once the plugin is installed successfully, you should be able to see a new category “Restful Webservice” in the wizard menu

This plugin also provides capability to add jars or libraries to the project and generate template classes.

Now create a new project in eclipse. Choose the dymanic web project as an option to create the project. Name the project and click on Next. Leave the other options as default ones.In the Web Module screen, check the “Generate web.xml deployment descriptor” option and click Finish.

To make the project Jersey compliant, right click on project root and select New->other.

Go to the Restful Webservice option and choose the Jersey RESTful Webservice. It will launch the Jersey Restful Webservice wizard.

Enter the package name and click on Finish. Do not modify any other values.

Web.xml file gets updated and jersey jars are added to the lib folder.

A java class (with the same name as that in wizard) gets generated with Jersey supported annotations. The class is your starting point to develop a  REST service. Here is how it will look like:

@Path(“/<add your restful service class name here>”)

public class SimpleRestService {

private static final Logger logger = Logger.getLogger(SimpleRestService.class);

The @Path annotation will define the service class name. Similarly, every resource in the service has to be identified uniquely using the @Path annotation at method level.

Similarly, you can make the project RestEasy compliant. In this case, class generated will look like as follows:

@Path(“/<add your restful service class name here>”)

public class SimpleRestService {

private static final Logger logger = Logger.getLogger(SimpleRestService.class);

@GET

@Path(“/<add method name here>”)

@Produces(MediaType.TEXT_PLAIN)

public String getSomething(@QueryParam(“request”) String request ,

@DefaultValue(“1”) @QueryParam(“version”) int version) {

……………………..

 

A Restlet compliant class will look like as follows:

public class SimpleRestService extends ServerResource {

private static final Logger logger = Logger.getLogger(SimpleRestService.class);

@Get

public String getSomething() {

…………………..

 

A Spring compliant class will be seen like the one below:

@RestController

@RequestMapping(“/<add your restful service class name here>”)

public class SimpleRestController {

// Logger instance

private static final Logger logger = Logger.getLogger(SimpleRestController.class);

@RequestMapping(value = “/<add method name here>”, method = RequestMethod.GET)

public String getSomething(@RequestParam(value = “request”) String request, @RequestParam(value = “version”, required = false, defaultValue = “1”) int version) {

if (logger.isDebugEnabled()) {

logger.debug(“Start getSomething”);

logger.debug(“data: ‘” + request + “‘”);

}

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: