Skip to content

Intellij configuration

jakob-grabner edited this page Oct 13, 2015 · 4 revisions

Starting out with Jersey & Apache Tomcat using IntelliJ

Based on this.

Creating a New Project

To create a new project, open up the New Project dialog in IntelliJ. In the left menu, select Java Enterprise. To the right, select RESTful Web Service. Below, change the radio selection from **Download **to Set up library later. We will be using **Maven **to download and manage our libraries.

Intellij Setup 01

Adding Maven Support

In order to add Maven to your project, you can avoid the command line completely by right-clicking on your project’s name in the Project sidebar and selecting Add Framework Support. In the Add Frameworks Support dialog, select Maven. Once this is done, the pom.xml file will open in the editor. You will need to enter at least a groupId, usually your package name. For this tutorial, I will be using at.grabner.

groupId: This is generally unique amongst an organization or a project. For example, all core Maven artifacts do live under the groupId org.apache.maven. Group ID's do not necessarily use the dot notation, for example, the junit project. Note that the dot-notated groupId does not have to correspond to the package structure that the project contains. It is, however, a good practice to follow. When stored within a repository, the group acts much like the Java packaging structure does in an operating system. The dots are replaced by OS specific directory separators (such as '/' in Unix) which becomes a relative directory structure from the base repository.

artifactId: The artifactId is generally the name that the project is known by. It, along with the groupId, create a key that separates this project from every other project in the world (at least, it should :) ).

version: groupId:artifactId denote a single project but they cannot delineate which incarnation of that project we are talking about. In short: code changes, those changes should be versioned, and this element keeps those versions in line. It is also used within an artifact's repository to separate versions from each other.

The three elements given above point to a specific version of a project letting Maven knows who we are dealing with, and when in its software lifecycle we want them.

Intellij Setup 02

Your project structure should look as follows:

Intellij Setup 03

If you are missing the web.xml make sure you added the Web Application Framework. Open the Add Frameworks Support dialog again. This time, select Web Application and ensure that Create web.xml is checked.

Maven

Before we can use Jersey, we need to add the library to our project. We will do this with Maven by adding the following to our pom.xml file:

org.glassfish.jersey.core jersey-client 2.8 org.glassfish.jersey.containers jersey-container-servlet 2.8 org.glassfish.jersey.containers jersey-container-servlet-core 2.8 com.google.guava guava 18.0 asm asm 3.3.1

To be honest, I am not entirely sure which dependencies are minimum required for jersey to work. With this setup it works for me.

Creating a rest resource

Inside of the src/main/java directory, create a new package matching your groupId if not done automatically. Inside of this package, create a resource such as HelloWorld.java:

package at.grabner;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;

// The Java class will be hosted at the URI path "/<artifactname>/apiv1/"
@Path("/apiv1/")
public class HelloWorld {
    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }    
}

Add a artifact to deploy

Open the project structure dialog File -> Project Structure -> Artifacts -> Add -> Web Application Exploded -> From Module with Dependencies -> Your Module

Intellij Setup 07

Give your artifact a meaningful name, because the name will be part of the url to reach the web services. Make sure you add all dependencies you need on the server to the run the web services. To add dependencies right click it and choose Put into /WEB-INF/libs

Setting up Tomcat

Add a remote server:

Tools -> Deployment -> Configuration Add -> Use type SFTP or whatever works for you

Set connection parameters: Intellij Setup 04

Add mapping: Intellij Setup 05

Creating a Run & Debug Configuration

Before Tomcat can serve your application, you’ll need to set it up. Do this by creating a new run configuration. This is done by navigating through Run > Edit Configurations… > “+” > Tomcat Server > Remote. Name this whatever you like. During this step, we need to select the artifact for Tomcat to deploy. Choose the **Deployment **tab, clicking “+”, and choosing Artifact… Select the only artifact listed then choose **Apply **and finally OK.

Intellij Setup 06

Configuring your Servlet

Inside of your web/WEB-INF/web.xml file, you’ll need to add the following contents:

<web-app>
<display-name>SmartCare API</display-name>
<filter>
    <filter-name>My_Rest_API</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                at.grabner
            </param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.scanning.recursive</param-name>
            <param-value>false</param-value>
        </init-param>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>My_Rest_API</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

We use package scanning, this means Jersey will automatically look for all rest resources in the defined packages. Note that the two properties in and <filter-mapping have to be the same (My_Rest_API in this case).

The url-pattern is part of the url your web service can be reached. It follows the general pattern :/// in my case this would be: 192.168.1.1:8888/jersey_template/apiv1

Run your Application

That’s everything! From here, you can run your application through IntelliJ.