May 12, 2016

Building Rest Service Made Easy Using Eclipse Plugin

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:JerseyRestEasyRestletSpringEclipse 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 order to start with developing REST Services, download the plugin from 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(“/”) 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(“/”) public class SimpleRestService { private static final Logger logger = Logger.getLogger(SimpleRestService.class); @GET @Path(“/”) @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(“/”) public class SimpleRestController { // Logger instance private static final Logger logger = Logger.getLogger(SimpleRestController.class); @RequestMapping(value = “/”, 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 + “‘”);

Add comment