Creating a Maven Web-Service in IntelliJ

Here is a step-by-step procedure on making a Maven web-service using Java in the IntelliJ Environment.

Open IntelliJ and go to File > New project. In the popup that opens, select Maven on the left and check the checkbox Create from archetype and select maven-archetype-webapp.



Give your project a group-id and an artifact id. The group id could be something like com.testpackage, and the artifact id something like testproject. Leave all other settings as they are and continue to finish. This should create a new project.

In the pom.xml (or testproject.xml) file, add the dependencies for rest-easy.

Add the jetty plugin to the plugins block. We would be using the jetty server to run our web-service.
(Note: This documentation assumes that you have maven installed and added to your PATH variable, such that you can use the mvn command from the terminal.)





Add the following content to your web.xml file, which is present in src > main > webapp > WEB-INF:



Create a directory named java within main. Right click on the java directory and go to Mark Directory as and select Sources Root.

Right click on the java directory and select new package. Create two packages: com.testpackage.app and com.testpackage.rest.

In the package com.testpackage.app, create a new Java Class and name it EntryPoint. Copy the following content into EntryPoint.java:

package com.testpackage.app; // {{ groupId}}.app
// import the rest service
import com.testpackage.rest.RestService;
import javax.ws.rs.core.Application;

import java.util.HashSet;
import java.util.Set;

public class EntryPoint extends Application {
  private Set singletons = new HashSet();
  public EntryPoint() {
      // Register our rest service
      singletons.add(new RestService());
  }
  @Override
  public Set getSingletons() {
      return singletons;
  }
}


In the package com.testpackage.rest, create a new Java Class named RestService. Add the following code to the RestService.java file:


package com.testpackage.rest;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.io.*;
import java.util.*;
import java.sql.*;
import com.google.gson.*;
import org.apache.commons.dbutils.DbUtils;

@Path("/")
public class RestService {
  @GET
  @Path("/isReady")
  public Response isReady() {
      return Response.status(200).entity("ready").build();
  }
}
You have successfully created a maven web-service!

To build and run the webservice, open the terminal within IntelliJ and use the following commands:


$ mvn clean install
(to build the project)


$ mvn jetty:run
(to run the web-service)

The port at which the web-service is running would be given in the terminal. To test the web-service, open any browser and go the the url: localhost:/testproject/isReady
You should get a response ready in your browser. The service is running!

To add methods to the service, use the annotations similar to those in the isReady() method, and add the methods in the RestService.java file.

Comments

Popular posts from this blog

Hosting a Website and Creating a DNS Server

Creating a Mail Server Using Webmin