RESTful service with TomEE and IntelliJ IDEA

In this tutorial you’ll build a basic RESTful service using the Java EE APIs, IntelliJ IDEA, and TomEE.

Overview

  1. Setup the TomEE application server in IntelliJ.
  2. Create a new project.
  3. Write a RESTful Java class.
  4. Run.

Used in This Tutorial

Prerequisites

  • Download and install IntelliJ.
  • Download and unpack TomEE.

Tutorial

  1. Setup the TomEE application server in IntelliJ.
    1. Start IntelliJ, and on the startup screen go to Configure -> Settings -> Application Servers.Screen Shot 2013-06-04 at 1.54.26 PM
    2. Click the “+” button (top center) to add an application server.
    3. Select “TomEE” for the type of server.Screen Shot 2013-06-04 at 1.55.41 PM
    4. Enter the directory where you unpacked TomEE.Screen Shot 2013-06-04 at 1.56.55 PM
    5. Click “Ok” and “Ok” again.  Then click the little back button in the start up screen to go back to the main menu.
  2. Create a new project.
    1. Select “Create New Project” in the IntelliJ startup screen.
    2. Select “Java Module”
    3. Name it “MyFirstRESTfulService”.
    4. There’s an option here to select your JDK. You may need to configure your JDK, first. This is an IntelliJ requirement, and I don’t remember how I did this, I hope it’s straight forward enough for you.
    5. Click “Next”.
    6. Select “Application Server” then select “TomEE” from the drop down box on the right.Screen Shot 2013-06-04 at 2.00.42 PM
    7. Select “RESTful service” and “Set up library later”.
    8. Click “Finish”.
  3. Write a RESTful Java class.
    1. Double click the project name to expand the tree view and you will see the “src” directory.
    2. Right click “src” and create a package (select New -> Package) called “org.mybiz”.
    3. Right click “org.mybiz” and create a class (select New -> Java Class) called “Greeter”.
    4. Edit the Greeter class until it reads like this:
      package org.mybiz;
      
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      
      @Path("greeting")
      public class Greeter {
          @GET
          public String sayHi() {
              return "Hi!!";
          }
      }
  4. Run. Click the green play button to run the RESTful service. You should see messages in an output window at the bottom of IntelliJ. Look for a line like
    INFO: REST Service: http://localhost:8080/greeting/*  -> Pojo org.mybiz.Greeter

    This indicates the service was successfully deployed at the URL specified. Check for exceptions in case something failed.Screen Shot 2013-06-04 at 2.10.15 PM

Point your browser to the correct URL: http://localhost:8080/greeting/, and it works, yay!Screen Shot 2013-06-04 at 2.11.00 PM