Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Building Rest Service Made Easy Using Eclipse Plugin

DZone's Guide to

Building Rest Service Made Easy Using Eclipse Plugin

Want to get a RESTful service up and running quickly? There is an Eclipse plugin for that.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

RESTful services have become increasingly popular and as a result, there are various frameworks that support the development of REST Services. Each has their own advantages, but some of the widely used ones 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, download the plugin from 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 the capability to add jars or libraries to the project and generate template classes.

Now create a new project in eclipse. Choose the dynamic web project as an option to create the project. Name the project and click 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.

The Web.xml file will get updated, and jersey jars are added to the lib folder.

A java class (with the same name as in the wizard) gets generated with Jersey supported annotations. The class is your starting point to develop a  REST service. Here is what 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.

You can also 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 + “‘”);

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
restful ,webservice ,rest ,web ,service ,wizard ,jersey ,rest services

Published at DZone with permission of Vivek Raut, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}