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 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:
- IntelliJ IDEA Ultimate
Java Enterprise development is not supported in the free IntelliJ IDEA Community Edition. For more information, see IntelliJ IDEA Ultimate vs IntelliJ IDEA Community Edition
- Relevant bundled plugins
By default, all necessary plugins are bundled and enabled in IntelliJ IDEA Ultimate. If something does not work, make sure that the following plugins are enabled:
Jakarta EE Platform
Jakarta EE: Application Servers
Jakarta EE: Web/Servlets
Jakarta EE: RESTful Web Services (JAX-RS)
GlassFish
For more information, see Install plugins.
- 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.
- GlassFish
The GlassFish application server version 4.0 or later. You can get the latest release from the official repository. The Web Profile subset should be enough for the purposes of this tutorial.
note
This tutorial uses Oracle OpenJDK 17, Jakarta EE 9.1, and GlassFish 6.2.5. For more information about the compatibility between other GlassFish, Java, and Jakarta EE versions, see https://github.com/eclipse-ee4j/glassfish#compatibility.
- Tomcat
The Tomcat application server version 7 or later.
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 Jakarta EE.
Enter a name for your project:
RestGlassfishHelloWorld
. For this tutorial, use Oracle OpenJDK 17 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 9.1 because that's what GlassFish 6.2.5 used in this tutorial is compatible with.
tip
For GlassFish 5, select the Java EE 8 specification. For GlassFish 7, select Jakarta EE 10.
In the Dependencies list, select the following:
Contexts and Dependency Injection (CDI)
RESTful Web Services (JAX-RS)
Servlet
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/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
{...}
Let IntelliJ IDEA know where the GlassFish application server is located.
Press CtrlAlt0S 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.
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 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 press ShiftF10 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 opening the URL yourself: http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/hello-world
If you get a 404
error, make sure you have selected the Jakarta EE specification version that is compatible with your version of GlassFish when creating the project.
For more information, see the GlassFish version compatibility.
Thanks for your feedback!