How to create JAX-RS Jersey Java EE RESTful web applications in IntelliJ IDEA Ultimate Edition

A friendly guide to creating JAX-RS Jersey Java EE RESTful web applications in IntelliJ IDEA Ultimate Edition

Before you start

Ensure you have an application server installed and configured in IntelliJ IDEA (tested with Tomcat 9.0.26 and Glassfish 5.0.0)

Project creation

Creating a project in IntelliJ IDEA Ultimate Edition

  1. Start IntelliJ IDEA and from the main menu, select "Create a new project"
  2. Among the different project types that are shown, choose Java Enterprise
  3. Check the Web Application checkbox among the additional libraries (do not check "RESTful Web Service")
  4. Click "Next" and continue as you like through the project creation wizard

Convert the project to a Maven project

Once the project has been created and is opened in the IDE, follow these steps

  1. Right click on your project's name in the Project tool window (file explorer)
  2. Select "Add framework support" and from the contextual menu, add Maven support
  3. Click "OK" and let the IDE add and process the new files generated for the maven project

Configure Maven (pom.xml)

  1. Locate the pom.xml Maven configuration file and replace its content with the provided pom.xml file
  2. Inside pom.xml replace the groupId by the one of your project
    • If you have a base package created, use this base package as a groupId
    • If not, create a base package in the src/main/java folder and use the same name for the groupId
  3. Inside pom.xml replace the artifactId by a significant name describing your project (project's name for example)
  4. If wanted/needed, change the version (still inside pom.xml)
  5. Enable Auto Import for newly added Maven dependency if asked

     <!-- Example: base package is com.example.webapi, located in the src/main/java folder and project name is "Hello World API" -->
    
     <groupId>com.example.webapi</groupId>
     <artifactId>Hello World API</artifactId>
    

Configure Jersey for JAX-RS (pom.xml)

In pom.xml locate the jersey.version tag and choose the version you want to use (latest version is highly recommended (2.29.1 at the time of writing))

Configure web application (web.xml)

  1. Locate the web.xml file - it should be at web/WEB-INF/web.xml - and replace it content with the provided web.xml file
  2. Replace the param-value for the jersey.config.server.provider.packages init param of the API Root servlet with the groupId you selected (and wrote into pom.xml)
  3. (Optional) Change the base url-pattern for your API as you like in the API Root servlet-mapping

Configure IntelliJ IDEA to work with your application

Artifact configuration

For IntelliJ IDEA to work as expected with your application (deployment in a local application server), follow these steps

  1. Hit Ctrl+Alt+Shift+S (Windows) or +; (MacOS) to access the Project Structure dialog
  2. Under Project Settings go to Artifacts
  3. Select the existing artifact (should be ${yourProjectName}:war exploded)
  4. In the right pane, select all "Available Elements" and right click to add of of them into /WEB-INF/lib

You will have to repeat those steps each time you add a dependency with Maven to ensure your project will work

Application server configuration (build & run configuration)

Now your project is all set up and should be ready for development but you have to ensure your application server is well set too

If you already have an application server set (default with the method described above)

You just have to ensure there is no warning in the application server configuration (inside IntelliJ IDEA) and that the artifact being deployed is the one you just configured

If you do not have an application server set for your project

  1. Create a new configuration and select the application server of your choice.
  2. Ensure there is no warning or error in the configuration
  3. Select the artifact you just configured for deployment

Conclusion

You should now be able to run your project, create and play with resources. Thanks for using this guide.