IntelliJ IDEA 2020.2 Help

Tutorial: Your first RESTful web service

This tutorial describes how to develop a simple RESTful web service in IntelliJ IDEA and deploy it to the GlassFishTomcat application server. The service will output Hello, World! when you access a specific URL through the web browser or otherwise send a GET request to this URL. Use the switcher at the top of the page for instructions for a different application server.

You will create a new Java Enterprise project, add the necessary Java code, tell IntelliJ IDEA where your GlassFishTomcat server is located, then use a run configuration to build the artifact, start the server, and deploy the artifact to it.

Here is what you will need:

  • Java SE Development Kit (JDK) version 1.8 or later. You can get the JDK directly from IntelliJ IDEA as described in Java Development Kit (JDK) or download and install it manually, for example Oracle JDK.

  • The GlassFish application server version 3.0.1 or later. You can get the latest release from the official reference implementation web site. The Web Profile subset should be enough for the purposes of this tutorial.

  • The Tomcat application server version 7 or later.

  • A web browser to view your web application.

Create a new Java Enterprise project

IntelliJ IDEA includes a dedicated wizard for creating Java Enterprise projects based on various Java EE and Jakarta EE implementations. In this tutorial, we will create a web service based on the Eclipse Jersey implementation.

  1. From the main menu, select File | New | Project.

  2. In the New Project dialog, select Java Enterprise. For this tutorial, use Java 1.8 as the project SDKs and leave other settings by default: Maven as the build tool and JUnit as the test runner. Select Java as your project language and click Next.

    New Java Enterprise project wizard
  3. In the Libraries and Frameworks list, select the RESTful Web Services (JAX-RS) specification, and then select Eclipse Jersey Server and Eclipse Jersey Client as the implementation. Then click Next.

    Select the languages and frameworks for the new Java Enterprise project
  4. Enter a name for your project: RestGlassfishHelloWorldRestTomcatHelloWorld. Then click Finish.

    Enter a name for the new Java Enterprise project
    Enter a name for the new Java Enterprise project

Write the source code

Add the HelloWorld class with a method that returns the string Hello, World! and the MyApplication class with the application configuration.

  1. In the Project tool window, right-click the src/main/java directory and select New | Java Class.

  2. In the New Java Class dialog, enter the name of your class HelloWorld and press Enter.

  3. Add the following code to HelloWorld.java:

    import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello-world") public class HelloWorld { @GET @Produces("text/plain") public String getClichedMessage() { return "Hello, World!"; } }

    This is a root resource class, which uses the following JAX-RS annotations to implement the RESTful web service:

    • The @Path annotation identifies the URI for accessing this resource, relative to the application root.

    • The @GET annotation indicates that the getClichedMessage() method will process HTTP GET requests to the specified URI.

    • The @Produces annotation specifies the MIME media type that the method produces and returns.

  4. In the Project tool window, right-click the src/main/java directory and select New | Java Class.

  5. In the New Java Class dialog, enter the name of your class MyApplication and press Enter.

  6. Add the following code to MyApplication.java:

    import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; @ApplicationPath("/") public class MyApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> h = new HashSet<>(); h.add( HelloWorld.class ); return h; } }

    This is a subclass of javax.ws.rs.core.Application, which is used to configure the environment where the application runs REST resources defined in your resource classes. In our example, it overrides the getClasses() method and returns a Set of root resource, provider, and feature classes to be included in the published JAX-RS application (here, it's just the HelloWorld.class).

    The @ApplicationPath annotation identifies the URL mapping for the application.

Configure the application server

Let IntelliJ IDEA know where the GlassFishTomcat application server is located.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Build, Execution, Deployment | Application Servers.

  2. Click the Add button and select Glassfish ServerTomcat Server.

  3. Specify the path to the GlassFishTomcat server install location. IntelliJ IDEA detects and sets the name and version appropriately.

    GlassFish application server configuration
    Tomcat application server configuration

Make sure that you have the correct Java runtime configured for your GlassFish server. You can run GlassFish on any supported Java runtime or use the one configured for your project (Java 8 in this tutorial). Press Ctrl+Alt+Shift+S to open the Project Structure dialog and copy the JDK home path under SDKs. Then open the asenv.conf file under glassfish/config in the GlassFish installation directory and add the path to the JDK home as the value for AS_JAVA like this: AS_JAVA=/absolute/path/to/java/home.

Create a run configuration

IntelliJ IDEA needs a run configuration to build the artifacts and deploy them to your application server.

  1. From the main menu, select Run | Edit Configurations.

  2. In the Run/Debug Configurations dialog, click the Add button, expand the GlassFish ServerTomcat Server node, and select Local.

  3. Fix any warnings that appear at the bottom of the run configuration settings dialog.

    Run configuration warning

    Most likely, you will need to fix the following:

    • On the Server tab, set the Server Domain to domain1.

    • On the Deployment tab, add the artifact that you want to deploy: RestGlassfishHelloWorld:war exploded RestTomcatHelloWorld:war exploded

  4. On the Server tab, set the URL to http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/hello-world http://localhost:8080/RestTomcatHelloWorld_war_exploded/hello-world and save the run configuration.

    GlassFish run configuration done
    Tomcat run configuration done
  5. To run the configuration, press Alt+Shift+F10 and select the created application server configuration.

    Alternatively, if you have your run configuration selected in the main toolbar at the top, you can press Shift+F10 to run it.

This run configuration builds the artifacts, then starts the GlassFishTomcat server, and deploys the artifacts to the server. You should see the corresponding output in the Run tool window.

Started GlassFish server and deployed application in the Run tool window
Started Tomcat server and deployed application in the Run tool window

Once this is done, it opens the specified URL in your web browser.

Deployed application output in the web browser
Deployed application output in the web browser

Troubleshooting

If you are using IntelliJ IDEA version 2020.2.2 or earlier, the New Project wizard will not add all of the necessary dependencies required for Tomcat. In this case, open pom.xml and add the following dependencies:

<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.31</version> </dependency>

For example, in version 2020.2.3, the generated pom.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>RestTomcatHelloWorld</artifactId> <version>1.0-SNAPSHOT</version> <name>RestTomcatHelloWorld</name> <packaging>war</packaging> <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <junit.version>5.6.2</junit.version> </properties> <dependencies> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.1.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.31</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.0</version> </plugin> </plugins> </build> </project>
Last modified: 06 October 2020