Tutorial: Your first RESTful web service
This tutorial describes how to create a simple RESTful web service in IntelliJ IDEA and deploy it to the Tomcat 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 this 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 Tomcat 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:
IntelliJ IDEA Ultimate | Java Enterprise / Jakarta Enterprise development is not supported in the free IntelliJ IDEA Community Edition. For more information, refer to IntelliJ IDEA Ultimate vs IntelliJ IDEA Community Edition |
Relevant plugins | By default, the following necessary plugins are bundled and enabled in IntelliJ IDEA Ultimate. If something does not work, make sure that the following plugins are enabled:
|
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 or OpenJDK. |
Tomcat | The Tomcat application server version 7 or later.
|
Web browser | You will need a web browser to view your web application. |
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 simple web application.
In the main menu, go to File | New | Project.
In the New Project dialog, select Jakarta EE.
Enter a name for your project:
RestTomcatHelloWorld
. For this tutorial, use Oracle OpenJDK 21 as the project SDK and select the REST service template. Don't select or add an application server, we will do it later. Select Java and Maven. Click Next to continue.In the Version field, select Jakarta EE 10 because that's what Tomcat 10.1 used in this tutorial is compatible with.
tip
For Tomcat 9, select Java EE 8. For Tomcat 10, select Jakarta EE 9.1.
In the Dependencies list, select the following:
Servlet
Eclipse Jersey Server
Weld SE
Click Create.
IntelliJ IDEA creates a project with some boilerplate code that you can build and deploy successfully.
tip
Use the Project tool window to browse and open files in your project or press CtrlShift0N and type the name of the file.
pom.xml is the Project Object Model with Maven configuration information, including dependencies and plugins necessary for building the project.
pom.xml
{...}HelloResource.java 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 thehello()
method will process HTTP GET requests to the specified URI.The
@Produces
annotation specifies the MIME media type that the method produces and returns.
src/main/java/com/example/RestTomcatHelloWorld/HelloResource.java
{...}HelloApplication.java 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. The@ApplicationPath
annotation identifies the URL mapping for the application root (by default, it is set to/api
).src/main/java/com/example/RestTomcatHelloWorld/HelloApplication.java
{...}
Let IntelliJ IDEA know where the Tomcat application server is located.
Press CtrlAlt0S to open settings and then select Build, Execution, Deployment | Application Servers.
Click
and select Tomcat.
Specify the path to the Tomcat server install location. IntelliJ IDEA detects and sets the name and version appropriately.
IntelliJ IDEA needs a run configuration to build the artifacts and deploy them to your application server.
In the main menu, go to Run | Edit Configurations.
In the Run/Debug Configurations dialog, click
, expand the Tomcat Server node, and select Local.
Fix any warnings that appear at the bottom of the run configuration settings dialog.
Most likely, you will need to fix the following:
On the Deployment tab, add the artifact that you want to deploy:
RestTomcatHelloWorld:war exploded
On the Server tab, set the URL to point to the root resource:
http://localhost:8080/RestTomcatHelloWorld_war_exploded/api/hello-world
Click OK to save the run configuration.
To run the configuration, press AltShiftF10 and select the created application server configuration.
Alternatively, if you have your run configuration selected in the main toolbar at the top, you can click
in the main toolbar or press ShiftF10 to run it.
This run configuration builds the artifacts, then starts the Tomcat server, and deploys the artifacts to the server. You should see the corresponding output in the Services tool window.
![Started Tomcat server and deployed application in the Services tool window Started Tomcat server and deployed application in the Services tool window](https://resources.jetbrains.com/help/img/idea/2024.3/rest_ws_tomcat_run_tool_window.png)
Once this is done, IntelliJ IDEA opens the specified URL in your web browser.
![Deployed application output in the web browser Deployed application output in the web browser](https://resources.jetbrains.com/help/img/idea/2024.3/rest_all_web_browser_output.png)
If not, try opening the URL yourself: http://localhost:8080/RestTomcatHelloWorld_war_exploded/api/hello-world
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>
Thanks for your feedback!