IntelliJ IDEA 2016.3 Help

Creating and Running Your First RESTful Web Service on GlassFish Application Server

This tutorial illustrates developing a simple RESTful web service and deploying it to the GlassFish application server.

Before you start

  1. Make sure you are using IntelliJ IDEA ULTIMATE Edition.
  2. Install the Java SE Development Kit (JDK), version 1.8 or later, see Download Oracle JDK.
  3. Download the GlassFish application server, version 3.0.1 or later, see Download GlassFish.
  4. Make sure a web browser is available on your computer.

Configuring the GlassFish server in IntelliJ IDEA

  1. Open the Settings / Preferences Dialog by pressing Ctrl+Alt+S or by choosing File | Settings for Windows and Linux or IntelliJ IDEA | Preferences for OS X, and click Application Servers under Build, Execution, Deployment.
  2. On the Application Servers page that opens, click /help/img/idea/2016.3/new.png above the central pane and choose GlassFish Server from the list.
    /help/img/idea/2016.3/rest_ws_glassfish_configure_app_server_step_1.png
  3. In the right-hand pane, specify the GlassFish Server installation folder in the GlassFish Home field. Type the path to it manually or click /help/img/idea/2016.3/browseButton.png 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.
    /help/img/idea/2016.3/rest_ws_glassfish_configure_app_server_step_2.png

    The other fields are filled automatically or are optional, so just click OK.

Configuring the JDK

  1. Press Ctrl+Shift+Alt+S or choose File | Project Structure on the main menu.
  2. In the Project Structure Dialog that opens, choose SDK's under thePlatform Settings.
  3. On the SDKs page that opens, click /help/img/idea/2016.3/new.png above the central pane and choose JDK.
    rest_ws_glassfish_configure_jdk_step_1.png
  4. In the right-hand pane, specify the installation folder of the Java SE Development Kit (JDK) to use. Type the path manually or click /help/img/idea/2016.3/browseButton.png 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.
    rest_ws_glassfish_configure_jdk_step_2.png

    All the mandatory fields in the other tabs are filled in automatically, so just click OK.

Creating a project

  1. Click Create New Project on the Welcome screen, or choose File | New | Project on the main menu.

    The New Project wizard opens.

  2. On the first, Project Category and Options page of the Wizard:
    1. In the left-hand pane, select Java Enterprise.
    2. From the Project SDK list, select the JDK to use. In our example, it is 1.8.
    3. From the Application Server drop-down list, choose GlassFish 4.1.1.
    4. From the JavaEE Version drop down list, choose JavaEE 7.
    5. In the Additional Libraries and Frameworks area, select the Web Application and RESTful Web Service check boxes.
    6. 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 check boxes.
    7. Click Next.
    /help/img/idea/2016.3/rest_ws_glassfish_create_project_step_1.png
  3. 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.

    /help/img/idea/2016.3/rest_ws_glassfish_create_project_step_2.png

Exploring the project structure

When the project is created, you will see something similar to this in the Project tool window:

/help/img/idea/2016.3/rest_ws_glassfish_project_structure.png
  • rest_glassfish_hello_world is a module folder (which in this case coincides with the project folder).
  • The .idea folder and the file Rest_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 descriptor WEB-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.

  1. In the src folder, create the HelloWorld.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"; } }
  2. In the src folder, create the MyApplication.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).
Let's have a look at this configuration.

  1. Open the Project Structure dialog by pressing Ctrl+Shift+Alt+S or choosing File | Project Structure on the main menu.
  2. Under Project Settings, select Artifacts.

    The available artifact configurations are shown in the central pane under /help/img/idea/2016.3/new.png and /help/img/idea/2016.3/delete.png. 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.

  3. The artifact settings are shown in the right-hand pane of the dialog:

    rest_ws_glassfish_artifact.png
    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.

  1. Choose Run | Edit Configuration on the main menu. Alternatively, click Shift+Alt+F10 and select Edit Configuration from the pop-up menu.
  2. 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.
    rest_ws_glassfish_run_config_server_tab.png
    • 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 shows GlassFish 4.1.1, IntelliJ IDEA has automatically named the generated configuration after the appointed application server.
    • In the Open browser area, the After launch check box 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.
      rest_ws_glassfish_no_artifact_marked_for_deployment.png
      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.
      rest_ws_glassfish_run_config_deployment_tab.png
    All the other fields are filled in automatically or are optional, so just click OK to save the run configuration.

Running the application

Click /help/img/idea/2016.3/run.png on the toolbar. After that:

  1. IntelliJ IDEA compiles your source code and builds an application artifact.
  2. The Run Tool Window opens. IntelliJ IDEA starts the server and deploys the artifact on it.
  3. Finally, your default web browser starts and you see the application output Hello World there.
    rest_ws_glassfish_result_in_browser.png

See Also

Last modified: 29 November 2016