Creating and Running Your First RESTful Web Service on GlassFish Application Server
This feature is only supported in the Ultimate edition.
This tutorial illustrates developing a simple RESTful web service and deploying it to the GlassFish application server.
- Before you start
- Configuring the GlassFish server in IntelliJ IDEA
- Configuring the JDK
- Creating a project
- Exploring the project structure
- Developing source code
- Examining the generated artifact configuration
- Exploring and completing the run configuration
- Running the application
Before you start
- Make sure you are using IntelliJ IDEA ULTIMATE Edition.
- Install the Java SE Development Kit (JDK), version 1.8 or later, see Download Oracle JDK.
- Download the GlassFish application server, version 3.0.1 or later, see Download GlassFish.
- Make sure a web browser is available on your computer.
Configuring the GlassFish server in IntelliJ IDEA
- Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing for Windows and Linux or for macOS, and click Application Servers under Build, Execution, Deployment.
- On the Application Servers page that opens, click above the central pane and choose GlassFish Server from the list.
- In the right-hand pane, specify the GlassFish Server installation folder in the GlassFish Home field. Type the path to it manually or click and choose the installation folder in the dialog box that opens. IntelliJ IDEA detects the version of the application server and automatically fills in the Name field as follows: GlassFish <version>. In our example it is GlassFish 4.1.1.
The other fields are filled automatically or are optional, so just click OK.
Configuring the JDK
- Press Ctrl+Shift+Alt+S or choose on the main menu.
- In the Project Structure Dialog that opens, choose SDK's under thePlatform Settings.
- On the SDKs page that opens, click above the central pane and choose JDK.
- In the right-hand pane, specify the installation folder of the Java SE Development Kit (JDK) to use. Type the path manually or click and choose the installation folder in the dialog box that opens. IntelliJ IDEA detects the version of the JDK and automatically enters it in the Name field. In our example it is 1.8.
All the mandatory fields in the other tabs are filled in automatically, so just click OK.
Creating a project
- Click Create New Project on the Welcome screen, or choose File | New | Project on the main menu.
The New Project wizard opens.
- On the first, Project Category and Options page of the Wizard:
- In the left-hand pane, select Java Enterprise.
- From the Project SDK list, select the JDK to use. In our example, it is 1.8.
- From the Application Server drop-down list, choose GlassFish 4.1.1.
- From the JavaEE Version drop down list, choose JavaEE 7.
- In the Additional Libraries and Frameworks area, select the Web Application and RESTful Web Service checkboxes.
- Choose the Download option in the area below the Additional Libraries and Frameworks list. The area is displayed only after you have selected the Web Application and RESTful Web Service checkboxes.
- Click Next.
- On the second, Project Name and Location page of the Wizard, specify the name for your new project, in our example it is Rest_glassfish_hello_world.
Click Finish and wait while IntelliJ IDEA is creating the project.
Exploring the project structure
When the project is created, you will see something similar to this in the Project tool window:
- rest_glassfish_hello_world is a module folder (which in this case coincides with the project folder).
- The
.idea
folder and the fileRest_glassfish_hello_world.iml
contain configuration data for your project and module respectively. - The folder
src
is for your Java source code. - The folder
web
is for the web part of your application. At the moment this folder contains the deployment descriptorWEB-INF\web.xml
. - External Libraries include your JDK and the JAR files for working with the GlassFish Server.
Developing source code
Our Hello World application will contain a resource class HelloWorld.java
and a configuration class MyApplication
. Its only function will be to output the text Hello World.
- In the
src
folder, create theHelloWorld.java
class with the following code:import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; // The Java class will be hosted at the URI path "/helloworld" @Path("/helloworld") 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"; } }
- In the
src
folder, create theMyApplication.java
class with the following code:import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; import java.util.HashSet; import java.util.Set; //Defines the base URI for all resource URIs. @ApplicationPath("/") //The java class declares root resource and provider classes public class MyApplication extends Application{ //The method returns a non-empty collection with classes, that must be included in the published JAX-RS application @Override public Set<Class<?>> getClasses() { HashSet h = new HashSet<Class<?>>(); h.add( HelloWorld.class ); return h; } }
Examining the generated artifact configuration
Besides building a RESTful-specific project structure, IntelliJ IDEA has also configured an artifact for us.
The word artifact in IntelliJ IDEA may mean one of the following:
- An artifact configuration, i.e. a specification of the output to be generated for a project;
- An actual output generated according to such a specification (configuration).
- Open the Project Structure dialog by pressing Ctrl+Shift+Alt+S or choosing File | Project Structure on the main menu.
- Under Project Settings, select Artifacts.
The available artifact configurations are shown in the central pane under and . Currently there is only one configuration
rest_glassfish_hello_world:war exploded
, it is a decompressed web application archive (WAR), a directory structure that is ready for deployment onto a web server. -
The artifact settings are shown in the right-hand pane of the dialog: Learn more about artifacts at Working with Artifacts and Artifacts.
IntelliJ IDEA has already filled in all the mandatory fields, no changes are required from our side, so just click Cancel to leave the dialog.
Exploring and completing the run configuration
In IntelliJ IDEA, any application is launched according to a dedicated run configuration. During the project creation, we have specified the GlassFish Server as the application server for running our application. Based on this choice and the annotations from the code, IntelliJ IDEA has created a run configuration and filled almost all the mandatory fields.
- Choose on the main menu.
- In the Edit Configuration dialog box that opens, expand the GlassFish Server node and click GlassFish 4.1.1. The right-hand pane shows the settings of the automatically generated run configuration.
- The Application Server field shows
GlassFish 4.1.1
, which is the installation of the GlassFish Server that was chosen during the project creation. The Name field also showsGlassFish 4.1.1
, IntelliJ IDEA has automatically named the generated configuration after the appointed application server. - In the Open browser area, the After launch checkbox is selected, so the page with the application output will be opened automatically.
In the text box below, we need to specify the URL address of the page to open. In our example, it is
http://localhost:8080/rest_glassfish_hello_world_war_exploded/helloworld
. - To have rest_glassfish_hello_world:war exploded deployed automatically on launching the run configuration, the artifact has to be marked for deployment. If you have completed the project creation steps successfully, the artifact is marked for deployment automatically. If it is not, IntelliJ IDEA displays a warning No artifacts marked for deployment and a Fix button. When you click Fix, IntelliJ IDEA opens the Deployment tab where the rest_glassfish_hello_world:war exploded is added to the Deploy on the server startup list.
- The Application Server field shows
Running the application
Click on the toolbar. After that:
- IntelliJ IDEA compiles your source code and builds an application artifact.
- The Run Tool Window opens. IntelliJ IDEA starts the server and deploys the artifact on it.
- Finally, your default web browser starts and you see the application output
Hello World
there.