Tutorial: Your first RESTful web service
Last modified: 10 August 2022Required plugins: Jakarta EE Platform, Jakarta EE: Application Servers, Jakarta EE: Web/Servlets, Jakarta EE: RESTful Web Services (JAX-RS), GlassFish (bundled)
This tutorial describes how to create a simple RESTful web service in IntelliJ IDEA and deploy it to the GlassFish 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 GlassFish 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 or OpenJDK.
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.
note
This tutorial uses JDK 1.8, Java EE 8, and GlassFish 4.1.1. For more information about the compatibility between other GlassFish and Java versions, see https://github.com/eclipse-ee4j/glassfish#compatibility.
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 simple web application.
From the main menu, select File | New | Project.
In the New Project dialog, select Java Enterprise.
Enter a name for your project:
RestGlassfishHelloWorld
. For this tutorial, use Java 1.8 as the project SDK and select the REST service template. Don't select or add an application server, we will do it later. Select Maven and JUnit. Click Next to continue.In the Dependencies list, select the following:
CDI
JAX-RS
Servlet
Click Create.
IntelliJ IDEA creates the default project structure.
Explore the default project structure
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 Ctrl+Shift+N 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/RestGlassfishHelloWorld/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/RestGlassfishHelloWorld/HelloApplication.java
{...}
Configure the application server
Let IntelliJ IDEA know where the GlassFish application server is located.
Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Application Servers.
Click
and select Glassfish Server.
Specify the path to the GlassFish server install location. IntelliJ IDEA detects and sets the name and version appropriately.
note
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 (this tutorial uses Java 8). Press Ctrl+Alt+Shift+S to open the Project Structure dialog and copy the JDK home path under SDKs. Then open the GlassFish environment configuration file in the GlassFish installation directory (<glassfish_root>
\glassfish on Windows or <glassfish_root>\config \asenv.bat /glassfish on macOS or Linux) and add the path to the JDK home path as the value for/config /asenv.conf AS_JAVA
, for example, like this:AS_JAVA=/Library/Java/jdk-1.8.0.jdk/Contents/Home
tip
When you have an application server configured, you can select it when creating a new project or in the run configuration.
Create a run configuration
IntelliJ IDEA needs a run configuration to build the artifacts and deploy them to your application server.
From the main menu, select Run | Edit Configurations.
In the Run/Debug Configurations dialog, click
, expand the Glassfish 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 Server tab, set the Server Domain to
domain1
.On the Deployment tab, add the artifact that you want to deploy:
RestGlassfishHelloWorld:war exploded
On the Server tab, set the URL to point to the root resource:
http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/hello-world
Click OK to save the run configuration.
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 GlassFish server, and deploys the artifacts to the server. You should see the corresponding output in the Run tool window.

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

If not, try openning the URL yourself: http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/hello-world
Thanks for your feedback!