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.
Join the DZone community and get the full member experience.
Join For FreeRESTful 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 + “‘”);
Published at DZone with permission of Vivek Raut, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments